git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@15654 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2003-01-20 17:18:06 +00:00
parent 2c70c72df9
commit 6cff936ed7
8 changed files with 138 additions and 47 deletions

View file

@ -4,6 +4,15 @@
* Source/Additions/Unicode.m: Added built-in support for the Latin9 * Source/Additions/Unicode.m: Added built-in support for the Latin9
character encoding ... contains the Euro symbol ... nice for character encoding ... contains the Euro symbol ... nice for
european users. european users.
* Source/NSArray.m: Make abstract copying implementation like that
of latest MacOS-X rather than OPENSTEP ... no relationship between
mutability and depth.
* Source/NSDictionary.m: ditto
* Source/NSSet.m: ditto
* Source/GSArray.m: Implement optimised copies ... copying an immutable
object just retains it, and copying a mutable object is shallow.
* Source/GSDictionary.m: ditto
* Source/GSSet.m: ditto
2003-01-16 Richard Frith-Macdonald <rfm@gnu.org> 2003-01-16 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -90,17 +90,47 @@
@end @end
/** /**
* This protocol must be adopted by any class wishing to support copying. * This protocol must be adopted by any class wishing to support copying -
* ie where instances of the class should be able to create new instances
* which are copies of the original and, where a class has mutable and
* immutable versions, where the copies are immutable.
*/ */
@protocol NSCopying @protocol NSCopying
/**
* Called by [NSObject-copy] passing NSDefaultMallocZone() as zone.<br />
* This method returns a copy of the receiver and, where the receiver is a
* mutable variant of a class which has an immutable partner class, the
* object returned is an instance of that immutable class.<br />
* The new object is <em>not</em> autoreleased, and is considered to be
* 'owned' by the calling code ... which is therefore responsible for
* releasing it.<br />
* In the case where the receiver is an instance of a container class,
* it is undefined whether contained objects are merely retained in the
* new copy, or are themselves copied, or whether some other mechanism
* entirely is used.
*/
- (id) copyWithZone: (NSZone*)zone; - (id) copyWithZone: (NSZone*)zone;
@end @end
/** /**
* This protocol must be adopted by any class wishing to support * This protocol must be adopted by any class wishing to support
* mutable copying. * mutable copying - ie where instances of the class should be able
* to create mutable copies of themselves.
*/ */
@protocol NSMutableCopying @protocol NSMutableCopying
/**
* Called by [NSObject-mutableCopy] passing NSDefaultMallocZone() as zone.<br />
* This method returns a copy of the receiver and, where the receiver is an
* immmutable variant of a class which has a mutable partner class, the
* object returned is an instance of that mutable class.
* The new object is <em>not</em> autoreleased, and is considered to be
* 'owned' by the calling code ... which is therefore responsible for
* releasing it.<br />
* In the case where the receiver is an instance of a container class,
* it is undefined whether contained objects are merely retained in the
* new copy, or are themselves copied, or whether some other mechanism
* entirely is used.
*/
- (id) mutableCopyWithZone: (NSZone*)zone; - (id) mutableCopyWithZone: (NSZone*)zone;
@end @end

View file

