Array tidyup/optimisation

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@8509 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 2001-01-08 16:45:36 +00:00
parent d2ac6b1b5b
commit 0ab12ac694
8 changed files with 450 additions and 324 deletions

View file

@ -1,3 +1,15 @@
2001-01-08 Richard Frith-Macdonald <rfm@gnu.org>
* Headers/Foundation/NSGArray.h: removed
* Headers/Foundation/NSArray.h: merge core/non-core classes.
* Source/GNUmakefile: replace NSGArray with GSArray.
* Source/NSGArray.m: converted to GSArray.m
* Source/NSArray.m: simplified by merging core/non-core classes.
Use placeholder class when allocating constant arrays.
* Source/GSArray.m: simplified version of GSArray.m
Added GSPlaceholderArray.
* Source/NSSerializer.m: use GSInlineArray where possible.
2001-01-04 Richard Frith-Macdonald <rfm@gnu.org> 2001-01-04 Richard Frith-Macdonald <rfm@gnu.org>
* Tools/gsdoc.gsdoc: Tidied and fixed a few typos/spelling errors. * Tools/gsdoc.gsdoc: Tidied and fixed a few typos/spelling errors.

View file

@ -30,13 +30,6 @@
@class NSString; @class NSString;
@interface NSArray : NSObject <NSCoding, NSCopying, NSMutableCopying> @interface NSArray : NSObject <NSCoding, NSCopying, NSMutableCopying>
- (id) initWithObjects: (id*) objects count: (unsigned) count;
- (unsigned) count;
- (id) objectAtIndex: (unsigned)index;
@end
@interface NSArray (NonCore)
+ (id) array; + (id) array;
+ (id) arrayWithArray: (NSArray*)array; + (id) arrayWithArray: (NSArray*)array;
@ -44,21 +37,24 @@
+ (id) arrayWithObject: (id)anObject; + (id) arrayWithObject: (id)anObject;
+ (id) arrayWithObjects: (id)firstObj, ...; + (id) arrayWithObjects: (id)firstObj, ...;
+ (id) arrayWithObjects: (id*)objects count: (unsigned)count; + (id) arrayWithObjects: (id*)objects count: (unsigned)count;
- (NSArray*) arrayByAddingObject: (id)anObject; - (NSArray*) arrayByAddingObject: (id)anObject;
- (NSArray*) arrayByAddingObjectsFromArray: (NSArray*)anotherArray; - (NSArray*) arrayByAddingObjectsFromArray: (NSArray*)anotherArray;
- (id) initWithArray: (NSArray*)array;
- (id) initWithContentsOfFile: (NSString*)file;
- (id) initWithObjects: firstObj, ...;
- (id) initWithObjects: (id*)objects count: (unsigned)count;
- (BOOL) containsObject: anObject; - (BOOL) containsObject: anObject;
- (unsigned) count; // Primitive
- (void) getObjects: (id*)objs; - (void) getObjects: (id*)objs;
- (void) getObjects: (id*)objs range: (NSRange)aRange; - (void) getObjects: (id*)objs range: (NSRange)aRange;
- (unsigned) indexOfObject: (id)anObject; - (unsigned) indexOfObject: (id)anObject;
- (unsigned) indexOfObject: (id)anObject inRange: (NSRange)aRange; - (unsigned) indexOfObject: (id)anObject inRange: (NSRange)aRange;
- (unsigned) indexOfObjectIdenticalTo: (id)anObject; - (unsigned) indexOfObjectIdenticalTo: (id)anObject;
- (unsigned) indexOfObjectIdenticalTo: (id)anObject inRange: (NSRange)aRange; - (unsigned) indexOfObjectIdenticalTo: (id)anObject inRange: (NSRange)aRange;
- (id) initWithArray: (NSArray*)array;
- (id) initWithContentsOfFile: (NSString*)file;
- (id) initWithObjects: firstObj, ...;
- (id) initWithObjects: (id*)objects count: (unsigned)count; // Primitive
- (id) lastObject; - (id) lastObject;
- (id) objectAtIndex: (unsigned)index; // Primitive
- (id) firstObjectCommonWithArray: (NSArray*)otherArray; - (id) firstObjectCommonWithArray: (NSArray*)otherArray;
- (BOOL) isEqualToArray: (NSArray*)otherArray; - (BOOL) isEqualToArray: (NSArray*)otherArray;
@ -98,19 +94,16 @@
@interface NSMutableArray : NSArray @interface NSMutableArray : NSArray
- (id) initWithCapacity: (unsigned)numItems;
- (void) addObject: (id)anObject;
- (void) replaceObjectAtIndex: (unsigned)index withObject: (id)anObject;
- (void) insertObject: (id)anObject atIndex: (unsigned)index;
- (void) removeObjectAtIndex: (unsigned)index;
@end
@interface NSMutableArray (NonCore)
+ (id) arrayWithCapacity: (unsigned)numItems; + (id) arrayWithCapacity: (unsigned)numItems;
+ (id) initWithCapacity: (unsigned)numItems;
- (void) addObject: (id)anObject; // Primitive
- (void) addObjectsFromArray: (NSArray*)otherArray; - (void) addObjectsFromArray: (NSArray*)otherArray;
- (id) initWithCapacity: (unsigned)numItems; // Primitive
- (void) insertObject: (id)anObject atIndex: (unsigned)index; // Primitive
- (void) removeObjectAtIndex: (unsigned)index; // Primitive
- (void) replaceObjectAtIndex: (unsigned)index
withObject: (id)anObject; // Primitive
- (void) replaceObjectsInRange: (NSRange)aRange - (void) replaceObjectsInRange: (NSRange)aRange
withObjectsFromArray: (NSArray*)anArray; withObjectsFromArray: (NSArray*)anArray;
- (void) replaceObjectsInRange: (NSRange)aRange - (void) replaceObjectsInRange: (NSRange)aRange

