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

  1. // Borland C++ - (C) Copyright 1992 by Borland International
  2.  
  3. /*------------------------------------------------------------*/
  4. /* filename -       tsortcol.cpp                              */
  5. /*                                                            */
  6. /* function(s)                                                */
  7. /*                  TNSSortedCollection 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. ccIndex TNSSortedCollection::indexOf(void *item)
  19. {
  20.     ccIndex  i;
  21.  
  22.     if( search( keyOf(item), i ) == 0 )
  23.         return ccNotFound;
  24.     else
  25.         {
  26.         if( duplicates )
  27.             {
  28.             while( i < count && item != items[i] )
  29.                 i++;
  30.             }
  31.         if( i < count )
  32.             return i;
  33.         else
  34.             return ccNotFound;
  35.         }
  36. }
  37.  
  38. ccIndex TNSSortedCollection::insert( void *item )
  39. {
  40.     ccIndex  i;
  41.     if( search( keyOf(item), i ) == 0 || duplicates )   // order dependency!
  42.         atInsert( i, item );                            // must do Search
  43.                                                         // before calling
  44.                                                         // AtInsert
  45.     return i;
  46. }
  47.  
  48. Boolean TNSSortedCollection::search( void *key, ccIndex& index )
  49. {
  50.     ccIndex l = 0;
  51.     ccIndex h = count - 1;
  52.     Boolean res = False;
  53.     while( l <= h )
  54.         {
  55.         ccIndex i = (l +  h) >> 1;
  56.         ccIndex c = compare( keyOf( items[i] ), key );
  57.         if( c < 0 )
  58.             l = i + 1;
  59.         else
  60.             {
  61.             h = i - 1;
  62.             if( c == 0 )
  63.                 {
  64.                 res = True;
  65.                 if( !duplicates )
  66.                     l = i;
  67.                 }
  68.             }
  69.         }
  70.     index = l;
  71.     return res;
  72. }
  73.  
  74.