@ -34,6 +34,8 @@
static SEL eqSel; static SEL eqSel;
static Class GSInlineArrayClass;
@class GSArrayEnumerator; @class GSArrayEnumerator;
@class GSArrayEnumeratorReverse; @class GSArrayEnumeratorReverse;
@ -73,6 +75,7 @@ static SEL eqSel;
{ {
[self setVersion: 1]; [self setVersion: 1];
eqSel = @selector(isEqual:); eqSel = @selector(isEqual:);
GSInlineArrayClass = [GSInlineArray class];
} }
} }
@ -83,6 +86,11 @@ static SEL eqSel;
return array; return array;
} }
- (id) copyWithZone: (NSZone*)zone
{
return RETAIN(self); // Optimised version
}
- (void) dealloc - (void) dealloc
{ {
if (_contents_array) if (_contents_array)
@ -357,6 +365,17 @@ static SEL eqSel;
_count++; /* Do this AFTER we have retained the object. */ _count++; /* Do this AFTER we have retained the object. */
} }
/**
* Optimised code for copying
*/
- (id) copyWithZone: (NSZone*)zone
{
NSArray *copy;
copy = (id)NSAllocateObject(GSInlineArrayClass, sizeof(id)*_count, zone);
return [copy initWithObjects: _contents_array count: _count];
}
- (void) exchangeObjectAtIndex: (unsigned int)i1 - (void) exchangeObjectAtIndex: (unsigned int)i1
withObjectAtIndex: (unsigned int)i2 withObjectAtIndex: (unsigned int)i2
{ {
@ -892,8 +911,6 @@ static SEL eqSel;
@implementation GSPlaceholderArray @implementation GSPlaceholderArray
static Class GSInlineArrayClass;
+ (void) initialize + (void) initialize
{ {
GSInlineArrayClass = [GSInlineArray class]; GSInlineArrayClass = [GSInlineArray class];

View file

@ -84,6 +84,11 @@ static SEL objSel;
} }
} }
- (id) copyWithZone: (NSZone*)zone
{
return RETAIN(self);
}
- (unsigned) count - (unsigned) count
{ {
return map.nodeCount; return map.nodeCount;
@ -277,6 +282,13 @@ static SEL objSel;
} }
} }
- (id) copyWithZone: (NSZone*)zone
{
NSDictionary *copy = [GSDictionary allocWithZone: zone];
return [copy initWithDictionary: self copyItems: NO];
}
/* Designated initialiser */ /* Designated initialiser */
- (id) initWithCapacity: (unsigned)cap - (id) initWithCapacity: (unsigned)cap
{ {

View file

@ -144,6 +144,11 @@ static Class mutableSetClass;
} }
} }
- (id) copyWithZone: (NSZone*)z
{
return RETAIN(self);
}
- (unsigned) count - (unsigned) count
{ {
return map.nodeCount; return map.nodeCount;
@ -504,6 +509,14 @@ static Class mutableSetClass;
} }
} }
/* Override version from GSSet */
- (id) copyWithZone: (NSZone*)z
{
NSSet *copy = [setClass allocWithZone: z];
return [copy initWithSet: self copyItems: NO];
}
/* Designated initialiser */ /* Designated initialiser */
- (id) initWithCapacity: (unsigned)cap - (id) initWithCapacity: (unsigned)cap
{ {

View file

@ -306,12 +306,16 @@ static SEL rlSel;
} }
/** /**
* The default NSArray implemntation of a copy is simply to -retain * Returns a new copy of the receiver.<br />
* the receiver and return it. * The default abstract implementation of a copy is to use the
* -initWithArray:copyItems: method with the flag set to YES.<br />
* Immutable subclasses generally simply retain and return the receiver.
*/ */
- (id) copyWithZone: (NSZone*)zone - (id) copyWithZone: (NSZone*)zone
{ {
return RETAIN(self); NSArray *copy = [NSArrayClass allocWithZone: zone];
return [copy initWithArray: self copyItems: YES];
} }
/** <override-subclass /> /** <override-subclass />
@ -669,12 +673,16 @@ static SEL rlSel;
/** /**
* Returns an NSMutableArray instance containing the same objects as * Returns an NSMutableArray instance containing the same objects as
* the receiver. * the receiver.<br />
* The default implementation does this by calling the
* -initWithArray:copyItems: method on a newly created object,
* and passing it NO to tell it just to retain the items.
*/ */
- (id) mutableCopyWithZone: (NSZone*)zone - (id) mutableCopyWithZone: (NSZone*)zone
{ {
return [[GSMutableArrayClass allocWithZone: zone] NSMutableArray *copy = [NSMutableArrayClass allocWithZone: zone];
initWithArray: self];
return [copy initWithArray: self copyItems: NO];
} }
/** <override-subclass /> /** <override-subclass />
@ -828,7 +836,7 @@ static int compare(id elem1, id elem2, void* context)
NSMutableArray *sortedArray; NSMutableArray *sortedArray;
sortedArray = [[NSMutableArrayClass allocWithZone: sortedArray = [[NSMutableArrayClass allocWithZone:
NSDefaultMallocZone()] initWithArray: self]; NSDefaultMallocZone()] initWithArray: self copyItems: NO];
[sortedArray sortUsingFunction: comparator context: context]; [sortedArray sortUsingFunction: comparator context: context];
return AUTORELEASE([sortedArray makeImmutableCopyOnFail: NO]); return AUTORELEASE([sortedArray makeImmutableCopyOnFail: NO]);
@ -1108,32 +1116,6 @@ static int compare(id elem1, id elem2, void* context)
return NSMutableArrayClass; return NSMutableArrayClass;
} }
/* The NSCopying Protocol */
- (id) copyWithZone: (NSZone*)zone
{
/* a deep copy */
unsigned count = [self count];
id objects[count];
NSArray *newArray;
unsigned i;
[self getObjects: objects];
for (i = 0; i < count; i++)
{
objects[i] = [objects[i] copyWithZone: zone];
}
newArray = [[GSArrayClass allocWithZone: zone]
initWithObjects: objects count: count];
#if GS_WITH_GC == 0
while (i > 0)
{
[objects[--i] release];
}
#endif
return newArray;
}
/** <init /> /** <init />
* Initialise the array with the specified capacity ... this * Initialise the array with the specified capacity ... this
* should ensure that the array can have numItems added efficiently. * should ensure that the array can have numItems added efficiently.

View file

@ -141,15 +141,31 @@ static SEL appSel;
return nil; return nil;
} }
/**
* Returns a new copy of the receiver.<br />
* The default abstract implementation of a copy is to use the
* -initWithDictionary:copyItems: method with the flag set to YES.<br />
* Immutable subclasses generally simply retain and return the receiver.
*/
- (id) copyWithZone: (NSZone*)z - (id) copyWithZone: (NSZone*)z
{ {
return RETAIN(self); NSDictionary *copy = [NSDictionaryClass allocWithZone: z];
return [copy initWithDictionary: self copyItems: NO];
} }
/**
* Returns a new instance containing the same objects as
* the receiver.<br />
* The default implementation does this by calling the
* -initWithDictionary:copyItems: method on a newly created object,
* and passing it NO to tell it just to retain the items.
*/
- (id) mutableCopyWithZone: (NSZone*)z - (id) mutableCopyWithZone: (NSZone*)z
{ {
return [[GSMutableDictionaryClass allocWithZone: z] NSMutableDictionary *copy = [NSMutableDictionaryClass allocWithZone: z];
initWithDictionary: self];
return [copy initWithDictionary: self copyItems: NO];
} }
- (Class) classForCoder - (Class) classForCoder

