home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 038 / dho_9a.zip / SORTLIST.CC < prev    next >
C/C++ Source or Header  |  1994-10-12  |  3KB  |  119 lines

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