git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3217 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1998-11-14 03:34:59 +00:00
parent 81ab7668ac
commit e0100997d5

View file

@ -31,17 +31,17 @@
@interface NSGArray : NSArray @interface NSGArray : NSArray
{ {
id *_contents_array; id *_contents_array;
unsigned _count; unsigned _count;
} }
@end @end
@interface NSGMutableArray : NSMutableArray @interface NSGMutableArray : NSMutableArray
{ {
id *_contents_array; id *_contents_array;
unsigned _count; unsigned _count;
unsigned _capacity; unsigned _capacity;
int _grow_factor; int _grow_factor;
} }
@end @end
@ -51,152 +51,200 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGArray class]) { if (self == [NSGArray class])
[self setVersion: 1]; {
behavior_class_add_class(self, [NSArrayNonCore class]); [self setVersion: 1];
behavior_class_add_class(self, [NSArrayNonCore class]);
} }
} }
+ (id) allocWithZone: (NSZone*)zone + (id) allocWithZone: (NSZone*)zone
{ {
NSGArray *array = NSAllocateObject(self, 0, zone); NSGArray *array = NSAllocateObject(self, 0, zone);
return array; return array;
} }
- (void) dealloc - (void) dealloc
{ {
if (_contents_array) { if (_contents_array)
unsigned i; {
unsigned i;
for (i = 0; i < _count; i++) { for (i = 0; i < _count; i++)
[_contents_array[i] release]; {
[_contents_array[i] release];
} }
NSZoneFree([self zone], _contents_array); NSZoneFree([self zone], _contents_array);
} }
[super dealloc]; [super dealloc];
} }
/* This is the designated initializer for NSArray. */ /* This is the designated initializer for NSArray. */
- (id) initWithObjects: (id*)objects count: (unsigned)count - (id) initWithObjects: (id*)objects count: (unsigned)count
{ {
if (count > 0) { if (count > 0)
unsigned i; {
unsigned i;
_contents_array = NSZoneMalloc([self zone], sizeof(id)*count); _contents_array = NSZoneMalloc([self zone], sizeof(id)*count);
if (_contents_array == 0) { if (_contents_array == 0)
[self release]; {
return nil; [self release];
} return nil;
}
for (i = 0; i < count; i++) { for (i = 0; i < count; i++)
if ((_contents_array[i] = [objects[i] retain]) == nil) { {
_count = i; if ((_contents_array[i] = [objects[i] retain]) == nil)
[self release]; {
[NSException raise: NSInvalidArgumentException _count = i;
format: @"Tried to add nil"]; [self release];
[NSException raise: NSInvalidArgumentException
format: @"Tried to add nil"];
} }
} }
_count = count; _count = count;
} }
return self; return self;
} }
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
[aCoder encodeValueOfObjCType: @encode(unsigned) [aCoder encodeValueOfObjCType: @encode(unsigned)
at: &_count]; at: &_count];
if (_count > 0) { if (_count > 0)
[aCoder encodeArrayOfObjCType: @encode(id) {
count: _count [aCoder encodeArrayOfObjCType: @encode(id)
at: _contents_array]; count: _count
at: _contents_array];
} }
} }
- (id) initWithCoder: (NSCoder*)aCoder - (id) initWithCoder: (NSCoder*)aCoder
{ {
[aCoder decodeValueOfObjCType: @encode(unsigned) [aCoder decodeValueOfObjCType: @encode(unsigned)
at: &_count]; at: &_count];
if (_count > 0) { if (_count > 0)
_contents_array = NSZoneCalloc([self zone], _count, sizeof(id)); {
if (_contents_array == 0) { _contents_array = NSZoneCalloc([self zone], _count, sizeof(id));
[NSException raise: NSMallocException if (_contents_array == 0)
format: @"Unable to make array"]; {
[NSException raise: NSMallocException
format: @"Unable to make array"];
} }
[aCoder decodeArrayOfObjCType: @encode(id) [aCoder decodeArrayOfObjCType: @encode(id)
count: _count count: _count
at: _contents_array]; at: _contents_array];
} }
return self; return self;
} }
- (id) init - (id) init
{ {
return [self initWithObjects: 0 count: 0]; return [self initWithObjects: 0 count: 0];
} }
- (unsigned) count - (unsigned) count
{ {
return _count; return _count;
} }
- (unsigned) hash - (unsigned) hash
{ {
return _count; return _count;
} }
- (unsigned) indexOfObject: anObject - (unsigned) indexOfObject: anObject
{ {
unsigned i; unsigned i;
for (i = 0; i < _count; i++) { /*
if ([_contents_array[i] isEqual: anObject]) { * For large arrays, speed things up a little by caching the method.
return i; */
if (_count > 8)
{
SEL sel = @selector(isEqual:);
BOOL (*imp)(id,SEL,id);
imp = (BOOL (*)(id,SEL,id))[anObject methodForSelector: sel];
for (i = 0; i < _count; i++)
{
if ((*imp)(anObject, sel, _contents_array[i]))
{
return i;
}
} }
} }
return NSNotFound; else
{
for (i = 0; i < _count; i++)
{
if ([anObject isEqual: _contents_array[i]])
{
return i;
}
}
}
return NSNotFound;
} }
- (unsigned) indexOfObjectIdenticalTo: anObject - (unsigned) indexOfObjectIdenticalTo: anObject
{ {
unsigned i; unsigned i;
for (i = 0; i < _count; i++) { for (i = 0; i < _count; i++)
if (anObject == _contents_array[i]) { {
return i; if (anObject == _contents_array[i])
{
return i;
} }
} }
return NSNotFound; return NSNotFound;
}
- (id) lastObject
{
if (_count)
{
return _contents_array[_count-1];
}
return nil;
} }
- (id) objectAtIndex: (unsigned)index - (id) objectAtIndex: (unsigned)index
{ {
if (index >= _count) { if (index >= _count)
[NSException raise: NSRangeException {
format: @"Index out of bounds"]; [NSException raise: NSRangeException
format: @"Index out of bounds"];
} }
return _contents_array[index]; return _contents_array[index];
} }
- (void) getObjects: (id*)aBuffer - (void) getObjects: (id*)aBuffer
{ {
unsigned i; unsigned i;
for (i = 0; i < _count; i++) { for (i = 0; i < _count; i++)
aBuffer[i] = _contents_array[i]; {
aBuffer[i] = _contents_array[i];
} }
} }
- (void) getObjects: (id*)aBuffer range: (NSRange)aRange - (void) getObjects: (id*)aBuffer range: (NSRange)aRange
{ {
unsigned i, j = 0, e = aRange.location + aRange.length; unsigned i, j = 0, e = aRange.location + aRange.length;
if (_count < e) { if (_count < e)
e = _count; {
e = _count;
} }
for (i = aRange.location; i < e; i++) { for (i = aRange.location; i < e; i++)
aBuffer[j++] = _contents_array[i]; {
aBuffer[j++] = _contents_array[i];
} }
} }
@ -208,148 +256,167 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGMutableArray class]) { if (self == [NSGMutableArray class])
[self setVersion: 1]; {
behavior_class_add_class(self, [NSMutableArrayNonCore class]); [self setVersion: 1];
behavior_class_add_class(self, [NSGArray class]); behavior_class_add_class(self, [NSMutableArrayNonCore class]);
behavior_class_add_class(self, [NSGArray class]);
} }
} }
- (id) initWithCapacity: (unsigned)cap - (id) initWithCapacity: (unsigned)cap
{ {
if (cap == 0) { if (cap == 0)
cap = 1; {
cap = 1;
} }
_contents_array = NSZoneMalloc([self zone], sizeof(id)*cap); _contents_array = NSZoneMalloc([self zone], sizeof(id)*cap);
_capacity = cap; _capacity = cap;
_grow_factor = cap > 1 ? cap/2 : 1; _grow_factor = cap > 1 ? cap/2 : 1;
return self; return self;
} }
- (id) initWithCoder: (NSCoder*)aCoder - (id) initWithCoder: (NSCoder*)aCoder
{ {
unsigned count; unsigned count;
[aCoder decodeValueOfObjCType: @encode(unsigned) [aCoder decodeValueOfObjCType: @encode(unsigned)
at: &count]; at: &count];
if ([self initWithCapacity: count] == nil) { if ([self initWithCapacity: count] == nil)
[NSException raise: NSMallocException {
format: @"Unable to make array"]; [NSException raise: NSMallocException
format: @"Unable to make array"];
} }
if (count > 0) { if (count > 0)
[aCoder decodeArrayOfObjCType: @encode(id) {
count: count [aCoder decodeArrayOfObjCType: @encode(id)
at: _contents_array]; count: count
_count = count; at: _contents_array];
_count = count;
} }
return self; return self;
} }
- (id) initWithObjects: (id*)objects count: (unsigned)count - (id) initWithObjects: (id*)objects count: (unsigned)count
{ {
self = [self initWithCapacity: count]; self = [self initWithCapacity: count];
if (self != nil && count > 0) { if (self != nil && count > 0)
unsigned i; {
unsigned i;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++)
if ((_contents_array[i] = [objects[i] retain]) == nil) { {
_count = i; if ((_contents_array[i] = [objects[i] retain]) == nil)
[self release]; {
[NSException raise: NSInvalidArgumentException _count = i;
format: @"Tried to add nil"]; [self release];
[NSException raise: NSInvalidArgumentException
format: @"Tried to add nil"];
} }
} }
_count = count; _count = count;
} }
return self; return self;
} }
- (void) insertObject: (id)anObject atIndex: (unsigned)index - (void) insertObject: (id)anObject atIndex: (unsigned)index
{ {
unsigned i; unsigned i;
if (!anObject) { if (!anObject)
[NSException raise: NSInvalidArgumentException {
format: @"Tried to insert nil"]; [NSException raise: NSInvalidArgumentException
format: @"Tried to insert nil"];
} }
if (index > _count) { if (index > _count)
[NSException raise: NSRangeException format: {
@"in insertObject:atIndex:, index %d is out of range", index]; [NSException raise: NSRangeException format:
@"in insertObject:atIndex:, index %d is out of range", index];
} }
if (_count == _capacity) { if (_count == _capacity)
id *ptr; {
size_t size = (_capacity + _grow_factor)*sizeof(id); id *ptr;
size_t size = (_capacity + _grow_factor)*sizeof(id);
ptr = NSZoneRealloc([self zone], _contents_array, size); ptr = NSZoneRealloc([self zone], _contents_array, size);
if (ptr == 0) { if (ptr == 0)
[NSException raise: NSMallocException {
format: @"Unable to grow"]; [NSException raise: NSMallocException
format: @"Unable to grow"];
} }
_contents_array = ptr; _contents_array = ptr;
_capacity += _grow_factor; _capacity += _grow_factor;
_grow_factor = _capacity/2; _grow_factor = _capacity/2;
} }
for (i = _count; i > index; i--) { for (i = _count; i > index; i--)
_contents_array[i] = _contents_array[i - 1]; {
_contents_array[i] = _contents_array[i - 1];
} }
/* /*
* Make sure the array is 'sane' so that it can be deallocated * Make sure the array is 'sane' so that it can be deallocated
* safely by an autorelease pool if the '[anObject retain]' causes * safely by an autorelease pool if the '[anObject retain]' causes
* an exception. * an exception.
*/ */
_contents_array[index] = nil; _contents_array[index] = nil;
_count++; _count++;
_contents_array[index] = [anObject retain]; _contents_array[index] = [anObject retain];
} }
- (void) addObject: (id)anObject - (void) addObject: (id)anObject
{ {
if (anObject == nil) { if (anObject == nil)
[NSException raise: NSInvalidArgumentException {
format: @"Tried to add nil"]; [NSException raise: NSInvalidArgumentException
format: @"Tried to add nil"];
} }
if (_count >= _capacity) { if (_count >= _capacity)
id *ptr; {
size_t size = (_capacity + _grow_factor)*sizeof(id); id *ptr;
size_t size = (_capacity + _grow_factor)*sizeof(id);
ptr = NSZoneRealloc([self zone], _contents_array, size); ptr = NSZoneRealloc([self zone], _contents_array, size);
if (ptr == 0) { if (ptr == 0)
[NSException raise: NSMallocException {
format: @"Unable to grow"]; [NSException raise: NSMallocException
format: @"Unable to grow"];
} }
_contents_array = ptr; _contents_array = ptr;
_capacity += _grow_factor; _capacity += _grow_factor;
_grow_factor = _capacity/2; _grow_factor = _capacity/2;
} }
_contents_array[_count] = [anObject retain]; _contents_array[_count] = [anObject retain];
_count++; /* Do this AFTER we have retained the object. */ _count++; /* Do this AFTER we have retained the object. */
} }
- (void) removeLastObject - (void) removeLastObject
{ {
if (_count == 0) { if (_count == 0)
[NSException raise: NSRangeException {
format: @"Trying to remove from an empty array."]; [NSException raise: NSRangeException
format: @"Trying to remove from an empty array."];
} }
_count--; _count--;
[_contents_array[_count] release]; [_contents_array[_count] release];
} }
- (void) removeObjectAtIndex: (unsigned)index - (void) removeObjectAtIndex: (unsigned)index
{ {
id obj; id obj;
if (index >= _count) { if (index >= _count)
[NSException raise: NSRangeException format: {
@"in removeObjectAtIndex:, index %d is out of range", index]; [NSException raise: NSRangeException
format: @"in removeObjectAtIndex:, index %d is out of range",
index];
} }
obj = _contents_array[index]; obj = _contents_array[index];
_count--; _count--;
while (index < _count) { while (index < _count)
_contents_array[index] = _contents_array[index+1]; {
index++; _contents_array[index] = _contents_array[index+1];
index++;
} }
[obj release]; /* Adjust array BEFORE releasing object. */ [obj release]; /* Adjust array BEFORE releasing object. */
} }
- (void) replaceObjectAtIndex: (unsigned)index withObject: (id)anObject - (void) replaceObjectAtIndex: (unsigned)index withObject: (id)anObject