home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / collect.cpp < prev    next >
C/C++ Source or Header  |  1998-03-31  |  4KB  |  195 lines

  1. /*
  2. ** Module   :COLLECT.CPP
  3. ** Abstract :Class Collection and Sorted Collection methods
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Sun  13/03/1994     Updated
  8. **      Sun  23/02/1997     Updated
  9. **      Wed  05/03/1997     Some routines moved to the .H file
  10. **                          to achieve maximum performance
  11. */
  12.  
  13. #include <string.h>
  14. #include <collect.h>
  15. #include <version.h>
  16.  
  17. Collection::Collection(unsigned aCount, unsigned aDelta)
  18. {
  19.     dwCount = aCount;
  20.     dwLast  = 0;
  21.     dwDelta = aDelta;
  22.     ppData  = new Ptr[dwCount];
  23. }
  24.  
  25. Collection::~Collection()
  26. {
  27.     RemoveAll();
  28.     delete ppData;
  29. }
  30.  
  31. void Collection::Add(Ptr newitem)
  32. {
  33.     if (dwLast < dwCount)
  34.         ppData[dwLast++] = newitem;
  35.     else
  36.     {
  37.         Ptr * tmp = new Ptr[dwCount + dwDelta];
  38.         memmove(tmp, ppData, sizeof(Ptr) * dwCount);
  39.         dwCount += dwDelta;
  40.         delete ppData;
  41.         ppData = tmp;
  42.         ppData[dwLast++] = newitem;
  43.     }
  44. }
  45. void Collection::At(Ptr p, unsigned pos)
  46. {
  47.     if(dwLast < dwCount)
  48.     {
  49.         if(pos > dwLast)
  50.             pos = dwLast;
  51.         if(pos == dwLast)
  52.             ppData[dwLast++] = p;
  53.         else
  54.         {
  55.             memmove(&ppData[pos+1], &ppData[pos], sizeof(Ptr)*(dwLast - pos));
  56.             ppData[pos] = p;
  57.             dwLast++;
  58.         }
  59.     }
  60.     else
  61.     {
  62.         Ptr* tmp = new Ptr[dwCount+dwDelta];
  63.         if(pos)
  64.             memmove(tmp, ppData, sizeof(Ptr) * pos);
  65.         if(pos < dwLast)
  66.             memmove(&tmp[pos+1], &ppData[pos], sizeof(Ptr) * (dwLast - pos));
  67.         tmp[pos] = p;
  68.         dwCount += dwDelta;
  69.         dwLast++;
  70.         delete ppData;
  71.         ppData = tmp;
  72.     }
  73. }
  74.  
  75. Ptr Collection::Remove(unsigned item)
  76. {
  77.     Ptr tmp = Get(item);
  78.     if(tmp)
  79.     {
  80.         memmove(&ppData[item], &ppData[item+1], sizeof(Ptr) * (dwCount-item-1));
  81.         dwLast--;
  82.     }
  83.     return tmp;
  84. }
  85.  
  86. void Collection::ForEach(ForEachFunc func)
  87. {
  88.     for(int i = 0; i < dwLast; i++)
  89.         func(ppData[i]);
  90. }
  91.  
  92. void Collection::RemoveAll()
  93. {
  94.     for(int i = 0; i < dwLast; i++)
  95.         Free(ppData[i]);
  96.     dwLast = 0;
  97. }
  98.  
  99. void Collection::Free(Ptr p)
  100. {
  101.     delete p;
  102. }
  103.  
  104. void Collection::move_items(PCollection src, unsigned from)
  105. {
  106.     if((src->dwLast + dwLast) >= dwCount)
  107.     {
  108.         Ptr* tmp = new Ptr[dwCount + src->dwLast];
  109.         memcpy(tmp, ppData, dwLast * sizeof(Ptr));
  110.         delete ppData;
  111.         ppData = tmp;
  112.         dwCount += src->dwLast;
  113.     }
  114.  
  115.     //Make free space
  116.     memmove(&ppData[from + src->dwLast],
  117.             &ppData[from],
  118.             (dwLast - from) * sizeof(Ptr));
  119.     memcpy(&ppData[from], src->ppData, src->dwLast * sizeof(Ptr));
  120.     dwLast += src->dwLast;
  121.     src->dwLast = 0;
  122. }
  123.  
  124. void Collection::remove_items(PCollection dest, unsigned from, unsigned count)
  125. {
  126.     dest->RemoveAll();
  127.     if((from + count) > dwLast)
  128.     {
  129.         if(from > dwLast)
  130.             return;
  131.         count = dwLast - from;
  132.     }
  133.  
  134.  
  135.     if(dest->dwCount < count)
  136.     {
  137.         delete dest->ppData;
  138.         dest->dwCount = count;
  139.         dest->ppData = new Ptr[count];
  140.     }
  141.  
  142.     dest->dwLast = count;
  143.     memcpy( dest->ppData,
  144.             &ppData[from],
  145.             count * sizeof(Ptr));
  146.  
  147.     if((dwLast-from-count) > 0)
  148.         memmove(&ppData[from],
  149.                 &ppData[from+count],
  150.                 sizeof(Ptr) * (dwLast-from-count));
  151.     dwLast -= count;
  152. }
  153.  
  154. void SortedCollection::Add(Ptr p)
  155. {
  156.     if(!dwLast)
  157.     {
  158.         Collection::Add(p);
  159.         return;
  160.     }
  161.     unsigned pos = Look(p);
  162.     int rc = 0;
  163.  
  164.     if(pos < dwLast)
  165.         rc = Compare(p, Get(pos));
  166.     else
  167.     {
  168.         Collection::Add(p);
  169.         return;
  170.     }
  171.     if(( !rc && bDuplicates) || rc)
  172.         At(p, pos);
  173. }
  174.  
  175. unsigned SortedCollection::Look(Ptr p)
  176. {
  177.     int l, m, h;
  178.  
  179.     l = m = 0;
  180.     h = dwLast-1;
  181.  
  182.     while(l <= h)
  183.     {
  184.         m = (l+h) >> 1;
  185.         int rc = Compare(p, Get(m));
  186.         if(!rc)
  187.             return m;
  188.         if(rc < 0)
  189.             h = m-1;
  190.         if(rc > 0)
  191.             l = m+1;
  192.     }
  193.     return l;
  194. }
  195.