home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / sysba021.zip / SRC.ZIP / sysbar2 / Piper / cpuload / collect.cpp < prev    next >
C/C++ Source or Header  |  1997-09-05  |  3KB  |  150 lines

  1. /*
  2. ** Module   :COLLECT.CPP
  3. ** Abstract :Base Collection class implemetation
  4. **
  5. ** Update : Sun  13-03-94 Updated
  6. **          Fri  05/09/97 Last cleanup before sending it.
  7. */
  8.  
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <io.h>
  13.  
  14. #include "collect.h"
  15.  
  16. Collection::Collection(DWord aCount, DWord aDelta)
  17. {
  18.     dwCount = aCount;
  19.     dwLast  = 0;
  20.     dwDelta = aDelta;
  21.     ppData  = new Ptr[dwCount];
  22. }
  23.  
  24. Collection::~Collection()
  25. {
  26.     for(DWord i=0; i < dwLast;i++)
  27.         Free(ppData[i]);
  28.     delete ppData;
  29. }
  30.  
  31. Ptr Collection::Get(DWord index)
  32. {
  33.     if (index < dwLast)
  34.         return ppData[index];
  35.     else
  36.         return 0;
  37. }
  38.  
  39. void Collection::Add(Ptr newitem)
  40. {
  41.     if (dwLast < dwCount)
  42.         ppData[dwLast++] = newitem;
  43.     else
  44.     {
  45.         Ptr * tmp = new Ptr[dwCount + dwDelta];
  46.         memmove(tmp, ppData, sizeof(Ptr) * dwCount);
  47.         dwCount += dwDelta;
  48.         delete ppData;
  49.         ppData = tmp;
  50.         ppData[dwLast++] = newitem;
  51.     }
  52. }
  53. void Collection::At(Ptr p, DWord pos)
  54. {
  55.     if(dwLast < dwCount)
  56.     {
  57.         if(pos > dwLast)
  58.             pos = dwLast;
  59.         if(pos == dwLast)
  60.             ppData[dwLast++] = p;
  61.         else
  62.         {
  63.             memmove(&ppData[pos+1], &ppData[pos], sizeof(Ptr)*(dwLast - pos));
  64.             ppData[pos] = p;
  65.             dwLast++;
  66.         }
  67.     }
  68.     else
  69.     {
  70.         Ptr* tmp = new Ptr[dwCount+dwDelta];
  71.         if(pos)
  72.             memmove(tmp, ppData, sizeof(Ptr) * pos);
  73.         if(pos < dwLast)
  74.             memmove(&tmp[pos+1], &ppData[pos], sizeof(Ptr) * (dwLast - pos));
  75.         tmp[pos] = p;
  76.         dwCount += dwDelta;
  77.         dwLast++;
  78.         delete ppData;
  79.         ppData = tmp;
  80.     }
  81. }
  82.  
  83. Ptr Collection::Remove(DWord item)
  84. {
  85.     Ptr tmp = Get(item);
  86.     if(tmp)
  87.     {
  88.         memmove(&ppData[item], &ppData[item+1], sizeof(Ptr) * (dwCount-item-1));
  89.         dwLast--;
  90.     }
  91.     return tmp;
  92. }
  93.  
  94. void Collection::ForEach(ForEachFunc func)
  95. {
  96.     for(int i = 0; i < dwLast; i++)
  97.         func(ppData[i]);
  98. }
  99.  
  100. void Collection::RemoveAll()
  101. {
  102.     for(int i = 0; i < dwLast; i++)
  103.         Free(ppData[i]);
  104. }
  105.  
  106. //─────────────────────────────────────────────────────────────────────────────
  107.  
  108. void SortedCollection::Add(Ptr p)
  109. {
  110.     if(!dwLast)
  111.     {
  112.         Collection::Add(p);
  113.         return;
  114.     }
  115.     DWord pos = Look(p);
  116.     int rc = 0;
  117.  
  118.     if(pos < dwLast)
  119.         rc = Compare(p, Get(pos));
  120.     else
  121.     {
  122.         Collection::Add(p);
  123.         return;
  124.     }
  125.     if(( !rc && bDuplicates) || rc)
  126.         At(p, pos);
  127. }
  128.  
  129. DWord SortedCollection::Look(Ptr p)
  130. {
  131.     int l, m, h;
  132.  
  133.     l = m = 0;
  134.     h = dwLast-1;
  135.  
  136.     while(l <= h)
  137.     {
  138.         m = (l+h) >> 1;
  139.         int rc = Compare(p, Get(m));
  140.         if(!rc)
  141.             return m;
  142.         if(rc < 0)
  143.             h = m-1;
  144.         if(rc > 0)
  145.             l = m+1;
  146.     }
  147.     return l;
  148. }
  149.  
  150.