home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 10.ddi / TVSRC.ZIP / TCOLLECT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.3 KB  |  245 lines

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