mirror of
https://github.com/gnustep/libs-base.git
synced 2025-06-01 01:01:03 +00:00
Tidied.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3217 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
81ab7668ac
commit
e0100997d5
1 changed files with 231 additions and 164 deletions
|
@ -51,7 +51,8 @@
|
|||
|
||||
+ (void) initialize
|
||||
{
|
||||
if (self == [NSGArray class]) {
|
||||
if (self == [NSGArray class])
|
||||
{
|
||||
[self setVersion: 1];
|
||||
behavior_class_add_class(self, [NSArrayNonCore class]);
|
||||
}
|
||||
|
@ -66,10 +67,12 @@
|
|||
|
||||
- (void) dealloc
|
||||
{
|
||||
if (_contents_array) {
|
||||
if (_contents_array)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < _count; i++) {
|
||||
for (i = 0; i < _count; i++)
|
||||
{
|
||||
[_contents_array[i] release];
|
||||
}
|
||||
NSZoneFree([self zone], _contents_array);
|
||||
|
@ -80,17 +83,21 @@
|
|||
/* This is the designated initializer for NSArray. */
|
||||
- (id) initWithObjects: (id*)objects count: (unsigned)count
|
||||
{
|
||||
if (count > 0) {
|
||||
if (count > 0)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
_contents_array = NSZoneMalloc([self zone], sizeof(id)*count);
|
||||
if (_contents_array == 0) {
|
||||
if (_contents_array == 0)
|
||||
{
|
||||
[self release];
|
||||
return nil;
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
if ((_contents_array[i] = [objects[i] retain]) == nil) {
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
if ((_contents_array[i] = [objects[i] retain]) == nil)
|
||||
{
|
||||
_count = i;
|
||||
[self release];
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
|
@ -106,7 +113,8 @@
|
|||
{
|
||||
[aCoder encodeValueOfObjCType: @encode(unsigned)
|
||||
at: &_count];
|
||||
if (_count > 0) {
|
||||
if (_count > 0)
|
||||
{
|
||||
[aCoder encodeArrayOfObjCType: @encode(id)
|
||||
count: _count
|
||||
at: _contents_array];
|
||||
|
@ -117,9 +125,11 @@
|
|||
{
|
||||
[aCoder decodeValueOfObjCType: @encode(unsigned)
|
||||
at: &_count];
|
||||
if (_count > 0) {
|
||||
if (_count > 0)
|
||||
{
|
||||
_contents_array = NSZoneCalloc([self zone], _count, sizeof(id));
|
||||
if (_contents_array == 0) {
|
||||
if (_contents_array == 0)
|
||||
{
|
||||
[NSException raise: NSMallocException
|
||||
format: @"Unable to make array"];
|
||||
}
|
||||
|
@ -149,11 +159,34 @@
|
|||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < _count; i++)
|
||||
{
|
||||
if ([anObject isEqual: _contents_array[i]])
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NSNotFound;
|
||||
}
|
||||
|
||||
|
@ -161,17 +194,29 @@
|
|||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < _count; i++) {
|
||||
if (anObject == _contents_array[i]) {
|
||||
for (i = 0; i < _count; i++)
|
||||
{
|
||||
if (anObject == _contents_array[i])
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return NSNotFound;
|
||||
}
|
||||
|
||||
- (id) lastObject
|
||||
{
|
||||
if (_count)
|
||||
{
|
||||
return _contents_array[_count-1];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (id) objectAtIndex: (unsigned)index
|
||||
{
|
||||
if (index >= _count) {
|
||||
if (index >= _count)
|
||||
{
|
||||
[NSException raise: NSRangeException
|
||||
format: @"Index out of bounds"];
|
||||
}
|
||||
|
@ -183,7 +228,8 @@
|
|||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < _count; i++) {
|
||||
for (i = 0; i < _count; i++)
|
||||
{
|
||||
aBuffer[i] = _contents_array[i];
|
||||
}
|
||||
}
|
||||
|
@ -192,10 +238,12 @@
|
|||
{
|
||||
unsigned i, j = 0, e = aRange.location + aRange.length;
|
||||
|
||||
if (_count < e) {
|
||||
if (_count < e)
|
||||
{
|
||||
e = _count;
|
||||
}
|
||||
for (i = aRange.location; i < e; i++) {
|
||||
for (i = aRange.location; i < e; i++)
|
||||
{
|
||||
aBuffer[j++] = _contents_array[i];
|
||||
}
|
||||
}
|
||||
|
@ -208,7 +256,8 @@
|
|||
|
||||
+ (void) initialize
|
||||
{
|
||||
if (self == [NSGMutableArray class]) {
|
||||
if (self == [NSGMutableArray class])
|
||||
{
|
||||
[self setVersion: 1];
|
||||
behavior_class_add_class(self, [NSMutableArrayNonCore class]);
|
||||
behavior_class_add_class(self, [NSGArray class]);
|
||||
|
@ -217,7 +266,8 @@
|
|||
|
||||
- (id) initWithCapacity: (unsigned)cap
|
||||
{
|
||||
if (cap == 0) {
|
||||
if (cap == 0)
|
||||
{
|
||||
cap = 1;
|
||||
}
|
||||
_contents_array = NSZoneMalloc([self zone], sizeof(id)*cap);
|
||||
|
@ -232,11 +282,13 @@
|
|||
|
||||
[aCoder decodeValueOfObjCType: @encode(unsigned)
|
||||
at: &count];
|
||||
if ([self initWithCapacity: count] == nil) {
|
||||
if ([self initWithCapacity: count] == nil)
|
||||
{
|
||||
[NSException raise: NSMallocException
|
||||
format: @"Unable to make array"];
|
||||
}
|
||||
if (count > 0) {
|
||||
if (count > 0)
|
||||
{
|
||||
[aCoder decodeArrayOfObjCType: @encode(id)
|
||||
count: count
|
||||
at: _contents_array];
|
||||
|
@ -248,11 +300,14 @@
|
|||
- (id) initWithObjects: (id*)objects count: (unsigned)count
|
||||
{
|
||||
self = [self initWithCapacity: count];
|
||||
if (self != nil && count > 0) {
|
||||
if (self != nil && count > 0)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
if ((_contents_array[i] = [objects[i] retain]) == nil) {
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
if ((_contents_array[i] = [objects[i] retain]) == nil)
|
||||
{
|
||||
_count = i;
|
||||
[self release];
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
|
@ -268,20 +323,24 @@
|
|||
{
|
||||
unsigned i;
|
||||
|
||||
if (!anObject) {
|
||||
if (!anObject)
|
||||
{
|
||||
[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];
|
||||
}
|
||||
if (_count == _capacity) {
|
||||
if (_count == _capacity)
|
||||
{
|
||||
id *ptr;
|
||||
size_t size = (_capacity + _grow_factor)*sizeof(id);
|
||||
|
||||
ptr = NSZoneRealloc([self zone], _contents_array, size);
|
||||
if (ptr == 0) {
|
||||
if (ptr == 0)
|
||||
{
|
||||
[NSException raise: NSMallocException
|
||||
format: @"Unable to grow"];
|
||||
}
|
||||
|
@ -289,7 +348,8 @@
|
|||
_capacity += _grow_factor;
|
||||
_grow_factor = _capacity/2;
|
||||
}
|
||||
for (i = _count; i > index; i--) {
|
||||
for (i = _count; i > index; i--)
|
||||
{
|
||||
_contents_array[i] = _contents_array[i - 1];
|
||||
}
|
||||
/*
|
||||
|
@ -304,16 +364,19 @@
|
|||
|
||||
- (void) addObject: (id)anObject
|
||||
{
|
||||
if (anObject == nil) {
|
||||
if (anObject == 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);
|
||||
|
||||
ptr = NSZoneRealloc([self zone], _contents_array, size);
|
||||
if (ptr == 0) {
|
||||
if (ptr == 0)
|
||||
{
|
||||
[NSException raise: NSMallocException
|
||||
format: @"Unable to grow"];
|
||||
}
|
||||
|
@ -327,7 +390,8 @@
|
|||
|
||||
- (void) removeLastObject
|
||||
{
|
||||
if (_count == 0) {
|
||||
if (_count == 0)
|
||||
{
|
||||
[NSException raise: NSRangeException
|
||||
format: @"Trying to remove from an empty array."];
|
||||
}
|
||||
|
@ -339,13 +403,16 @@
|
|||
{
|
||||
id obj;
|
||||
|
||||
if (index >= _count) {
|
||||
[NSException raise: NSRangeException format:
|
||||
@"in removeObjectAtIndex:, index %d is out of range", index];
|
||||
if (index >= _count)
|
||||
{
|
||||
[NSException raise: NSRangeException
|
||||
format: @"in removeObjectAtIndex:, index %d is out of range",
|
||||
index];
|
||||
}
|
||||
obj = _contents_array[index];
|
||||
_count--;
|
||||
while (index < _count) {
|
||||
while (index < _count)
|
||||
{
|
||||
_contents_array[index] = _contents_array[index+1];
|
||||
index++;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue