home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Gnuplot / Source / DoubleValueSortedList.m < prev    next >
Encoding:
Text File  |  1993-03-18  |  2.1 KB  |  131 lines

  1.  
  2. static char RCSId[]="$Id: DoubleValueSortedList.m,v 1.1.1.1 1993/03/18 03:31:39 davis Exp $";
  3.  
  4.  
  5. #import "DoubleValueSortedList.h"
  6. #import "TicObject.h"
  7.  
  8. @implementation DoubleValueSortedList
  9.  
  10.  
  11. - init
  12. {
  13.     [super init];
  14.     isAscending = YES;
  15.  
  16.     return self;
  17. }
  18.  
  19.  
  20. - setAscending:(BOOL)aBool
  21. {
  22.     isAscending = aBool;
  23.     [self _reorderObjects];
  24.     return self;
  25. }
  26.  
  27.  
  28. - (BOOL) isAscending
  29. {
  30.     return isAscending;
  31. }
  32.  
  33.  
  34. - addObject:anObject
  35. {
  36.     if (anObject) {
  37.     double value;
  38.     int count;
  39.  
  40.     /*  
  41.      *  Get the sort value of the object and insert it at the 
  42.      *  correct place in the list.
  43.      */
  44.  
  45.     value = [anObject doubleValue];
  46.     for (count = 0 ; count < numElements ; count++) {
  47.         switch (isAscending) {
  48.         case YES:
  49.         if ([[self objectAt:count] doubleValue] > value)
  50.             return [super insertObject:anObject at:count];
  51.         break;
  52.         case NO:
  53.         if ([[self objectAt:count] doubleValue] < value)
  54.             return [super insertObject:anObject at:count];
  55.         break;
  56.         }
  57.     }
  58.  
  59.     return [super insertObject:anObject at:numElements];
  60.     }
  61.  
  62.     return nil;
  63. }
  64.  
  65.  
  66.  
  67. /** Overridden to do nothing **/
  68.  
  69. - insertObject:anObject at:(unsigned)index
  70. {
  71.     return nil;
  72. }
  73.  
  74.  
  75. - replaceObjectAt:(unsigned) index with:newObject
  76. {
  77.     return nil;
  78. }
  79.  
  80.  
  81.  
  82. /** Private methods **/
  83.  
  84. - _reorderObjects
  85. {
  86.     int count;
  87.     BOOL done = NO;
  88.  
  89.     if (!numElements || (numElements == 1))
  90.     return self;
  91.  
  92.  
  93.     /*  Do a simple bubble sort (the list is assumed to be fairly small). */
  94.  
  95.     while (!done) {
  96.     done = YES;
  97.     for (count = 0 ; count < (numElements - 1) ; count++) {
  98.         switch (isAscending) {
  99.         case YES:
  100.         if ([[self objectAt:count] doubleValue] > 
  101.             [[self objectAt:count + 1] doubleValue]) {    /* Swap */
  102.             [super insertObject:[self removeObjectAt:count]
  103.                      at:count + 1];
  104.             done = NO;
  105.         }
  106.         break;
  107.         case NO:
  108.         if ([[self objectAt:count] doubleValue] < 
  109.             [[self objectAt:count + 1] doubleValue]) {    /* Swap */
  110.             [super insertObject:[self removeObjectAt:count]
  111.                      at:count + 1];
  112.             done = NO;
  113.         }
  114.         break;
  115.         }
  116.     }
  117.     }
  118.  
  119.     return self;
  120. }
  121.  
  122.  
  123. // Shuts up the compiler about unused RCSId
  124. - (const char *) rcsid
  125. {
  126.     return RCSId;
  127. }
  128.  
  129.  
  130. @end
  131.