home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dho.zip / DHO / SRC / SOURCE.ZIP / sortlist.cc < prev    next >
C/C++ Source or Header  |  1995-08-27  |  3KB  |  125 lines

  1. /****************************************/
  2. /*    Developer Helper Object Set       */
  3. /*  (C) 1994-95 Thomas E. Bednarz, Jr.  */
  4. /*     All rights reserved              */
  5. /***************************************/
  6.  
  7. /* $Id: sortlist.cc 1.2 1995/08/13 03:21:12 teb Exp $ */
  8.  
  9.  
  10. #include"sortlist.h"
  11.  
  12. //-------------------------------------------------------------------
  13. //  TSortedList
  14. TSortedList::TSortedList(void)
  15. {
  16.    
  17. };
  18.  
  19.  
  20. //-------------------------------------------------------------------
  21. //  keyOf
  22. void *TSortedList::keyOf( TObject *item ) 
  23.    return item->getKey(); 
  24. }
  25.  
  26.  
  27. //-------------------------------------------------------------------
  28. //  TSortedList
  29. TSortedList::TSortedList( ListIndex aLimit, ListIndex aDelta) :
  30.         TList( aLimit, aDelta )
  31. {
  32.    fDelta = aDelta; 
  33.    setLimit( aLimit ); 
  34. }
  35.  
  36.  
  37. //-------------------------------------------------------------------
  38. //  indexOf
  39. ListIndex TSortedList::indexOf(TObject *item)
  40. {
  41.     ListIndex  i;
  42.  
  43.     if( search( keyOf(item), i ) == 0 )
  44.         return ccNotFound;
  45.     else
  46.         {
  47.         if( duplicates )
  48.             {
  49.             while( i < fCount && item != fItems[i] )
  50.                 i++;
  51.             }
  52.         if( i < fCount )
  53.             return i;
  54.         else
  55.             return ccNotFound;
  56.         }
  57. }
  58.  
  59.  
  60. //-------------------------------------------------------------------
  61. //  insert
  62. ListIndex TSortedList::insert( TObject *item )
  63. {
  64.     ListIndex  i;
  65.     if( search( keyOf(item), i ) == 0 || duplicates )   // order dependency!
  66.         atInsert( i, item );                            // must do Search
  67.                                                         // before calling
  68.                                                         // AtInsert
  69.     return i;
  70. }
  71.  
  72.  
  73. //-------------------------------------------------------------------
  74. //  search
  75. Boolean TSortedList::search( void *key, ListIndex& index )
  76. {
  77.     ListIndex l = 0;
  78.     ListIndex h = fCount - 1;
  79.     Boolean res = False;
  80.     while( l <= h )
  81.         {
  82.         ListIndex i = (l +  h) >> 1;
  83.         ListIndex c = compare( keyOf( fItems[i] ), key );
  84.         if( c < 0 )
  85.             l = i + 1;
  86.         else
  87.             {
  88.             h = i - 1;
  89.             if( c == 0 )
  90.                 {
  91.                 res = True;
  92.                 if( !duplicates )
  93.                     l = i;
  94.                 }
  95.             }
  96.         }
  97.     index = l;
  98.     return res;
  99. }
  100.  
  101.  
  102. //-------------------------------------------------------------------
  103. //  KeepDuplicates
  104. void TSortedList::KeepDuplicates(Boolean dups)
  105. {
  106.    duplicates = dups;
  107. }
  108.  
  109.  
  110. //-------------------------------------------------------------------
  111. //  getDuplicateState
  112. Boolean TSortedList::getDuplicateState(void)
  113. {
  114.    return duplicates;
  115. }
  116.  
  117. //-------------------------------------------------------------------
  118. //  getClassName
  119. const char *TSortedList::getClassName(void)
  120. {
  121.    return "TSortedList";
  122. }
  123.  
  124.