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

@ -51,7 +51,8 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGArray class]) { if (self == [NSGArray class])
{
[self setVersion: 1]; [self setVersion: 1];
behavior_class_add_class(self, [NSArrayNonCore class]); behavior_class_add_class(self, [NSArrayNonCore class]);
} }
@ -66,10 +67,12 @@
- (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);
@ -80,17 +83,21 @@
/* 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]; [self release];
return nil; return nil;
} }
for (i = 0; i < count; i++) { for (i = 0; i < count; i++)
if ((_contents_array[i] = [objects[i] retain]) == nil) { {
if ((_contents_array[i] = [objects[i] retain]) == nil)
{
_count = i; _count = i;
[self release]; [self release];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
@ -106,7 +113,8 @@
{ {
[aCoder encodeValueOfObjCType: @encode(unsigned) [aCoder encodeValueOfObjCType: @encode(unsigned)
at: &_count]; at: &_count];
if (_count > 0) { if (_count > 0)
{
[aCoder encodeArrayOfObjCType: @encode(id) [aCoder encodeArrayOfObjCType: @encode(id)
count: _count count: _count
at: _contents_array]; at: _contents_array];
@ -117,9 +125,11 @@
{ {
[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)); _contents_array = NSZoneCalloc([self zone], _count, sizeof(id));
if (_contents_array == 0) { if (_contents_array == 0)
{
[NSException raise: NSMallocException [NSException raise: NSMallocException
format: @"Unable to make array"]; format: @"Unable to make array"];
} }
@ -149,11 +159,34 @@
{ {
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.
*/
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 i;
} }
} }
}
else
{
for (i = 0; i < _count; i++)
{
if ([anObject isEqual: _contents_array[i]])
{
return i;
}
}
}
return NSNotFound; return NSNotFound;
} }
@ -161,17 +194,29 @@
{ {
unsigned i; unsigned i;
for (i = 0; i < _count; i++) { for (i = 0; i < _count; i++)
if (anObject == _contents_array[i]) { {
if (anObject == _contents_array[i])
{
return 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 [NSException raise: NSRangeException
format: @"Index out of bounds"]; format: @"Index out of bounds"];
} }
@ -183,7 +228,8 @@
{ {
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];
} }
} }
@ -192,10 +238,12 @@
{ {
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,7 +256,8 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGMutableArray class]) { if (self == [NSGMutableArray class])
{
[self setVersion: 1]; [self setVersion: 1];
behavior_class_add_class(self, [NSMutableArrayNonCore class]); behavior_class_add_class(self, [NSMutableArrayNonCore class]);
behavior_class_add_class(self, [NSGArray class]); behavior_class_add_class(self, [NSGArray class]);
@ -217,7 +266,8 @@
- (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);
@ -232,11 +282,13 @@
[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 [NSException raise: NSMallocException
format: @"Unable to make array"]; format: @"Unable to make array"];
} }
if (count > 0) { if (count > 0)
{
[aCoder decodeArrayOfObjCType: @encode(id) [aCoder decodeArrayOfObjCType: @encode(id)
count: count count: count
at: _contents_array]; at: _contents_array];
@ -248,11 +300,14 @@
- (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) { {
if ((_contents_array[i] = [objects[i] retain]) == nil)
{
_count = i; _count = i;
[self release]; [self release];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
@ -268,20 +323,24 @@
{ {
unsigned i; unsigned i;
if (!anObject) { if (!anObject)
{
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to insert nil"]; format: @"Tried to insert nil"];
} }
if (index > _count) { if (index > _count)
{
[NSException raise: NSRangeException format: [NSException raise: NSRangeException format:
@"in insertObject:atIndex:, index %d is out of range", index]; @"in insertObject:atIndex:, index %d is out of range", index];
} }
if (_count == _capacity) { if (_count == _capacity)
{
id *ptr; id *ptr;
size_t size = (_capacity + _grow_factor)*sizeof(id); 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 [NSException raise: NSMallocException
format: @"Unable to grow"]; format: @"Unable to grow"];
} }
@ -289,7 +348,8 @@
_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];
} }
/* /*
@ -304,16 +364,19 @@
- (void) addObject: (id)anObject - (void) addObject: (id)anObject
{ {
if (anObject == nil) { if (anObject == nil)
{
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to add nil"]; format: @"Tried to add nil"];
} }
if (_count >= _capacity) { if (_count >= _capacity)
{
id *ptr; id *ptr;
size_t size = (_capacity + _grow_factor)*sizeof(id); 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 [NSException raise: NSMallocException
format: @"Unable to grow"]; format: @"Unable to grow"];
} }
@ -327,7 +390,8 @@
- (void) removeLastObject - (void) removeLastObject
{ {
if (_count == 0) { if (_count == 0)
{
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"Trying to remove from an empty array."]; format: @"Trying to remove from an empty array."];
} }
@ -339,13 +403,16 @@
{ {
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]; _contents_array[index] = _contents_array[index+1];
index++; index++;
} }