home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / TCOLLECT.CPP < prev    next >
Text File  |  1995-08-29  |  4KB  |  207 lines

  1. // Borland C++ - (C) Copyright 1992 by Borland International
  2.  
  3. /*------------------------------------------------------------*/
  4. /* filename -       tcollect.cpp                              */
  5. /*                                                            */
  6. /* function(s)                                                */
  7. /*                  TNSCollection member functions            */
  8. /*------------------------------------------------------------*/
  9.  
  10. #if !defined( __OBJECT_H )
  11. #include <object.h>
  12. #endif  // __OBJECT_H
  13.  
  14. #if !defined( __TCOLLECT_H )
  15. #include <tcollect.h>
  16. #endif  // __TCOLLECT_H
  17.  
  18. #if !defined( __STDLIB_H )
  19. #include <StdLib.h>
  20. #endif  // __STDLIB_H
  21.  
  22. #if !defined( __MEM_H )
  23. #include <Mem.h>
  24. #endif  // __MEM_H
  25.  
  26. TNSCollection::TNSCollection( ccIndex aLimit, ccIndex aDelta ) :
  27.     count( 0 ),
  28.     items( 0 ),
  29.     limit( 0 ),
  30.     delta( aDelta ),
  31.     shouldDelete( True )
  32. {
  33.     setLimit( aLimit );
  34. }
  35.  
  36. TNSCollection::TNSCollection() :
  37.     count( 0 ),
  38.     items( 0 ),
  39.     limit( 0 ),
  40.     delta( 0 ),
  41.     shouldDelete( True )
  42. {
  43.     items = 0;
  44. }
  45.  
  46. TNSCollection::~TNSCollection()
  47. {
  48.     if( shouldDelete )
  49.         freeAll();
  50.     setLimit(0);
  51. }
  52.  
  53. void *TNSCollection::at( ccIndex index )
  54. {
  55.     return items[index];
  56. }
  57.  
  58. void TNSCollection::atRemove( ccIndex index )
  59. {
  60.     if( index >= count )
  61.         error(1,0);
  62.  
  63.     count--;
  64.     memmove( &items[index], &items[index+1], (count-index)*sizeof(void *) );
  65. }
  66.  
  67. void TNSCollection::atFree( ccIndex index )
  68. {
  69.     void *item = at( index );
  70.     atRemove( index );
  71.     freeItem( item );
  72. }
  73.  
  74. void TNSCollection::atInsert(ccIndex index, void *item)
  75. {
  76.     if( index < 0 )
  77.         error(1,0);
  78.     if( count == limit )
  79.         setLimit(count + delta);
  80.  
  81.     memmove( &items[index+1], &items[index], (count-index)*sizeof(void *) );
  82.     count++;
  83.  
  84.     items[index] = item;
  85. }
  86.  
  87. void TNSCollection::atPut( ccIndex index, void *item )
  88. {
  89.     if( index >= count )
  90.         error(1,0);
  91.  
  92.     items[index] = item;
  93. }
  94.  
  95. void TNSCollection::remove( void *item )
  96. {
  97.     atRemove( indexOf(item) );
  98. }
  99.  
  100. void TNSCollection::removeAll()
  101. {
  102.     count = 0;
  103. }
  104.  
  105. void TNSCollection::error( ccIndex code, ccIndex )
  106. {
  107.     exit(212 - code);
  108. }
  109.  
  110. void *TNSCollection::firstThat( ccTestFunc Test, void *arg )
  111. {
  112.     for( ccIndex i = 0; i < count; i++ )
  113.         {
  114.         if( Test( items[i], arg ) == True )
  115.             return items[i];
  116.         }
  117.     return 0;
  118. }
  119.  
  120. void *TNSCollection::lastThat( ccTestFunc Test, void *arg )
  121. {
  122.     for( ccIndex i = count; i > 0; i-- )
  123.         {
  124.         if( Test( items[i-1], arg ) == True )
  125.             return items[i-1];
  126.         }
  127.     return 0;
  128. }
  129.  
  130. void TNSCollection::forEach( ccAppFunc action, void *arg )
  131. {
  132.     for( ccIndex i = 0; i < count; i++ )
  133.         action( items[i], arg );
  134. }
  135.  
  136. void TNSCollection::free( void *item )
  137. {
  138.     remove( item );
  139.     freeItem( item );
  140. }
  141.  
  142. void TNSCollection::freeAll()
  143. {
  144.     for( ccIndex i =  0; i < count; i++ )
  145.         freeItem( at(i) );
  146.     count = 0;
  147. }
  148.  
  149. void TNSCollection::freeItem( void *item )
  150. {
  151.     delete item;
  152. }
  153.  
  154. #pragma warn -rvl
  155. ccIndex TNSCollection::indexOf(void *item)
  156. {
  157.     for( ccIndex i = 0; i < count; i++ )
  158.         if( item == items[i] )
  159.             return i;
  160.  
  161.     error(1,0);
  162. }
  163. #pragma warn .rvl
  164.  
  165. ccIndex TNSCollection::insert( void *item )
  166. {
  167.     ccIndex loc = count;
  168.     atInsert( count, item );
  169.     return loc;
  170. }
  171.  
  172. void TNSCollection::pack()
  173. {
  174.     void **curDst = items;
  175.     void **curSrc = items;
  176.     void **last = items + count;
  177.     while( curSrc < last )
  178.         {
  179.         if( *curSrc != 0 )
  180.             *curDst++ = *curSrc;
  181.         *curSrc++;
  182.         }
  183. }
  184.  
  185. void TNSCollection::setLimit(ccIndex aLimit)
  186. {
  187.     if( aLimit < count )
  188.         aLimit =  count;
  189.     if( aLimit > maxCollectionSize)
  190.         aLimit = maxCollectionSize;
  191.     if( aLimit != limit )
  192.         {
  193.         void **aItems;
  194.         if (aLimit == 0 )
  195.             aItems = 0;
  196.         else
  197.             {
  198.             aItems = new void *[aLimit];
  199.             if( count !=  0 )
  200.                 memcpy( aItems, items, count*sizeof(void *) );
  201.             }
  202.         delete items;
  203.         items =  aItems;
  204.         limit =  aLimit;
  205.         }
  206. }
  207.