View file

@ -144,6 +144,7 @@ preface.h
# GNUStep source files # GNUStep source files
BASE_MFILES = \ BASE_MFILES = \
GSArray.m \
GSCountedSet.m \ GSCountedSet.m \
GSHTTPURLHandle.m \ GSHTTPURLHandle.m \
GSMime.m \ GSMime.m \
@ -181,7 +182,6 @@ NSFileHandle.m \
NSFileManager.m \ NSFileManager.m \
NSFormatter.m \ NSFormatter.m \
NSGeometry.m \ NSGeometry.m \
NSGArray.m \
NSGAttributedString.m \ NSGAttributedString.m \
NSGDictionary.m \ NSGDictionary.m \
NSHashTable.m \ NSHashTable.m \
@ -296,7 +296,6 @@ NSFileHandle.h \
NSFileManager.h \ NSFileManager.h \
NSFormatter.h \ NSFormatter.h \
NSGeometry.h \ NSGeometry.h \
NSGArray.h \
NSGAttributedString.h \ NSGAttributedString.h \
NSHashTable.h \ NSHashTable.h \
NSHost.h \ NSHost.h \

View file

@ -32,10 +32,10 @@
static SEL eqSel; static SEL eqSel;
@class NSGArrayEnumerator; @class GSArrayEnumerator;
@class NSGArrayEnumeratorReverse; @class GSArrayEnumeratorReverse;
@interface NSGArray : NSArray @interface GSArray : NSArray
{ {
@public @public
id *_contents_array; id *_contents_array;
@ -43,12 +43,12 @@ static SEL eqSel;
} }
@end @end
@interface NSGInlineArray : NSGArray @interface GSInlineArray : GSArray
{ {
} }
@end @end
@interface NSGMutableArray : NSMutableArray @interface GSMutableArray : NSMutableArray
{ {
@public @public
id *_contents_array; id *_contents_array;
@ -58,23 +58,25 @@ static SEL eqSel;
} }
@end @end
@class NSArrayNonCore; @interface GSPlaceholderArray : NSArray
{
}
@end
@implementation NSGArray @implementation GSArray
+ (void) initialize + (void) initialize
{ {
if (self == [NSGArray class]) if (self == [GSArray class])
{ {
[self setVersion: 1]; [self setVersion: 1];
eqSel = @selector(isEqual:); eqSel = @selector(isEqual:);
behavior_class_add_class(self, [NSArrayNonCore class]);
} }
} }
+ (id) allocWithZone: (NSZone*)zone + (id) allocWithZone: (NSZone*)zone
{ {
NSGArray *array = NSAllocateObject(self, 0, zone); GSArray *array = NSAllocateObject(self, 0, zone);
return array; return array;
} }
@ -277,7 +279,7 @@ static SEL eqSel;
@end @end
@implementation NSGInlineArray @implementation GSInlineArray
- (void) dealloc - (void) dealloc
{ {
if (_contents_array) if (_contents_array)
@ -316,17 +318,14 @@ static SEL eqSel;
} }
@end @end
@class NSMutableArrayNonCore; @implementation GSMutableArray
@implementation NSGMutableArray
+ (void) initialize + (void) initialize
{ {
if (self == [NSGMutableArray class]) if (self == [GSMutableArray class])
{ {
[self setVersion: 1]; [self setVersion: 1];
behavior_class_add_class(self, [NSMutableArrayNonCore class]); behavior_class_add_class(self, [GSArray class]);
behavior_class_add_class(self, [NSGArray class]);
} }
} }
@ -661,13 +660,13 @@ static SEL eqSel;
- (NSEnumerator*) objectEnumerator - (NSEnumerator*) objectEnumerator
{ {
return AUTORELEASE([[NSGArrayEnumerator allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[GSArrayEnumerator allocWithZone: NSDefaultMallocZone()]
initWithArray: self]); initWithArray: self]);
} }
- (NSEnumerator*) reverseObjectEnumerator - (NSEnumerator*) reverseObjectEnumerator
{ {
return AUTORELEASE([[NSGArrayEnumeratorReverse allocWithZone: return AUTORELEASE([[GSArrayEnumeratorReverse allocWithZone:
NSDefaultMallocZone()] initWithArray: self]); NSDefaultMallocZone()] initWithArray: self]);
} }
@ -675,17 +674,17 @@ static SEL eqSel;
@interface NSGArrayEnumerator : NSEnumerator @interface GSArrayEnumerator : NSEnumerator
{ {
NSGArray *array; GSArray *array;
unsigned pos; unsigned pos;
} }
- (id) initWithArray: (NSGArray*)anArray; - (id) initWithArray: (GSArray*)anArray;
@end @end
@implementation NSGArrayEnumerator @implementation GSArrayEnumerator
- (id) initWithArray: (NSGArray*)anArray - (id) initWithArray: (GSArray*)anArray
{ {
[super init]; [super init];
array = anArray; array = anArray;
@ -709,12 +708,12 @@ static SEL eqSel;
@end @end
@interface NSGArrayEnumeratorReverse : NSGArrayEnumerator @interface GSArrayEnumeratorReverse : GSArrayEnumerator
@end @end
@implementation NSGArrayEnumeratorReverse @implementation GSArrayEnumeratorReverse
- (id) initWithArray: (NSGArray*)anArray - (id) initWithArray: (GSArray*)anArray
{ {
[super initWithArray: anArray]; [super initWithArray: anArray];
pos = array->_count; pos = array->_count;
@ -729,7 +728,7 @@ static SEL eqSel;
} }
@end @end
@implementation NSGArray (GNUstep) @implementation GSArray (GNUstep)
/* /*
* The comparator function takes two items as arguments, the first is the * The comparator function takes two items as arguments, the first is the
* item to be added, the second is the item already in the array. * item to be added, the second is the item already in the array.
@ -847,3 +846,85 @@ static SEL eqSel;
} }
@end @end
@implementation GSPlaceholderArray
static Class GSInlineArrayClass;
+ (void) initialize
{
GSInlineArrayClass = [GSInlineArray class];
}
- (id) autorelease
{
NSWarnLog(@"-autorelease sent to uninitialised array");
return self; // placeholders never get released.
}
- (id) objectAtIndex: (unsigned)index
{
[NSException raise: NSInternalInconsistencyException
format: @"attempt to use uninitialised array"];
return 0;
}
- (void) dealloc
{
return; // placeholders never get deallocated.
}
- (id) initWithCoder: (NSCoder*)aCoder
{
GSInlineArray *a;
unsigned c;
[aCoder decodeValueOfObjCType: @encode(unsigned) at: &c];
a = (id)NSAllocateObject(GSInlineArrayClass, sizeof(id)*c,
GSObjCZone(self));
a->_contents_array = (id*)&a[1];
if (c > 0)
{
#if !GS_WITH_GC
unsigned i;
#endif
[aCoder decodeArrayOfObjCType: @encode(id)
count: c
at: a->_contents_array];
#if !GS_WITH_GC
for (i = 0; i < c; i++)
{
a->_contents_array[i] = RETAIN(a->_contents_array[i]);
a->_count = i;
}
#endif
}
a->_count = c;
return a;
}
- (id) initWithObjects: (id*)objects count: (unsigned)count
{
self = (id)NSAllocateObject(GSInlineArrayClass, sizeof(id)*count,
GSObjCZone(self));
return [self initWithObjects: objects count: count];
}
- (unsigned) count
{
[NSException raise: NSInternalInconsistencyException
format: @"attempt to use uninitialised array"];
return 0;
}
- (void) release
{
return; // placeholders never get released.
}
- (id) retain
{
return self; // placeholders never get retained.
}
@end

View file

@ -278,8 +278,10 @@ setup()
{ {
setup(); setup();
} }
- (id) autorelease - (id) autorelease
{ {
NSWarnLog(@"-autorelease sent to uninitialised string");
return self; // placeholders never get released. return self; // placeholders never get released.
} }

View file

@ -31,30 +31,39 @@
#include <Foundation/NSArray.h> #include <Foundation/NSArray.h>
#include <Foundation/NSCoder.h> #include <Foundation/NSCoder.h>
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSGArray.h>
#include <Foundation/NSRange.h> #include <Foundation/NSRange.h>
#include <limits.h> #include <limits.h>
#include <Foundation/NSUtilities.h> #include <Foundation/NSUtilities.h>
#include <Foundation/NSException.h> #include <Foundation/NSException.h>
#include <Foundation/NSAutoreleasePool.h> #include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSUserDefaults.h> #include <Foundation/NSUserDefaults.h>
#include <Foundation/NSThread.h>
#include <Foundation/NSMapTable.h>
#include <Foundation/NSLock.h>
#include <Foundation/NSDebug.h> #include <Foundation/NSDebug.h>
@class NSArrayEnumerator; @class NSArrayEnumerator;
@class NSArrayEnumeratorReverse; @class NSArrayEnumeratorReverse;
@interface NSArrayNonCore : NSArray @class GSArray;
@end @class GSInlineArray;
@interface NSMutableArrayNonCore : NSMutableArray @class GSMutableArray;
@end @class GSPlaceholderArray;
@class NSGInlineArray; static Class NSArrayClass;
static Class GSArrayClass;
static Class GSInlineArrayClass;
static Class NSMutableArrayClass;
static Class GSMutableArrayClass;
static Class GSPlaceholderArrayClass;
static Class NSArray_abstract_class; static GSPlaceholderArray *defaultPlaceholderArray;
static Class NSArray_concrete_class; static NSMapTable *placeholderMap;
static Class NSMutableArray_abstract_class; static NSLock *placeholderLock;
static Class NSMutableArray_concrete_class;
static Class NSGInlineArrayClass; @interface NSArray (GSPrivate)
- (id) _initWithObjects: firstObject rest: (va_list) ap;
@end
@implementation NSArray @implementation NSArray
@ -81,21 +90,64 @@ static SEL rlSel;
remSel = @selector(removeObjectAtIndex:); remSel = @selector(removeObjectAtIndex:);
rlSel = @selector(removeLastObject); rlSel = @selector(removeLastObject);
NSArray_abstract_class = [NSArray class]; NSArrayClass = [NSArray class];
behavior_class_add_class (self, [NSArrayNonCore class]); NSMutableArrayClass = [NSMutableArray class];
NSMutableArray_abstract_class = [NSMutableArray class]; GSArrayClass = [GSArray class];
NSArray_concrete_class = [NSGArray class]; GSInlineArrayClass = [GSInlineArray class];
NSMutableArray_concrete_class = [NSGMutableArray class]; GSMutableArrayClass = [GSMutableArray class];
NSMutableArray_concrete_class = [NSGMutableArray class]; GSPlaceholderArrayClass = [GSPlaceholderArray class];
NSGInlineArrayClass = [NSGInlineArray class];
/*
* Set up infrastructure for placeholder arrays.
*/
defaultPlaceholderArray = (GSPlaceholderArray*)
NSAllocateObject(GSPlaceholderArrayClass, 0, NSDefaultMallocZone());
placeholderMap = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks,
NSNonRetainedObjectMapValueCallBacks, 0);
placeholderLock = [NSLock new];
} }
} }
+ (id) allocWithZone: (NSZone*)z + (id) allocWithZone: (NSZone*)z
{ {
if (self == NSArray_abstract_class) if (self == NSArrayClass)
{ {
return NSAllocateObject(NSArray_concrete_class, 0, z); /*
* For a constant array, we return a placeholder object that can
* be converted to a real object when its initialisation method
* is called.
*/
if (z == NSDefaultMallocZone() || z == 0)
{
/*
* As a special case, we can return a placeholder for an array
* in the default malloc zone extremely efficiently.
*/
return defaultPlaceholderArray;
}
else
{
id obj;
/*
* For anything other than the default zone, we need to
* locate the correct placeholder in the (lock protected)
* table of placeholders.
*/
[placeholderLock lock];
obj = (id)NSMapGet(placeholderMap, (void*)z);
if (obj == nil)
{
/*
* There is no placeholder object for this zone, so we
* create a new one and use that.
*/
obj = (id)NSAllocateObject(GSPlaceholderArrayClass, 0, z);
NSMapInsert(placeholderMap, (void*)z, (void*)obj);
}
[placeholderLock unlock];
return obj;
}
} }
else else
{ {
@ -105,38 +157,109 @@ static SEL rlSel;
+ (id) array + (id) array
{ {
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] init]); id o;
o = [self allocWithZone: NSDefaultMallocZone()];
o = [o initWithObjects: (id*)0 count: 0];
return AUTORELEASE(o);
} }
+ (id) arrayWithArray: (NSArray*)array + (id) arrayWithArray: (NSArray*)array
{ {
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] id o;
initWithArray: array]);
o = [self allocWithZone: NSDefaultMallocZone()];
o = [o initWithArray: array];
return AUTORELEASE(o);
} }
+ (id) arrayWithContentsOfFile: (NSString*)file + (id) arrayWithContentsOfFile: (NSString*)file
{ {
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] id o;
initWithContentsOfFile: file]);
o = [self allocWithZone: NSDefaultMallocZone()];
o = [o initWithContentsOfFile: file];
return AUTORELEASE(o);
} }
+ (id) arrayWithObject: (id)anObject + (id) arrayWithObject: (id)anObject
{ {
id o; id o;
if (anObject == nil) o = [self allocWithZone: NSDefaultMallocZone()];
[NSException raise: NSInvalidArgumentException
format: @"Tried to add nil"];
o = NSAllocateObject(NSGInlineArrayClass, sizeof(id), NSDefaultMallocZone());
o = [o initWithObjects: &anObject count: 1]; o = [o initWithObjects: &anObject count: 1];
return AUTORELEASE(o); return AUTORELEASE(o);
} }
/* This is the designated initializer for NSArray. */ + (id) arrayWithObjects: firstObject, ...
- (id) initWithObjects: (id*)objects count: (unsigned)count
{ {
[self subclassResponsibility: _cmd]; va_list ap;
return nil; va_start(ap, firstObject);
self = [[self allocWithZone: NSDefaultMallocZone()]
_initWithObjects: firstObject rest: ap];
va_end(ap);
return AUTORELEASE(self);
}
+ (id) arrayWithObjects: (id*)objects count: (unsigned)count
{
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
initWithObjects: objects count: count]);
}
- (NSArray*) arrayByAddingObject: (id)anObject
{
id na;
unsigned c = [self count];
if (anObject == nil)
[NSException raise: NSInvalidArgumentException
format: @"Attempt to add nil to an array"];
if (c == 0)
na = [[GSArrayClass allocWithZone: NSDefaultMallocZone()]
initWithObjects: &anObject count: 1];
else
{
id objects[c+1];
[self getObjects: objects];
objects[c] = anObject;
na = [[GSArrayClass allocWithZone: NSDefaultMallocZone()]
initWithObjects: objects count: c+1];
}
return AUTORELEASE(na);
}
- (NSArray*) arrayByAddingObjectsFromArray: (NSArray*)anotherArray
{
id na;
unsigned c, l;
c = [self count];
l = [anotherArray count];
{
id objects[c+l];
[self getObjects: objects];
[anotherArray getObjects: &objects[c]];
na = [NSArrayClass arrayWithObjects: objects count: c+l];
}
return na;
}
- (Class) classForCoder
{
return NSArrayClass;
}
- (BOOL) containsObject: anObject
{
return ([self indexOfObject: anObject] != NSNotFound);
}
- (id) copyWithZone: (NSZone*)zone
{
return RETAIN(self);
} }
- (unsigned) count - (unsigned) count
@ -145,19 +268,6 @@ static SEL rlSel;
return 0; return 0;
} }
- (id) objectAtIndex: (unsigned)index
{
[self subclassResponsibility: _cmd];
return nil;
}
/* The NSCoding Protocol */
- (Class) classForCoder
{
return NSArray_abstract_class;
}
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
unsigned count = [self count]; unsigned count = [self count];
@ -176,202 +286,6 @@ static SEL rlSel;
} }
} }
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned count;
[aCoder decodeValueOfObjCType: @encode(unsigned)
at: &count];
if (count > 0)
{
id contents[count];
[aCoder decodeArrayOfObjCType: @encode(id)
count: count
at: contents];
return [self initWithObjects: contents count: count];
}
else
return [self initWithObjects: 0 count: 0];
}
/* The NSCopying Protocol */
- (id) copyWithZone: (NSZone*)zone
{
return RETAIN(self);
}
/* The NSMutableCopying Protocol */
- (id) mutableCopyWithZone: (NSZone*)zone
{
return [[NSMutableArray_concrete_class allocWithZone: zone]
initWithArray: self];
}
@end
@implementation NSArrayNonCore
- (NSArray*) arrayByAddingObject: (id)anObject
{
id na;
unsigned c = [self count];
if (anObject == nil)
[NSException raise: NSInvalidArgumentException
format: @"Attempt to add nil to an array"];
if (c == 0)
na = [[NSArray_concrete_class allocWithZone: NSDefaultMallocZone()]
initWithObjects: &anObject count: 1];
else
{
id objects[c+1];
[self getObjects: objects];
objects[c] = anObject;
na = [[NSArray_concrete_class allocWithZone: NSDefaultMallocZone()]
initWithObjects: objects count: c+1];
}
return AUTORELEASE(na);
}
- (NSArray*) arrayByAddingObjectsFromArray: (NSArray*)anotherArray
{
id na;
unsigned c, l;
c = [self count];
l = [anotherArray count];
{
id objects[c+l];
[self getObjects: objects];
[anotherArray getObjects: &objects[c]];
na = [NSArray_abstract_class arrayWithObjects: objects count: c+l];
}
return na;
}
- (id) initWithObjects: firstObject rest: (va_list) ap
{
register unsigned i;
register unsigned curSize;
auto unsigned prevSize;
auto unsigned newSize;
auto id *objsArray;
auto id tmpId;
/* Do initial allocation. */
prevSize = 3;
curSize = 5;
objsArray = (id*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(id) * curSize);
tmpId = firstObject;
/* Loop through adding objects to array until a nil is
* found.
*/
for (i = 0; tmpId != nil; i++)
{
/* Put id into array. */
objsArray[i] = tmpId;
/* If the index equals the current size, increase size. */
if (i == curSize - 1)
{
/* Fibonacci series. Supposedly, for this application,
* the fibonacci series will be more memory efficient.
*/
newSize = prevSize + curSize;
prevSize = curSize;
curSize = newSize;
/* Reallocate object array. */
objsArray = (id*)NSZoneRealloc(NSDefaultMallocZone(), objsArray,
sizeof(id) * curSize);
}
tmpId = va_arg(ap, id);
}
va_end( ap );
/* Put object ids into NSArray. */
self = [self initWithObjects: objsArray count: i];
NSZoneFree(NSDefaultMallocZone(), objsArray);
return( self );
}
- (id) initWithObjects: firstObject, ...
{
va_list ap;
va_start(ap, firstObject);
self = [self initWithObjects: firstObject rest: ap];
va_end(ap);
return self;
}
- (id) initWithContentsOfFile: (NSString*)file
{
NSString *myString;
myString = [[NSString allocWithZone: NSDefaultMallocZone()]
initWithContentsOfFile: file];
if (myString)
{
id result;
NS_DURING
{
result = [myString propertyList];
}
NS_HANDLER
{
result = nil;
}
NS_ENDHANDLER
RELEASE(myString);
if ([result isKindOfClass: NSArray_abstract_class])
{
[self initWithArray: result];
return self;
}
}
NSWarnMLog(@"Contents of file does not contain an array", 0);
RELEASE(self);
return nil;
}
+ (id) arrayWithObjects: firstObject, ...
{
va_list ap;
va_start(ap, firstObject);
self = [[self allocWithZone: NSDefaultMallocZone()]
initWithObjects: firstObject rest: ap];
va_end(ap);
return AUTORELEASE(self);
}
+ (id) arrayWithObjects: (id*)objects count: (unsigned)count
{
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
initWithObjects: objects count: count]);
}
- (id) initWithArray: (NSArray*)array
{
unsigned c;
c = [array count];
{
id objects[c];
[array getObjects: objects];
self = [self initWithObjects: objects count: c];
}
return self;
}
- (void) getObjects: (id*)aBuffer - (void) getObjects: (id*)aBuffer
{ {
unsigned i, c = [self count]; unsigned i, c = [self count];
@ -463,16 +377,155 @@ static SEL rlSel;
return NSNotFound; return NSNotFound;
} }
- (BOOL) containsObject: anObject - (id) init
{ {
return ([self indexOfObject: anObject] != NSNotFound); return [self initWithObjects: (id*)0 count: 0];
}
- (id) initWithArray: (NSArray*)array
{
unsigned c;
c = [array count];
{
id objects[c];
[array getObjects: objects];
self = [self initWithObjects: objects count: c];
}
return self;
}
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned count;
[aCoder decodeValueOfObjCType: @encode(unsigned)
at: &count];
if (count > 0)
{
id contents[count];
[aCoder decodeArrayOfObjCType: @encode(id)
count: count
at: contents];
return [self initWithObjects: contents count: count];
}
else
return [self initWithObjects: 0 count: 0];
}
- (id) initWithContentsOfFile: (NSString*)file
{
NSString *myString;
myString = [[NSString allocWithZone: NSDefaultMallocZone()]
initWithContentsOfFile: file];
if (myString)
{
id result;
NS_DURING
{
result = [myString propertyList];
}
NS_HANDLER
{
result = nil;
}
NS_ENDHANDLER
RELEASE(myString);
if ([result isKindOfClass: NSArrayClass])
{
[self initWithArray: result];
return self;
}
}
NSWarnMLog(@"Contents of file does not contain an array", 0);
RELEASE(self);
return nil;
}
/* This is the designated initializer for NSArray. */
- (id) initWithObjects: (id*)objects count: (unsigned)count
{
[self subclassResponsibility: _cmd];
return nil;
}
- (id) _initWithObjects: firstObject rest: (va_list) ap
{
register unsigned i;
register unsigned curSize;
auto unsigned prevSize;
auto unsigned newSize;
auto id *objsArray;
auto id tmpId;
/* Do initial allocation. */
prevSize = 3;
curSize = 5;
objsArray = (id*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(id) * curSize);
tmpId = firstObject;
/* Loop through adding objects to array until a nil is
* found.
*/
for (i = 0; tmpId != nil; i++)
{
/* Put id into array. */
objsArray[i] = tmpId;
/* If the index equals the current size, increase size. */
if (i == curSize - 1)
{
/* Fibonacci series. Supposedly, for this application,
* the fibonacci series will be more memory efficient.
*/
newSize = prevSize + curSize;
prevSize = curSize;
curSize = newSize;
/* Reallocate object array. */
objsArray = (id*)NSZoneRealloc(NSDefaultMallocZone(), objsArray,
sizeof(id) * curSize);
}
tmpId = va_arg(ap, id);
}
va_end( ap );
/* Put object ids into NSArray. */
self = [self initWithObjects: objsArray count: i];
NSZoneFree(NSDefaultMallocZone(), objsArray);
return( self );
}
- (id) initWithObjects: firstObject, ...
{
va_list ap;
va_start(ap, firstObject);
self = [self _initWithObjects: firstObject rest: ap];
va_end(ap);
return self;
}
- (id) mutableCopyWithZone: (NSZone*)zone
{
return [[GSMutableArrayClass allocWithZone: zone]
initWithArray: self];
}
- (id) objectAtIndex: (unsigned)index
{
[self subclassResponsibility: _cmd];
return nil;
} }
- (BOOL) isEqual: (id)anObject - (BOOL) isEqual: (id)anObject
{ {
if (self == anObject) if (self == anObject)
return YES; return YES;
if ([anObject isKindOfClass: NSArray_abstract_class]) if ([anObject isKindOfClass: NSArrayClass])
return [self isEqualToArray: anObject]; return [self isEqualToArray: anObject];
return NO; return NO;
} }
@ -570,10 +623,10 @@ static SEL rlSel;
NSMutableArray *sortedArray; NSMutableArray *sortedArray;
NSArray *result; NSArray *result;
sortedArray = [[NSMutableArray_abstract_class allocWithZone: sortedArray = [[NSMutableArrayClass allocWithZone:
NSDefaultMallocZone()] initWithArray: self]; NSDefaultMallocZone()] initWithArray: self];
[sortedArray sortUsingFunction: comparator context: context]; [sortedArray sortUsingFunction: comparator context: context];
result = [NSArray_abstract_class arrayWithArray: sortedArray]; result = [NSArrayClass arrayWithArray: sortedArray];
RELEASE(sortedArray); RELEASE(sortedArray);
return result; return result;
} }
@ -806,16 +859,14 @@ static NSString *indentStrings[] = {
{ {
if (self == [NSMutableArray class]) if (self == [NSMutableArray class])
{ {
behavior_class_add_class (self, [NSMutableArrayNonCore class]);
behavior_class_add_class (self, [NSArrayNonCore class]);
} }
} }
+ (id) allocWithZone: (NSZone*)z + (id) allocWithZone: (NSZone*)z
{ {
if (self == NSMutableArray_abstract_class) if (self == NSMutableArrayClass)
{ {
return NSAllocateObject(NSMutableArray_concrete_class, 0, z); return NSAllocateObject(GSMutableArrayClass, 0, z);
} }
else else
{ {
@ -833,7 +884,7 @@ static NSString *indentStrings[] = {
- (Class) classForCoder - (Class) classForCoder
{ {
return NSMutableArray_abstract_class; return NSMutableArrayClass;
} }
/* The NSCopying Protocol */ /* The NSCopying Protocol */
@ -849,7 +900,7 @@ static NSString *indentStrings[] = {
[self getObjects: objects]; [self getObjects: objects];
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
objects[i] = [objects[i] copyWithZone: zone]; objects[i] = [objects[i] copyWithZone: zone];
newArray = [[NSArray_concrete_class allocWithZone: zone] newArray = [[GSArrayClass allocWithZone: zone]
initWithObjects: objects count: count]; initWithObjects: objects count: count];
while (i > 0) while (i > 0)
RELEASE(objects[--i]); RELEASE(objects[--i]);
@ -905,11 +956,6 @@ static NSString *indentStrings[] = {
[self subclassResponsibility: _cmd]; [self subclassResponsibility: _cmd];
} }
@end
@implementation NSMutableArrayNonCore
+ (id) arrayWithCapacity: (unsigned)numItems + (id) arrayWithCapacity: (unsigned)numItems
{ {
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]

View file

@ -35,11 +35,11 @@
#include <Foundation/NSNotificationQueue.h> #include <Foundation/NSNotificationQueue.h>
#include <Foundation/NSObjCRuntime.h> #include <Foundation/NSObjCRuntime.h>
#include <base/NSGArray.h>
@class NSGDictionary; @class NSGDictionary;
@class NSGMutableDictionary; @class NSGMutableDictionary;
@class NSDataMalloc; @class NSDataMalloc;
@class GSInlineArray;
@class GSMutableArray;
@class GSCString; @class GSCString;
@class GSUnicodeString; @class GSUnicodeString;
@class GSMutableString; @class GSMutableString;
@ -86,14 +86,6 @@ static char st_dict = (char)ST_DICT;
static char st_mdict = (char)ST_MDICT; static char st_mdict = (char)ST_MDICT;
static char st_data = (char)ST_DATA; static char st_data = (char)ST_DATA;
typedef struct {
@defs(NSGArray)
} NSGArrayStruct;
typedef struct {
@defs(NSGMutableArray)
} NSGMutableArrayStruct;
/* /*
@ -522,7 +514,8 @@ deserializeFromInfo(_NSDeserializerInfo* info)
} }
else else
{ {
a = NSAllocateObject(IACls, 0, NSDefaultMallocZone()); a = NSAllocateObject(IACls, sizeof(id)*size,
NSDefaultMallocZone());
a = (*iaInitImp)(a, iaInitSel, objects, size); a = (*iaInitImp)(a, iaInitSel, objects, size);
} }
#if !GS_WITH_GC #if !GS_WITH_GC
@ -694,8 +687,8 @@ deserializeFromInfo(_NSDeserializerInfo* info)
maInitSel = @selector(initWithObjects:count:); maInitSel = @selector(initWithObjects:count:);
idInitSel = @selector(initWithObjects:forKeys:count:); idInitSel = @selector(initWithObjects:forKeys:count:);
mdInitSel = @selector(initWithObjects:forKeys:count:); mdInitSel = @selector(initWithObjects:forKeys:count:);
IACls = [NSGArray class]; IACls = [GSInlineArray class];
MACls = [NSGMutableArray class]; MACls = [GSMutableArray class];
DCls = [NSDataMalloc class]; DCls = [NSDataMalloc class];
IDCls = [NSGDictionary class]; IDCls = [NSGDictionary class];
MDCls = [NSGMutableDictionary class]; MDCls = [NSGMutableDictionary class];

View file

@ -75,7 +75,7 @@
@class GSString; @class GSString;
@class GSMutableString; @class GSMutableString;
@class GSPlaceholderString; @class GSPlaceholderString;
@class NSGMutableArray; @class GSMutableArray;
@class NSGMutableDictionary; @class NSGMutableDictionary;
@ -4024,7 +4024,7 @@ setupPl()
plInit = (id (*)(id, SEL, unichar*, unsigned)) plInit = (id (*)(id, SEL, unichar*, unsigned))
[NSStringClass instanceMethodForSelector: plSel]; [NSStringClass instanceMethodForSelector: plSel];
plArray = [NSGMutableArray class]; plArray = [GSMutableArray class];
plAdd = (id (*)(id, SEL, id)) plAdd = (id (*)(id, SEL, id))
[plArray instanceMethodForSelector: @selector(addObject:)]; [plArray instanceMethodForSelector: @selector(addObject:)];