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

  1. /*------------------------------------------------------------*/
  2. /* filename -       tsortcol.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TNSSortedCollection 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_TNSSortedCollection
  19. #define Uses_opstream
  20. #define Uses_ipstream
  21. #define Uses_TSortedCollection
  22. #include <tv.h>
  23.  
  24.  
  25. ccIndex TNSSortedCollection::indexOf(void *item)
  26. {
  27.     ccIndex  i;
  28.  
  29.     if( search( keyOf(item), i ) == 0 )
  30.         return ccNotFound;
  31.     else
  32.         {
  33.         if( duplicates )
  34.             {
  35.             while( i < count && item != items[i] )
  36.                 i++;
  37.             }
  38.         if( i < count )
  39.             return i;
  40.         else
  41.             return ccNotFound;
  42.         }
  43. }
  44.  
  45. ccIndex TNSSortedCollection::insert( void *item )
  46. {
  47.     ccIndex  i;
  48.     if( search( keyOf(item), i ) == 0 || duplicates )   // order dependency!
  49.         atInsert( i, item );                            // must do Search
  50.                                                         // before calling
  51.                                                         // AtInsert
  52.     return i;
  53. }
  54.  
  55. void *TNSSortedCollection::keyOf( void *item )
  56. {
  57.     return item;
  58. }
  59.  
  60. Boolean TNSSortedCollection::search( void *key, ccIndex& index )
  61. {
  62.     ccIndex l = 0;
  63.     ccIndex h = count - 1;
  64.     Boolean res = False;
  65.     while( l <= h )
  66.         {
  67.         ccIndex i = (l +  h) >> 1;
  68.         ccIndex c = compare( keyOf( items[i] ), key );
  69.         if( c < 0 )
  70.             l = i + 1;
  71.         else
  72.             {
  73.             h = i - 1;
  74.             if( c == 0 )
  75.                 {
  76.                 res = True;
  77.                 if( !duplicates )
  78.                     l = i;
  79.                 }
  80.             }
  81.         }
  82.     index = l;
  83.     return res;
  84. }
  85.  
  86. void TSortedCollection::write( opstream& os )
  87. {
  88.     TCollection::write( os );
  89.     os << (int)duplicates;
  90. }
  91.  
  92. void *TSortedCollection::read( ipstream& is )
  93. {
  94.     TCollection::read( is );
  95.     int temp;
  96.     is >> temp;
  97.     duplicates = Boolean(temp);
  98.     return this;
  99. }
  100.  
  101.  
  102. TSortedCollection::TSortedCollection( StreamableInit ) :
  103.     TCollection( streamableInit )
  104. {
  105. }
  106.