home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 038 / dho_9a.zip / LIST.CC < prev    next >
C/C++ Source or Header  |  1994-10-12  |  6KB  |  263 lines

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