home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Database / OTC_EOFBetaExamples_V1.0 / NSFoundation / ClassCluster / BetterRealMutableArray.m < prev    next >
Encoding:
Text File  |  1994-07-31  |  2.8 KB  |  154 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.  *    BetterRealMutableArray
  9.  *
  10.  *    Inherits From:        NSMutableArray
  11.  *
  12.  *    Conforms To:        NSCopying, NSMutableCopying, NSCoding
  13.  *
  14.  *    Declared In:        BetterRealMutableArray.h
  15.  *
  16.  *
  17.  *------------------------------------------------------------------------*/
  18. #import "BetterRealMutableArray.h"
  19. #import <appkit/appkit.h>
  20.  
  21. #define ARRAY_DEFAULT_CAPACITY 10
  22.  
  23.  
  24.  
  25.  
  26. @implementation BetterRealMutableArray
  27.  
  28. /*--------------------------------------------------------------------------
  29.  *    Allocing, initing, and deallocing
  30.  *------------------------------------------------------------------------*/
  31. + arrayWithCapacity: (unsigned)aNumItems
  32. {
  33.     return [[[self alloc] initWithCapacity: aNumItems] autorelease];
  34. }
  35.  
  36.  
  37. - initWithCapacity:(unsigned)aNumItems
  38. {
  39.     unsigned iterator;
  40.  
  41.     array = NX_MALLOC(array, id, aNumItems);
  42.     count = 0;
  43.     capacity = aNumItems;
  44.     
  45.     for (iterator = 0; iterator < aNumItems; iterator++)
  46.         array[iterator] = nil;
  47.     
  48.     return self;
  49. }
  50.  
  51.  
  52. - (void) dealloc
  53. {
  54.     NX_FREE(array);
  55.     return [super dealloc];
  56. }
  57.  
  58.  
  59. /*--------------------------------------------------------------------------
  60.  *    Immutable array primitives
  61.  *------------------------------------------------------------------------*/
  62. - (unsigned)count
  63. {
  64.     return capacity;
  65. }
  66.  
  67.    
  68. - objectAtIndex:(unsigned)index
  69. {
  70.     return array[index];
  71. }    
  72.  
  73.  
  74. /*--------------------------------------------------------------------------
  75.  *    Mutable array primitives
  76.  *------------------------------------------------------------------------*/
  77. - (void)addObject:object
  78. {
  79.     int    iterator = 0;
  80.  
  81.     if (count == capacity) {
  82.         NSLog(@"BetterRealMutable: Array full -- cannot addObject %s",
  83.             [[(NSObject *)object description] cString]);
  84.         return;
  85.     }
  86.  
  87.     while (array[iterator] != nil) iterator++;
  88.  
  89.     array[iterator] = [object retain];
  90.     count++;
  91.  
  92.     return;
  93. }
  94.  
  95.  
  96. - (void)replaceObjectAtIndex:(unsigned)index withObject:object
  97. {
  98.     if (index >= capacity) 
  99.         return;
  100.  
  101.     if (array[index] == nil) count++;
  102.     else [array[index] release];
  103.  
  104.     array[index] = [object retain];
  105.  
  106.     return;
  107. }
  108.  
  109.  
  110. - (void)removeLastObject
  111. {
  112.     unsigned    iterator = capacity-1;
  113.  
  114.     if (count == 0) return;
  115.  
  116.  
  117.     while (array[iterator] == nil) iterator--;
  118.  
  119.     [array[iterator] release];
  120.     array[iterator] = nil;
  121.     count--;
  122.  
  123.     return;
  124. }
  125.  
  126.  
  127. - (void)insertObject:object atIndex:(unsigned)index
  128. {
  129.     if (index >= capacity) return;
  130.  
  131.     if (array[index] == nil) count++;
  132.     else [array[index] release];
  133.  
  134.     array[index] = [object retain];
  135.     return;
  136. }
  137.  
  138.  
  139. - (void)removeObjectAtIndex:(unsigned)index
  140. {
  141.     if (index >= capacity) return;
  142.  
  143.     if (array[index] != nil) count--;
  144.  
  145.     [array[index] release];
  146.     array[index] = nil;
  147.  
  148.     return;
  149. }
  150.  
  151.  
  152. @end
  153.  
  154.