home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Database / OTC_EOFBetaExamples_V1.0 / NSFoundation / ClassCluster / RealMutableArray.m < prev   
Encoding:
Text File  |  1994-07-31  |  3.4 KB  |  175 lines

  1. /*--------------------------------------------------------------------------
  2.  *
  3.  *     You may freely copy, distribute, and reuse the code in this example.
  4.  *     SHL Systemhouse disclaims any warranty of any kind, expressed or  
  5.  *    implied, as to its fitness for any particular use.
  6.  *
  7.  *
  8.  *    RealMutableArray
  9.  *
  10.  *    Inherits From:        NSMutableArray
  11.  *
  12.  *    Conforms To:        NSCopying, NSMutableCopying, NSCoding
  13.  *
  14.  *    Declared In:        RealMutableArray.h
  15.  *
  16.  *
  17.  *------------------------------------------------------------------------*/
  18. #import "RealMutableArray.h"
  19.  
  20. #define ARRAY_DEFAULT_CAPACITY 10
  21.  
  22.  
  23.  
  24. @implementation RealMutableArray
  25.  
  26. /*--------------------------------------------------------------------------
  27.  *    Allocing, initing, and deallocing
  28.  *------------------------------------------------------------------------*/
  29. + array
  30. {
  31.     return [self arrayWithCapacity:(unsigned)ARRAY_DEFAULT_CAPACITY];
  32. }
  33.  
  34.  
  35. + arrayWithCapacity: (unsigned)aNumItems
  36. {
  37.     return [[[self alloc] initWithCapacity: aNumItems] autorelease];
  38. }
  39.  
  40.  
  41. - initWithCapacity:(unsigned)aNumItems
  42. {
  43.     unsigned iterator;
  44.  
  45.     array = [[NSMutableArray arrayWithCapacity:aNumItems] retain];
  46.     count = 0;
  47.     capacity = aNumItems;
  48.     
  49.     for (iterator = 0; iterator < aNumItems; iterator++)
  50.         [array addObject: [[Null alloc] init]];
  51.     
  52.     return self;
  53. }
  54.  
  55.  
  56. - (void) dealloc
  57. {
  58.     [array release];
  59.     return [super dealloc];
  60. }
  61.  
  62.  
  63. /*--------------------------------------------------------------------------
  64.  *    Immutable array primitives
  65.  *------------------------------------------------------------------------*/
  66. - (unsigned)count
  67. {
  68.     return [array count];
  69. }
  70.  
  71.    
  72. - objectAtIndex:(unsigned)index
  73. {
  74.     id    current;
  75.  
  76.     if (index > capacity) return nil;
  77.  
  78.     current = [array objectAtIndex: index];
  79.  
  80.     if ([current isKindOfClass: [Null class]])
  81.         return nil;
  82.     else return current;
  83. }    
  84.  
  85.  
  86. /*--------------------------------------------------------------------------
  87.  *    Mutable array primitives
  88.  *------------------------------------------------------------------------*/
  89. - (void)addObject:object
  90. {
  91.     unsigned    iterator = 0;
  92.  
  93.     if (count == capacity) {
  94.         NSLog(@"RealMutable: Array full -- cannot addObject %s",
  95.             [[object description] cString]);
  96.         return;
  97.     }
  98.  
  99.     while ([[array objectAtIndex: iterator]
  100.                 isKindOfClass: [Null class]] == NO)
  101.         iterator++;
  102.  
  103.     [array replaceObjectAtIndex:iterator withObject:object];
  104.     count++;
  105.  
  106.     return;
  107. }
  108.  
  109.  
  110. - (void)replaceObjectAtIndex:(unsigned)index withObject:object
  111. {
  112.     id    current = [array objectAtIndex:index];
  113.  
  114.     if ([current isKindOfClass: [Null class]] == YES)
  115.         count++;
  116.  
  117.     return [array replaceObjectAtIndex:index withObject:object];
  118. }
  119.  
  120.  
  121. - (void)removeLastObject
  122. {
  123.     unsigned    iterator = capacity-1;
  124.  
  125.     if (count == 0) {
  126.         NSLog(@"RemoveLastObject: RealMutableArray empty.");
  127.         return;
  128.     }
  129.  
  130.     while ([[array objectAtIndex: iterator]
  131.             isKindOfClass: [Null class]] == YES) {
  132.             iterator--;
  133.     }
  134.  
  135.     [array replaceObjectAtIndex:iterator
  136.             withObject:[[Null alloc] init]];
  137.     count--;
  138.     return;
  139. }
  140.  
  141.  
  142. - (void)insertObject:object atIndex:(unsigned)index
  143. {
  144.     id current = [array objectAtIndex: index];
  145.  
  146.     if ([current isKindOfClass: [Null class]] == YES)
  147.         count++;
  148.  
  149.     return [array replaceObjectAtIndex:index withObject:object];
  150. }
  151.  
  152.  
  153. - (void)removeObjectAtIndex:(unsigned)index
  154. {
  155.     id current = [array objectAtIndex: index];
  156.  
  157.     if ([current isKindOfClass: [Null class]] == NO) {
  158.         count--;
  159.         [array replaceObjectAtIndex:index 
  160.             withObject:[[Null alloc] init]];
  161.     }
  162.  
  163.     return;
  164. }
  165.  
  166.  
  167. @end
  168.  
  169.  
  170. @implementation Null : NSObject
  171. {
  172. }
  173.  
  174. @end
  175.