home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dho.zip / DHO / SRC / SOURCE.ZIP / list.cc < prev    next >
C/C++ Source or Header  |  1995-08-27  |  6KB  |  270 lines

  1. /****************************************/
  2. /*    Developer Helper Object Set       */
  3. /*  (C) 1994-95 Thomas E. Bednarz, Jr.  */
  4. /*     All rights reserved              */
  5. /***************************************/
  6.  
  7. /* $Id: list.cc 1.4 1995/08/13 03:21:12 teb Exp $ */
  8.  
  9.  
  10. #include"list.h"
  11. #include"object.h"
  12.  
  13. #include<stdlib.h>
  14. #include<memory.h>
  15.  
  16.  
  17. //-------------------------------------------------------------------
  18. //  TList
  19. TList::TList( ListIndex aLimit, ListIndex aDelta )
  20. {
  21.    fCount = 0;
  22.    fItems = 0;
  23.    fDelta = aDelta;
  24.    setLimit( aLimit );
  25. }
  26.  
  27.  
  28. //-------------------------------------------------------------------
  29. //  TList
  30. TList::TList()
  31. {
  32.     fItems = 0;
  33.     fCount=0;
  34.     fLimit=0;
  35.     fDelta=0;
  36.     fShouldDelete=True;
  37. }
  38.  
  39.  
  40. //-------------------------------------------------------------------
  41. //  ~TList
  42. TList::~TList()
  43. {
  44.     if( fShouldDelete )
  45.         freeAll();
  46.     setLimit(0);
  47. }
  48.  
  49.  
  50. //-------------------------------------------------------------------
  51. //  at
  52. TObject *TList::at( ListIndex index )
  53. {
  54.     return fItems[index];
  55. }
  56.  
  57. //-------------------------------------------------------------------
  58. //  atRemove
  59. void TList::atRemove( ListIndex index )
  60. {
  61.     if( index >= fCount )
  62.         error(1,0);
  63.  
  64.     fCount--;
  65.     memmove( &fItems[index], &fItems[index+1], (fCount-index)*sizeof(TObject *) );
  66. }
  67.  
  68.  
  69. //-------------------------------------------------------------------
  70. //  atFree
  71. void TList::atFree( ListIndex index )
  72. {
  73.     TObject *item = at( index );
  74.     atRemove( index );
  75.     freeItem( item );
  76. }
  77.  
  78.  
  79. //-------------------------------------------------------------------
  80. //  atInsert
  81. void TList::atInsert(ListIndex index, TObject *item)
  82. {
  83.     if( index < 0 )
  84.         error(1,0);
  85.     if( fCount == fLimit )
  86.         setLimit(fCount + fDelta);
  87.  
  88.     memmove( &fItems[index+1], &fItems[index], (fCount-index)*sizeof(TObject *) );
  89.     fCount++;
  90.  
  91.     fItems[index] = item;
  92. }
  93.  
  94.  
  95. //-------------------------------------------------------------------
  96. //  atPut
  97. void TList::atPut( ListIndex index, TObject *item )
  98. {
  99.     if( index >= fCount )
  100.         error(1,0);
  101.  
  102.     fItems[index] = item;
  103. }
  104.  
  105.  
  106. //-------------------------------------------------------------------
  107. //  remove
  108. void TList::remove( TObject *item )
  109. {
  110.     atRemove( indexOf(item) );
  111. }
  112.  
  113.  
  114. //-------------------------------------------------------------------
  115. //  removeAll
  116. void TList::removeAll()
  117. {
  118.     fCount = 0;
  119. }
  120.  
  121.  
  122. //-------------------------------------------------------------------
  123. //  error
  124. void TList::error( ListIndex code, ListIndex )
  125. {
  126.     exit(212 - code);
  127. }
  128.  
  129.  
  130. //-------------------------------------------------------------------
  131. //  firstThat
  132. TObject *TList::firstThat( TestFunc Test, void *arg )
  133. {
  134.     for( ListIndex i = 0; i < fCount; i++ )
  135.         {
  136.         if( Test( fItems[i], arg ) == True )
  137.             return fItems[i];
  138.         }
  139.     return 0;
  140. }
  141.  
  142.  
  143. //-------------------------------------------------------------------
  144. //  lastThat
  145. TObject *TList::lastThat( TestFunc Test, void *arg )
  146. {
  147.     for( ListIndex i = fCount; i > 0; i-- )
  148.         {
  149.         if( Test( fItems[i-1], arg ) == True )
  150.             return fItems[i-1];
  151.         }
  152.     return 0;
  153. }
  154.  
  155.  
  156. //-------------------------------------------------------------------
  157. //  forEach
  158. void TList::forEach( AppFunc action, void *arg )
  159. {
  160.     for( ListIndex i = 0; i < fCount; i++ )
  161.         action( fItems[i], arg );
  162. }
  163.  
  164.  
  165. //-------------------------------------------------------------------
  166. //  free
  167. void TList::free( TObject *item )
  168. {
  169.     remove( item );
  170.     freeItem( item );
  171. }
  172.  
  173.  
  174. //-------------------------------------------------------------------
  175. //  freeAll
  176. void TList::freeAll()
  177. {
  178.     for( ListIndex i =  0; i < fCount; i++ )
  179.         freeItem( at(i) );
  180.     fCount = 0;
  181. }
  182.  
  183.  
  184. //-------------------------------------------------------------------
  185. //  freeItem
  186. void TList::freeItem( TObject *item )
  187. {
  188.     delete item;
  189. }
  190.  
  191. //-------------------------------------------------------------------
  192. //  indexOf
  193. ListIndex TList::indexOf(TObject *item)
  194. {
  195.     for( ListIndex i = 0; i < fCount; i++ )
  196.         if( item == fItems[i] )
  197.             return i;
  198.  
  199.     error(1,0);
  200.     return NULL;
  201. }
  202.  
  203.  
  204. //-------------------------------------------------------------------
  205. //  insert
  206. ListIndex TList::insert( TObject *item )
  207. {
  208.     ListIndex loc = fCount;
  209.     atInsert( fCount, item );
  210.     return loc;
  211. }
  212.  
  213.  
  214. //-------------------------------------------------------------------
  215. //  pack
  216. void TList::pack()
  217. {
  218.     TObject **curDst = fItems;
  219.     TObject **curSrc = fItems;
  220.     TObject **last = fItems + fCount;
  221.     while( curSrc < last )
  222.         {
  223.         if( *curSrc != 0 )
  224.             *curDst++ = *curSrc;
  225.         *curSrc++;
  226.         }
  227. }
  228.  
  229.  
  230. //-------------------------------------------------------------------
  231. //  setLimit
  232. void TList::setLimit(ListIndex aLimit)
  233. {
  234.     if( aLimit < fCount )
  235.         aLimit =  fCount;
  236.     if( aLimit > maxCollectionSize)
  237.         aLimit = maxCollectionSize;
  238.     if( aLimit != fLimit )
  239.         {
  240.         TObject **aitems;
  241.         if (aLimit == 0 )
  242.             aitems = 0;
  243.         else
  244.             {
  245.             aitems = new TObject *[aLimit];
  246.             if( fCount !=  0 )
  247.                 memcpy( aitems, fItems, fCount*sizeof(TObject *) );
  248.             }
  249.         delete fItems;
  250.         fItems =  aitems;
  251.         fLimit =  aLimit;
  252.         }
  253. }
  254.  
  255.  
  256. //-------------------------------------------------------------------
  257. //  getCount
  258. ListIndex TList::getCount(void)
  259.    return fCount; 
  260. }
  261.  
  262.  
  263. //-------------------------------------------------------------------
  264. //  getClassName
  265. const char *TList::getClassName(void)
  266. {
  267.    return "TList";
  268. }
  269.