home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / src / TSortedCollection.cpp < prev    next >
C/C++ Source or Header  |  1998-01-19  |  2KB  |  101 lines

  1. /*
  2.  * TSortedCollection.cc
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. #define Uses_TNSSortedCollection
  13. #define Uses_opstream
  14. #define Uses_ipstream
  15. #define Uses_TSortedCollection
  16. #include <tvision/tv.h>
  17.  
  18.  
  19. ccIndex TNSSortedCollection::indexOf(void *item)
  20. {
  21.     ccIndex  i;
  22.  
  23.     if( search( keyOf(item), i ) == 0 )
  24.         return ccNotFound;
  25.     else
  26.         {
  27.         if( duplicates )
  28.             {
  29.             while( i < count && item != items[i] )
  30.                 i++;
  31.             }
  32.         if( i < count )
  33.             return i;
  34.         else
  35.             return ccNotFound;
  36.         }
  37. }
  38.  
  39. ccIndex TNSSortedCollection::insert( void *item )
  40. {
  41.     ccIndex  i;
  42.     if( search( keyOf(item), i ) == 0 || duplicates )   // order dependency!
  43.         atInsert( i, item );                            // must do Search
  44.                                                         // before calling
  45.                                                         // AtInsert
  46.     return i;
  47. }
  48.  
  49. void *TNSSortedCollection::keyOf( void *item )
  50. {
  51.     return item;
  52. }
  53.  
  54. Boolean TNSSortedCollection::search( void *key, ccIndex& index )
  55. {
  56.     ccIndex l = 0;
  57.     ccIndex h = count - 1;
  58.     Boolean res = False;
  59.     while( l <= h )
  60.         {
  61.         ccIndex i = (l +  h) >> 1;
  62.         ccIndex c = compare( keyOf( items[i] ), key );
  63.         if( c < 0 )
  64.             l = i + 1;
  65.         else
  66.             {
  67.             h = i - 1;
  68.             if( c == 0 )
  69.                 {
  70.                 res = True;
  71.                 if( !duplicates )
  72.                     l = i;
  73.                 }
  74.             }
  75.         }
  76.     index = l;
  77.     return res;
  78. }
  79.  
  80.  
  81. void TSortedCollection::write( opstream& os )
  82. {
  83.     TCollection::write( os );
  84.     os << (int)duplicates;
  85. }
  86.  
  87. void *TSortedCollection::read( ipstream& is )
  88. {
  89.     TCollection::read( is );
  90.     int temp;
  91.     is >> temp;
  92.     duplicates = Boolean(temp);
  93.     return this;
  94. }
  95.  
  96.  
  97. TSortedCollection::TSortedCollection( StreamableInit ) :
  98.     TCollection( streamableInit )
  99. {
  100. }
  101.