View file

@ -113,9 +113,17 @@ static Class NSMutableSet_concrete_class;
return NSSet_abstract_class; return NSSet_abstract_class;
} }
/**
* Returns a new copy of the receiver.<br />
* The default abstract implementation of a copy is to use the
* -initWithSet:copyItems: method with the flag set to YES.<br />
* Concrete subclasses generally simply retain and return the receiver.
*/
- (id) copyWithZone: (NSZone*)z - (id) copyWithZone: (NSZone*)z
{ {
return RETAIN(self); NSSet *copy = [NSSet_concrete_class allocWithZone: z];
return [copy initWithSet: self copyItems: YES];
} }
/** /**
@ -186,9 +194,18 @@ static Class NSMutableSet_concrete_class;
return 0; return 0;
} }
/**
* Returns a new instance containing the same objects as
* the receiver.<br />
* The default implementation does this by calling the
* -initWithSet:copyItems: method on a newly created object,
* and passing it NO to tell it just to retain the items.
*/
- (id) mutableCopyWithZone: (NSZone*)z - (id) mutableCopyWithZone: (NSZone*)z
{ {
return [[NSMutableSet_concrete_class allocWithZone: z] initWithSet: self]; NSMutableSet *copy = [NSMutableSet_concrete_class allocWithZone: z];
return [copy initWithSet: self copyItems: NO];
} }
- (NSEnumerator*) objectEnumerator - (NSEnumerator*) objectEnumerator
@ -493,11 +510,6 @@ static Class NSMutableSet_concrete_class;
return NSMutableSet_concrete_class; return NSMutableSet_concrete_class;
} }
- (id) copyWithZone: (NSZone*)z
{
return [[NSSet_concrete_class allocWithZone: z] initWithSet: self];
}
/** <init /> /** <init />
* Initialises a newly allocated set to contain no objects but * Initialises a newly allocated set to contain no objects but
* to have space available to hold the specified number of items.<br /> * to have space available to hold the specified number of items.<br />