From f6468a06aa1aaeb68ea63f709c1d2eef867ba9b9 Mon Sep 17 00:00:00 2001 From: Richard Frith-MacDonald Date: Thu, 8 Apr 1999 12:17:15 +0000 Subject: [PATCH] Make GNUstep copying of containers consistent with OPENSTEP git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4047 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++ Source/NSArray.m | 159 ++++++++++++++-------------- Source/NSDictionary.m | 150 +++++++++++++------------- Source/NSSet.m | 105 ++++++++----------- Testing/GNUmakefile | 2 + Testing/containers.m | 237 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 437 insertions(+), 222 deletions(-) create mode 100644 Testing/containers.m diff --git a/ChangeLog b/ChangeLog index 0b48cc5ef..47475b06b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Thu Apr 8 12:40:00 1999 Richard Frith-Macdonald + + * Source/NSArray.m: Make copying behavior consistent with OPENSTEP. + * Source/NSDictionary.m: Make copying behavior consistent with OPENSTEP. + * Source/NSSet.m: Make copying behavior consistent with OPENSTEP. + Wed Apr 7 12:22:00 1999 Richard Frith-Macdonald * Source/NSCharacterSet.m: Use punctuationCharSet.dat diff --git a/Source/NSArray.m b/Source/NSArray.m index fbcc82164..47b8e90c0 100644 --- a/Source/NSArray.m +++ b/Source/NSArray.m @@ -109,28 +109,28 @@ static Class NSMutableArray_concrete_class; + arrayWithObject: anObject { if (anObject == nil) - [NSException raise:NSInvalidArgumentException - format:@"Tried to add nil"]; - return [[[self alloc] initWithObjects:&anObject count:1] + [NSException raise: NSInvalidArgumentException + format: @"Tried to add nil"]; + return [[[self alloc] initWithObjects: &anObject count: 1] autorelease]; } /* This is the designated initializer for NSArray. */ - initWithObjects: (id*)objects count: (unsigned)count { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return nil; } - (unsigned) count { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return 0; } - objectAtIndex: (unsigned)index { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return nil; } @@ -178,40 +178,15 @@ static Class NSMutableArray_concrete_class; - copyWithZone: (NSZone*)zone { - /* a deep copy */ - unsigned count = [self count]; - id oldObjects[count]; - id newObjects[count]; - id newArray; - unsigned i; - BOOL needCopy = [self isKindOfClass: [NSMutableArray class]]; - - if (NSShouldRetainWithZone(self, zone) == NO) - needCopy = YES; - [self getObjects: oldObjects]; - for (i = 0; i < count; i++) - { - newObjects[i] = [oldObjects[i] copyWithZone:zone]; - if (newObjects[i] != oldObjects[i]) - needCopy = YES; - } - if (needCopy) - newArray = [[[[self class] _concreteClass] allocWithZone:zone] - initWithObjects:newObjects count:count]; - else - newArray = [self retain]; - for (i = 0; i < count; i++) - [newObjects[i] release]; - return newArray; + return [self retain]; } /* The NSMutableCopying Protocol */ - mutableCopyWithZone: (NSZone*)zone { - /* a shallow copy */ - return [[[[self class] _mutableConcreteClass] allocWithZone:zone] - initWithArray:self]; + return [[[[self class] _mutableConcreteClass] allocWithZone: zone] + initWithArray: self]; } @end @@ -303,7 +278,7 @@ static Class NSMutableArray_concrete_class; { va_list ap; va_start(ap, firstObject); - self = [self initWithObjects:firstObject rest:ap]; + self = [self initWithObjects: firstObject rest: ap]; va_end(ap); return self; } @@ -342,7 +317,7 @@ static Class NSMutableArray_concrete_class; { va_list ap; va_start(ap, firstObject); - self = [[self alloc] initWithObjects:firstObject rest:ap]; + self = [[self alloc] initWithObjects: firstObject rest: ap]; va_end(ap); return [self autorelease]; } @@ -388,22 +363,22 @@ static Class NSMutableArray_concrete_class; return [self count]; } -- (unsigned) indexOfObjectIdenticalTo:anObject +- (unsigned) indexOfObjectIdenticalTo: anObject { unsigned i, c = [self count]; for (i = 0; i < c; i++) - if (anObject == [self objectAtIndex:i]) + if (anObject == [self objectAtIndex: i]) return i; return NSNotFound; } -- (unsigned) indexOfObjectIdenticalTo:anObject inRange: (NSRange)aRange +- (unsigned) indexOfObjectIdenticalTo: anObject inRange: (NSRange)aRange { unsigned i, e = aRange.location + aRange.length, c = [self count]; if (c < e) e = c; for (i = aRange.location; i < e; i++) - if (anObject == [self objectAtIndex:i]) + if (anObject == [self objectAtIndex: i]) return i; return NSNotFound; } @@ -413,7 +388,7 @@ static Class NSMutableArray_concrete_class; { unsigned i, c = [self count]; for (i = 0; i < c; i++) - if ([[self objectAtIndex:i] isEqual: anObject]) + if ([[self objectAtIndex: i] isEqual: anObject]) return i; return NSNotFound; } @@ -435,13 +410,13 @@ static Class NSMutableArray_concrete_class; - (BOOL) containsObject: anObject { - return ([self indexOfObject:anObject] != NSNotFound); + return ([self indexOfObject: anObject] != NSNotFound); } - (BOOL) isEqual: anObject { - if ([anObject isKindOf:[NSArray class]]) - return [self isEqualToArray:anObject]; + if ([anObject isKindOf: [NSArray class]]) + return [self isEqualToArray: anObject]; return NO; } @@ -469,7 +444,7 @@ static Class NSMutableArray_concrete_class; { unsigned i = [self count]; while (i-- > 0) - [[self objectAtIndex:i] performSelector:aSelector]; + [[self objectAtIndex: i] performSelector: aSelector]; } - (void) makeObjectsPerform: (SEL)aSelector @@ -477,26 +452,26 @@ static Class NSMutableArray_concrete_class; [self makeObjectsPerformSelector: aSelector]; } -- (void) makeObjectsPerformSelector: (SEL)aSelector withObject:argument +- (void) makeObjectsPerformSelector: (SEL)aSelector withObject: argument { unsigned i = [self count]; while (i-- > 0) - [[self objectAtIndex:i] performSelector:aSelector withObject:argument]; + [[self objectAtIndex: i] performSelector: aSelector withObject: argument]; } -- (void) makeObjectsPerform: (SEL)aSelector withObject:argument +- (void) makeObjectsPerform: (SEL)aSelector withObject: argument { - [self makeObjectsPerformSelector: aSelector withObject:argument]; + [self makeObjectsPerformSelector: aSelector withObject: argument]; } - (NSArray*) sortedArrayUsingSelector: (SEL)comparator { int compare(id elem1, id elem2, void* context) { - return (int)[elem1 performSelector:comparator withObject:elem2]; + return (int)[elem1 performSelector: comparator withObject: elem2]; } - return [self sortedArrayUsingFunction:compare context:NULL]; + return [self sortedArrayUsingFunction: compare context: NULL]; } - (NSArray*) sortedArrayUsingFunction: (int(*)(id,id,void*))comparator @@ -518,7 +493,7 @@ static Class NSMutableArray_concrete_class; NSArray *result; sortedArray = [[NSMutableArray alloc] initWithArray: self]; - [sortedArray sortUsingFunction:comparator context:context]; + [sortedArray sortUsingFunction: comparator context: context]; result = [NSArray arrayWithArray: sortedArray]; [sortedArray release]; return result; @@ -527,15 +502,15 @@ static Class NSMutableArray_concrete_class; - (NSString*) componentsJoinedByString: (NSString*)separator { unsigned i, c = [self count]; - id s = [NSMutableString stringWithCapacity:2]; /* arbitrary capacity */ + id s = [NSMutableString stringWithCapacity: 2]; /* arbitrary capacity */ if (!c) return s; - [s appendString:[[self objectAtIndex:0] description]]; + [s appendString: [[self objectAtIndex: 0] description]]; for (i = 1; i < c; i++) { - [s appendString:separator]; - [s appendString:[[self objectAtIndex:i] description]]; + [s appendString: separator]; + [s appendString: [[self objectAtIndex: i] description]]; } return s; } @@ -559,7 +534,7 @@ static Class NSMutableArray_concrete_class; unsigned i, c = [self count]; id o; for (i = 0; i < c; i++) - if ([otherArray containsObject:(o = [self objectAtIndex:i])]) + if ([otherArray containsObject: (o = [self objectAtIndex: i])]) return o; return nil; } @@ -595,13 +570,13 @@ static Class NSMutableArray_concrete_class; - (NSEnumerator*) objectEnumerator { - return [[[NSArrayEnumerator alloc] initWithArray:self] + return [[[NSArrayEnumerator alloc] initWithArray: self] autorelease]; } - (NSEnumerator*) reverseObjectEnumerator { - return [[[NSArrayEnumeratorReverse alloc] initWithArray:self] + return [[[NSArrayEnumeratorReverse alloc] initWithArray: self] autorelease]; } @@ -677,12 +652,12 @@ static NSString *indentStrings[] = { (*appImp)(result, appSel, iSizeString); - if ([item respondsToSelector: + if ([item respondsToSelector: @selector(descriptionWithLocale:indent:)]) { [item descriptionWithLocale: locale indent: level to: result]; } - else if ([item respondsToSelector: + else if ([item respondsToSelector: @selector(descriptionWithLocale:)]) { [item descriptionWithLocale: locale to: result]; @@ -725,10 +700,30 @@ static NSString *indentStrings[] = { return [super allocWithZone: z]; } +/* The NSCopying Protocol */ + +- 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 = [[[[self class] _concreteClass] allocWithZone: zone] + initWithObjects: objects count: count]; + while (i > 0) + [objects[--i] release]; + return newArray; +} + /* This is the desgnated initializer for NSMutableArray */ - initWithCapacity: (unsigned)numItems { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return nil; } @@ -739,7 +734,7 @@ static NSString *indentStrings[] = { - (void) replaceObjectAtIndex: (unsigned)index withObject: anObject { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } - (void) replaceObjectsInRange: (NSRange)aRange @@ -766,12 +761,12 @@ static NSString *indentStrings[] = { - (void) insertObject: anObject atIndex: (unsigned)index { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } - (void) removeObjectAtIndex: (unsigned)index { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } @end @@ -781,13 +776,13 @@ static NSString *indentStrings[] = { + arrayWithCapacity: (unsigned)numItems { - return [[[self alloc] initWithCapacity:numItems] + return [[[self alloc] initWithCapacity: numItems] autorelease]; } -- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile +- (BOOL)writeToFile: (NSString *)path atomically: (BOOL)useAuxiliaryFile { - return [[self description] writeToFile:path atomically:useAuxiliaryFile]; + return [[self description] writeToFile: path atomically: useAuxiliaryFile]; } /* Override our superclass's designated initializer to go our's */ @@ -796,7 +791,7 @@ static NSString *indentStrings[] = { unsigned i; self = [self initWithCapacity: count]; for (i = 0; i < count; i++) - [self addObject:objects[i]]; + [self addObject: objects[i]]; return self; } @@ -806,7 +801,7 @@ static NSString *indentStrings[] = { if (count == 0) [NSException raise: NSRangeException format: @"Trying to remove from an empty array."]; - [self removeObjectAtIndex:count-1]; + [self removeObjectAtIndex: count-1]; } - (void) removeObjectIdenticalTo: anObject @@ -828,7 +823,7 @@ static NSString *indentStrings[] = { [self removeObjectAtIndex: pos]; } -- (void) removeObject: anObject inRange:(NSRange)aRange +- (void) removeObject: anObject inRange: (NSRange)aRange { unsigned c = [self count], s = aRange.location; unsigned i = aRange.location + aRange.length; @@ -849,7 +844,7 @@ static NSString *indentStrings[] = { [self removeObjectAtIndex: pos]; } -- (void) removeObjectIdenticalTo: anObject inRange:(NSRange)aRange +- (void) removeObjectIdenticalTo: anObject inRange: (NSRange)aRange { unsigned c = [self count], s = aRange.location; unsigned i = aRange.location + aRange.length; @@ -902,7 +897,7 @@ static NSString *indentStrings[] = { [self addObject: [otherArray objectAtIndex: i]]; } -- (void) setArray:(NSArray *)otherArray +- (void) setArray: (NSArray *)otherArray { [self removeAllObjects]; [self addObjectsFromArray: otherArray]; @@ -959,7 +954,7 @@ static NSString *indentStrings[] = { { unsigned i, c = [otherArray count]; for (i = 0; i < c; i++) - [self removeObject:[otherArray objectAtIndex:i]]; + [self removeObject: [otherArray objectAtIndex: i]]; } - (void) removeObjectsInRange: (NSRange)aRange @@ -976,10 +971,10 @@ static NSString *indentStrings[] = { { int compare(id elem1, id elem2, void* context) { - return (int)[elem1 performSelector:comparator withObject:elem2]; + return (int)[elem1 performSelector: comparator withObject: elem2]; } - [self sortUsingFunction:compare context:NULL]; + [self sortUsingFunction: compare context: NULL]; } - (void) sortUsingFunction: (int(*)(id,id,void*))compare @@ -1006,12 +1001,12 @@ static NSString *indentStrings[] = { d = c - stride; while (!found) { // move to left until correct place - id a = [self objectAtIndex:d + stride]; - id b = [self objectAtIndex:d]; + id a = [self objectAtIndex: d + stride]; + id b = [self objectAtIndex: d]; if ((*compare)(a, b, context) == NSOrderedAscending) { [a retain]; - [self replaceObjectAtIndex:d + stride withObject:b]; - [self replaceObjectAtIndex:d withObject:a]; + [self replaceObjectAtIndex: d + stride withObject: b]; + [self replaceObjectAtIndex: d withObject: a]; [a release]; if (stride > d) break; @@ -1047,7 +1042,7 @@ static NSString *indentStrings[] = { { if (next_index >= [array count]) return nil; - return [array objectAtIndex:next_index++]; + return [array objectAtIndex: next_index++]; } - (void) dealloc @@ -1076,6 +1071,6 @@ static NSString *indentStrings[] = { { if (next_index < 0) return nil; - return [array objectAtIndex:next_index--]; + return [array objectAtIndex: next_index--]; } @end diff --git a/Source/NSDictionary.m b/Source/NSDictionary.m index 888fd1217..d72959a19 100644 --- a/Source/NSDictionary.m +++ b/Source/NSDictionary.m @@ -87,89 +87,53 @@ static Class NSMutableDictionary_concrete_class; forKeys: (id*)keys count: (unsigned)count { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return 0; } - (unsigned) count { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return 0; } - objectForKey: (id)aKey { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return 0; } - (NSEnumerator*) keyEnumerator { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return nil; } - (NSEnumerator*) objectEnumerator { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return nil; } - copyWithZone: (NSZone*)z { - /* a deep copy */ - unsigned count = [self count]; - id oldKeys[count]; - id newKeys[count]; - id oldObjects[count]; - id newObjects[count]; - id newDictionary; - unsigned i; - id key; - NSEnumerator *enumerator = [self keyEnumerator]; - BOOL needCopy = [self isKindOfClass: [NSMutableDictionary class]]; - - if (NSShouldRetainWithZone(self, z) == NO) - needCopy = YES; - for (i = 0; (key = [enumerator nextObject]); i++) - { - oldKeys[i] = key; - oldObjects[i] = [self objectForKey:key]; - newKeys[i] = [oldKeys[i] copyWithZone:z]; - newObjects[i] = [oldObjects[i] copyWithZone:z]; - if (oldKeys[i] != newKeys[i] || oldObjects[i] != newObjects[i]) - needCopy = YES; - } - if (needCopy) - newDictionary = [[[[self class] _concreteClass] alloc] - initWithObjects:newObjects - forKeys:newKeys - count:count]; - else - newDictionary = [self retain]; - for (i = 0; i < count; i++) - { - [newKeys[i] release]; - [newObjects[i] release]; - } - return newDictionary; + return [self retain]; } - mutableCopyWithZone: (NSZone*)z { - /* a shallow copy */ - return [[[[[self class] _mutableConcreteClass] _mutableConcreteClass] alloc] - initWithDictionary:self]; + return [[[[self class] _mutableConcreteClass] allocWithZone: z] + initWithDictionary: self]; } - (void) encodeWithCoder: (NSCoder*)aCoder { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } - (id) initWithCoder: (NSCoder*)aCoder { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return nil; } @end @@ -191,9 +155,9 @@ static Class NSMutableDictionary_concrete_class; forKeys: (id*)keys count: (unsigned)count { - return [[[self alloc] initWithObjects:objects - forKeys:keys - count:count] + return [[[self alloc] initWithObjects: objects + forKeys: keys + count: count] autorelease]; } @@ -215,7 +179,7 @@ static Class NSMutableDictionary_concrete_class; } [objects getObjects: os]; [keys getObjects: ks]; - return [self initWithObjects:os forKeys:ks count:objectCount]; + return [self initWithObjects: os forKeys: ks count: objectCount]; } - (id) initWithObjectsAndKeys: (id)firstObject, ... @@ -322,7 +286,7 @@ static Class NSMutableDictionary_concrete_class; + dictionaryWithObjects: (NSArray*)objects forKeys: (NSArray*)keys { - return [[[self alloc] initWithObjects:objects forKeys:keys] + return [[[self alloc] initWithObjects: objects forKeys: keys] autorelease]; } @@ -335,7 +299,7 @@ static Class NSMutableDictionary_concrete_class; /* Override superclass's designated initializer */ - init { - return [self initWithObjects:NULL forKeys:NULL count:0]; + return [self initWithObjects: NULL forKeys: NULL count: 0]; } - initWithDictionary: (NSDictionary*)other @@ -371,10 +335,10 @@ static Class NSMutableDictionary_concrete_class; while ((k = [e nextObject])) { ks[i] = k; - os[i] = [other objectForKey:k]; + os[i] = [other objectForKey: k]; i++; } - return [self initWithObjects:os forKeys:ks count:c]; + return [self initWithObjects: os forKeys: ks count: c]; } } @@ -408,16 +372,16 @@ static Class NSMutableDictionary_concrete_class; return nil; } -+ dictionaryWithContentsOfFile:(NSString *)path ++ dictionaryWithContentsOfFile: (NSString *)path { - return [[[self alloc] initWithContentsOfFile:path] + return [[[self alloc] initWithContentsOfFile: path] autorelease]; } - (BOOL) isEqual: other { - if ([other isKindOfClass:[NSDictionary class]]) - return [self isEqualToDictionary:other]; + if ([other isKindOfClass: [NSDictionary class]]) + return [self isEqualToDictionary: other]; return NO; } @@ -434,7 +398,7 @@ static Class NSMutableDictionary_concrete_class; if (![o1 isEqual: o2]) return NO; /* - if (![[self objectForKey:k] isEqual:[other objectForKey:k]]) + if (![[self objectForKey: k] isEqual: [other objectForKey: k]]) return NO; */ } } @@ -454,7 +418,7 @@ static Class NSMutableDictionary_concrete_class; NSAssert (k[i], NSInternalInconsistencyException); } NSAssert (![e nextObject], NSInternalInconsistencyException); - return [[[NSArray alloc] initWithObjects:k count:c] + return [[[NSArray alloc] initWithObjects: k count: c] autorelease]; } @@ -470,7 +434,7 @@ static Class NSMutableDictionary_concrete_class; NSAssert (k[i], NSInternalInconsistencyException); } NSAssert (![e nextObject], NSInternalInconsistencyException); - return [[[NSArray alloc] initWithObjects:k count:c] + return [[[NSArray alloc] initWithObjects: k count: c] autorelease]; } @@ -528,9 +492,9 @@ compareIt(id o1, id o2, void* context) return [NSArray arrayWithObjects: obuf count: c]; } -- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile +- (BOOL)writeToFile: (NSString *)path atomically: (BOOL)useAuxiliaryFile { - return [[self description] writeToFile:path atomically:useAuxiliaryFile]; + return [[self description] writeToFile: path atomically: useAuxiliaryFile]; } - (NSString*) description @@ -659,13 +623,11 @@ static NSString *indentStrings[] = { (*appImp)(result, appSel, @" = "); - if ([item respondsToSelector: - @selector(descriptionWithLocale:indent:)]) + if ([item respondsToSelector: @selector(descriptionWithLocale:indent:)]) { [item descriptionWithLocale: locale indent: level to: result]; } - else if ([item respondsToSelector: - @selector(descriptionWithLocale:)]) + else if ([item respondsToSelector: @selector(descriptionWithLocale:)]) { [item descriptionWithLocale: locale to: result]; } @@ -700,21 +662,53 @@ static NSString *indentStrings[] = { return [super allocWithZone: z]; } +- copyWithZone: (NSZone*)z +{ + /* a deep copy */ + unsigned count = [self count]; + id keys[count]; + id objects[count]; + NSDictionary *newDictionary; + unsigned i; + id key; + NSEnumerator *enumerator = [self keyEnumerator]; + static SEL nxtSel = @selector(nextObject); + IMP nxtImp = [enumerator methodForSelector: nxtSel]; + static SEL objSel = @selector(objectForKey:); + IMP objImp = [self methodForSelector: objSel]; + + for (i = 0; (key = (*nxtImp)(enumerator, nxtSel)); i++) + { + keys[i] = key; + objects[i] = (*objImp)(self, objSel, key); + objects[i] = [objects[i] copyWithZone: z]; + } + newDictionary = [[[[self class] _concreteClass] allocWithZone: z] + initWithObjects: objects + forKeys: keys + count: count]; + while (i > 0) + { + [objects[--i] release]; + } + return newDictionary; +} + /* This is the designated initializer */ - initWithCapacity: (unsigned)numItems { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return 0; } -- (void) setObject:anObject forKey:(id)aKey +- (void) setObject: anObject forKey: (id)aKey { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } -- (void) removeObjectForKey:(id)aKey +- (void) removeObjectForKey: (id)aKey { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } @end @@ -723,7 +717,7 @@ static NSString *indentStrings[] = { + dictionaryWithCapacity: (unsigned)numItems { - return [[[self alloc] initWithCapacity:numItems] + return [[[self alloc] initWithCapacity: numItems] autorelease]; } @@ -732,9 +726,9 @@ static NSString *indentStrings[] = { forKeys: (id*)keys count: (unsigned)count { - [self initWithCapacity:count]; + [self initWithCapacity: count]; while (count--) - [self setObject:objects[count] forKey:keys[count]]; + [self setObject: objects[count] forKey: keys[count]]; return self; } @@ -742,21 +736,21 @@ static NSString *indentStrings[] = { { id k, e = [self keyEnumerator]; while ((k = [e nextObject])) - [self removeObjectForKey:k]; + [self removeObjectForKey: k]; } - (void) removeObjectsForKeys: (NSArray*)keyArray { int c = [keyArray count]; while (c--) - [self removeObjectForKey:[keyArray objectAtIndex:c]]; + [self removeObjectForKey: [keyArray objectAtIndex: c]]; } - (void) addEntriesFromDictionary: (NSDictionary*)other { id k, e = [other keyEnumerator]; while ((k = [e nextObject])) - [self setObject:[other objectForKey:k] forKey:k]; + [self setObject: [other objectForKey: k] forKey: k]; } - (void) setDictionary: (NSDictionary*)otherDictionary diff --git a/Source/NSSet.m b/Source/NSSet.m index 7b393dbc1..391e1c364 100644 --- a/Source/NSSet.m +++ b/Source/NSSet.m @@ -78,8 +78,8 @@ static Class NSMutableSet_concrete_class; + setWithObjects: (id*)objects count: (unsigned)count { - return [[[self alloc] initWithObjects:objects - count:count] + return [[[self alloc] initWithObjects: objects + count: count] autorelease]; } @@ -91,8 +91,8 @@ static Class NSMutableSet_concrete_class; + setWithObject: anObject { - return [[[self alloc] initWithObjects:&anObject - count:1] + return [[[self alloc] initWithObjects: &anObject + count: 1] autorelease]; } @@ -100,7 +100,7 @@ static Class NSMutableSet_concrete_class; { va_list ap; va_start(ap, firstObject); - self = [[self alloc] initWithObjects:firstObject rest:ap]; + self = [[self alloc] initWithObjects: firstObject rest: ap]; va_end(ap); return [self autorelease]; } @@ -120,70 +120,45 @@ static Class NSMutableSet_concrete_class; - initWithObjects: (id*)objects count: (unsigned)count { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return 0; } - initWithCoder: aCoder { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return nil; } - (void) encodeWithCoder: aCoder { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } - (unsigned) count { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; return 0; } - member: anObject { - return [self subclassResponsibility:_cmd]; + return [self subclassResponsibility: _cmd]; return 0; } - (NSEnumerator*) objectEnumerator { - return [self subclassResponsibility:_cmd]; + return [self subclassResponsibility: _cmd]; } - copyWithZone: (NSZone*)z { - /* a deep copy */ - int count = [self count]; - id objects[count]; - id enumerator = [self objectEnumerator]; - id o; - NSSet *newSet; - int i; - BOOL needCopy = [self isKindOfClass: [NSMutableSet class]]; - - if (NSShouldRetainWithZone(self, z) == NO) - needCopy = YES; - - for (i = 0; (o = [enumerator nextObject]); i++) { - objects[i] = [o copyWithZone:z]; - if (objects[i] != o) - needCopy = YES; - } - if (needCopy) - newSet = [[[[self class] _concreteClass] allocWithZone: z] - initWithObjects:objects count:count]; - else - newSet = [self retain]; - for (i = 0; i < count; i++) - [objects[i] release]; - return newSet; + return [self retain]; } - mutableCopyWithZone: (NSZone*)z { - /* a shallow copy */ return [[[[self class] _mutableConcreteClass] allocWithZone: z] initWithSet: self]; } @@ -244,7 +219,7 @@ static Class NSMutableSet_concrete_class; { va_list ap; va_start(ap, firstObject); - self = [self initWithObjects:firstObject rest:ap]; + self = [self initWithObjects: firstObject rest: ap]; va_end(ap); return self; } @@ -252,7 +227,7 @@ static Class NSMutableSet_concrete_class; /* Override superclass's designated initializer */ - init { - return [self initWithObjects:NULL count:0]; + return [self initWithObjects: NULL count: 0]; } - initWithArray: (NSArray*)other @@ -284,7 +259,7 @@ static Class NSMutableSet_concrete_class; os[i] = o; i++; } - self = [self initWithObjects:os count:c]; + self = [self initWithObjects: os count: c]; if (flag) while (--i) [os[i] release]; @@ -293,7 +268,7 @@ static Class NSMutableSet_concrete_class; - initWithSet: (NSSet*)other { - return [self initWithSet:other copyItems:NO]; + return [self initWithSet: other copyItems: NO]; } - (NSArray*) allObjects @@ -306,7 +281,7 @@ static Class NSMutableSet_concrete_class; { k[i] = [e nextObject]; } - return [[[NSArray alloc] initWithObjects:k count:c] + return [[[NSArray alloc] initWithObjects: k count: c] autorelease]; } @@ -323,7 +298,7 @@ static Class NSMutableSet_concrete_class; - (BOOL) containsObject: anObject { - return (([self member:anObject]) ? YES : NO); + return (([self member: anObject]) ? YES : NO); } - (unsigned) hash @@ -335,28 +310,28 @@ static Class NSMutableSet_concrete_class; { id o, e = [self objectEnumerator]; while ((o = [e nextObject])) - [o performSelector:aSelector]; + [o performSelector: aSelector]; } - (void) makeObjectsPerformSelector: (SEL)aSelector { id o, e = [self objectEnumerator]; while ((o = [e nextObject])) - [o performSelector:aSelector]; + [o performSelector: aSelector]; } -- (void) makeObjectsPerformSelector: (SEL)aSelector withObject:argument +- (void) makeObjectsPerformSelector: (SEL)aSelector withObject: argument { id o, e = [self objectEnumerator]; while ((o = [e nextObject])) - [o performSelector:aSelector withObject: argument]; + [o performSelector: aSelector withObject: argument]; } -- (void) makeObjectsPerform: (SEL)aSelector withObject:argument +- (void) makeObjectsPerform: (SEL)aSelector withObject: argument { id o, e = [self objectEnumerator]; while ((o = [e nextObject])) - [o performSelector:aSelector withObject: argument]; + [o performSelector: aSelector withObject: argument]; } - (BOOL) intersectsSet: (NSSet*) otherSet @@ -405,8 +380,8 @@ static Class NSMutableSet_concrete_class; - (BOOL) isEqual: other { - if ([other isKindOfClass:[NSSet class]]) - return [self isEqualToSet:other]; + if ([other isKindOfClass: [NSSet class]]) + return [self isEqualToSet: other]; return NO; } @@ -417,7 +392,7 @@ static Class NSMutableSet_concrete_class; { id o, e = [self objectEnumerator]; while ((o = [e nextObject])) - if (![other member:o]) + if (![other member: o]) return NO; } /* xxx Recheck this. */ @@ -448,7 +423,7 @@ static Class NSMutableSet_concrete_class; + setWithCapacity: (unsigned)numItems { - return [[[self alloc] initWithCapacity:numItems] + return [[[self alloc] initWithCapacity: numItems] autorelease]; } @@ -457,20 +432,26 @@ static Class NSMutableSet_concrete_class; return NSAllocateObject([self _mutableConcreteClass], 0, z); } +- copyWithZone: (NSZone*)z +{ + return [[[[self class] _concreteClass] allocWithZone: z] + initWithSet: self]; +} + /* This is the designated initializer */ - initWithCapacity: (unsigned)numItems { - return [self subclassResponsibility:_cmd]; + return [self subclassResponsibility: _cmd]; } - (void) addObject: anObject { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } - (void) removeObject: anObject { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } @end @@ -481,9 +462,9 @@ static Class NSMutableSet_concrete_class; - initWithObjects: (id*)objects count: (unsigned)count { - [self initWithCapacity:count]; + [self initWithCapacity: count]; while (count--) - [self addObject:objects[count]]; + [self addObject: objects[count]]; return self; } @@ -510,8 +491,8 @@ static Class NSMutableSet_concrete_class; id key; while ((key = [keys nextObject])) - if ([other containsObject:key] == NO) - [self removeObject:key]; + if ([other containsObject: key] == NO) + [self removeObject: key]; } - (void) minusSet: (NSSet*) other @@ -520,12 +501,12 @@ static Class NSMutableSet_concrete_class; id key; while ((key = [keys nextObject])) - [self removeObject:key]; + [self removeObject: key]; } - (void) removeAllObjects { - [self subclassResponsibility:_cmd]; + [self subclassResponsibility: _cmd]; } @end diff --git a/Testing/GNUmakefile b/Testing/GNUmakefile index 926370a5c..ea95dba57 100644 --- a/Testing/GNUmakefile +++ b/Testing/GNUmakefile @@ -79,12 +79,14 @@ awake \ thread-except \ nscharacterset \ NSData-test \ +containers \ nstimezone \ create-abbrevs \ create-regions # The tool Objective-C source files to be compiled benchmark_OBJC_FILES = benchmark.m +containers_OBJC_FILES = containers.m test01_OBJC_FILES = test01.m test02_OBJC_FILES = test02.m heap_OBJC_FILES = heap.m diff --git a/Testing/containers.m b/Testing/containers.m new file mode 100644 index 000000000..722d67f15 --- /dev/null +++ b/Testing/containers.m @@ -0,0 +1,237 @@ +#import +#import + +@interface Foo: NSObject +{ +} + ++ foo; +- (BOOL) conformsToProtocol: (Protocol *) aProtocol; +- (BOOL) respondsToSelector: (SEL) aSelector; +- copyWithZone: (NSZone *) zone; +- mutableCopyWithZone: (NSZone *) zone; +@end + + +@implementation Foo + ++ foo +{ + return [[[Foo alloc] init] autorelease]; +} + +- (BOOL) conformsToProtocol: (Protocol *) aProtocol +{ + BOOL ret = [super conformsToProtocol: aProtocol]; + + NSLog(@"-[<%@:0x%x> %@<%s>] -> %@", + NSStringFromClass([self class]), self, NSStringFromSelector(_cmd), + [aProtocol name], ret ? @"YES" : @"NO"); + return ret; +} + + +- (BOOL) respondsToSelector: (SEL) aSelector +{ + BOOL ret = [super respondsToSelector: aSelector]; + + if (![NSStringFromSelector(aSelector) hasPrefix:@"description"]) + NSLog(@"-[<%@:0x%x> %@%@] -> %@", + NSStringFromClass([self class]), self, NSStringFromSelector(_cmd), + NSStringFromSelector(aSelector), ret ? @"YES" : @"NO"); + return ret; +} + +- copyWithZone: (NSZone *) zone +{ + id ret = [Foo foo]; + + NSLog(@"-[<%@:0x%x> %@0x%x] -> <%@:0x%x>", + NSStringFromClass([self class]), self, + NSStringFromSelector(_cmd), zone, NSStringFromClass([ret class]), ret); + return ret; +} + +- mutableCopyWithZone: (NSZone *) zone +{ + id ret = [Foo foo]; + + NSLog(@"-[<%@:0x%x> %@0x%x] -> <%@:0x%x>", + NSStringFromClass([self class]), self, + NSStringFromSelector(_cmd), zone, NSStringFromClass([ret class]), ret); + return ret; +} + +- retain +{ + id ret = [super retain]; + + NSLog(@"-[<%@:0x%x> %@] -> retainCount = %d", + NSStringFromClass([self class]), self, + NSStringFromSelector(_cmd), [ret retainCount]); + return ret; +} + +@end + +void +isDeepArrayCopy(NSArray *obj1, NSArray *obj2) +{ + id obj1FirstObject = [obj1 objectAtIndex:0]; + id obj2FirstObject = [obj2 objectAtIndex:0]; + + NSLog(@"<%@:0x%x> -> <%@:0x%x>, %@", + NSStringFromClass([obj1 class]), obj1, + NSStringFromClass([obj2 class]), obj2, + (obj1FirstObject == obj2FirstObject) ? ((obj1 == obj2) ? @"retained" : @"shallow") : @"deep(ish)"); +} + +void +isDeepDictionaryCopy(NSDictionary *obj1, NSDictionary *obj2) +{ + id obj1FirstObject = [obj1 objectForKey: @"Key"]; + id obj2FirstObject = [obj2 objectForKey: @"Key"]; + + NSLog(@"<%@:0x%x> -> <%@:0x%x>, %@", + NSStringFromClass([obj1 class]), obj1, + NSStringFromClass([obj2 class]), obj2, + (obj1FirstObject == obj2FirstObject) ? ((obj1 == obj2) ? @"retained" : @"shallow") : @"deep(ish)"); +} + +void +isDeepSetCopy(NSSet *obj1, NSSet *obj2) +{ + id obj1FirstObject = [obj1 anyObject]; + id obj2FirstObject = [obj2 anyObject]; + + NSLog(@"<%@:0x%x> -> <%@:0x%x>, %@", + NSStringFromClass([obj1 class]), obj1, + NSStringFromClass([obj2 class]), obj2, + (obj1FirstObject == obj2FirstObject) ? ((obj1 == obj2) ? @"retained" : @"shallow") : @"deep(ish)"); +} + + +int main(int argc, char **argv) +{ + NSAutoreleasePool *thePool = [[NSAutoreleasePool alloc] init]; + NSArray *anArrayCopy; + NSArray *aMutableArrayCopy; + NSArray *anArrayMutableCopy; + NSArray *aMutableArrayMutableCopy; + NSMutableArray *aMutableArray + = [NSMutableArray arrayWithObject: [Foo foo]]; + NSArray *anArray = [NSArray arrayWithObject: [Foo foo]]; + NSDictionary *aDictionaryCopy; + NSDictionary *aMutableDictionaryCopy; + NSDictionary *aDictionaryMutableCopy; + NSDictionary *aMutableDictionaryMutableCopy; + NSMutableDictionary *aMutableDictionary + = [NSMutableDictionary dictionaryWithObjectsAndKeys: [Foo foo], @"Key", nil]; + NSDictionary *aDictionary + = [NSDictionary dictionaryWithObjectsAndKeys: [Foo foo], @"Key", nil]; + NSSet *aSetCopy; + NSSet *aMutableSetCopy; + NSSet *aSetMutableCopy; + NSSet *aMutableSetMutableCopy; + NSMutableSet *aMutableSet = [NSMutableSet setWithObject: [Foo foo]]; + NSSet *aSet = [NSSet setWithObject: [Foo foo]]; + NSZone *zone = NSDefaultMallocZone(); + + while (zone != 0) + { + NSLog(@"Copying from zone 0x%x -> 0x%x", NSDefaultMallocZone(), zone); + + NSLog(@"MutableArray -copy"); + aMutableArrayCopy = [aMutableArray copyWithZone:zone]; + NSLog(@"MutableArray -mutableCopy"); + aMutableArrayMutableCopy = [aMutableArray mutableCopyWithZone:zone]; + NSLog(@"Array -copy"); + anArrayCopy = [anArray copyWithZone:zone]; + NSLog(@"Array -mutableCopy"); + anArrayMutableCopy = [anArray mutableCopyWithZone:zone]; + + NSLog(@"MutableArray: %@", aMutableArray); + NSLog(@"MutableArrayCopy: %@", aMutableArrayCopy); + NSLog(@"MutableArrayMutableCopy: %@", aMutableArrayMutableCopy); + NSLog(@"anArray: %@", anArray); + NSLog(@"anArrayCopy: %@", anArrayCopy); + NSLog(@"anArrayCopy: %@", anArrayMutableCopy); + + NSLog(@"Test MutableArray against Copy"); + isDeepArrayCopy(aMutableArray, aMutableArrayCopy); + + NSLog(@"Test MutableArray against MutableCopy"); + isDeepArrayCopy(aMutableArray, aMutableArrayMutableCopy); + + NSLog(@"Test Array against Copy"); + isDeepArrayCopy(anArray, anArrayCopy); + + NSLog(@"Test Array against MutableCopy"); + isDeepArrayCopy(anArray, anArrayMutableCopy); + + NSLog(@"MutableDictionary -copy"); + aMutableDictionaryCopy = [aMutableDictionary copyWithZone:zone]; + NSLog(@"MutableDictionary -mutableCopy"); + aMutableDictionaryMutableCopy = [aMutableDictionary mutableCopyWithZone:zone]; + NSLog(@"Dictionary -copy"); + aDictionaryCopy = [aDictionary copyWithZone:zone]; + NSLog(@"Dictionary -mutableCopy"); + aDictionaryMutableCopy = [aDictionary mutableCopyWithZone:zone]; + + NSLog(@"MutableDictionary: %@", aMutableDictionary); + NSLog(@"MutableDictionaryCopy: %@", aMutableDictionaryCopy); + NSLog(@"MutableDictionaryMutableCopy: %@", aMutableDictionaryMutableCopy); + NSLog(@"aDictionary: %@", aDictionary); + NSLog(@"aDictionaryCopy: %@", aDictionaryCopy); + NSLog(@"aDictionaryCopy: %@", aDictionaryMutableCopy); + + NSLog(@"Test MutableDictionary against Copy"); + isDeepDictionaryCopy(aMutableDictionary, aMutableDictionaryCopy); + + NSLog(@"Test MutableDictionary against MutableCopy"); + isDeepDictionaryCopy(aMutableDictionary, aMutableDictionaryMutableCopy); + + NSLog(@"Test Dictionary against Copy"); + isDeepDictionaryCopy(aDictionary, aDictionaryCopy); + + NSLog(@"Test Dictionary against MutableCopy"); + isDeepDictionaryCopy(aDictionary, aDictionaryMutableCopy); + + NSLog(@"MutableSet -copy"); + aMutableSetCopy = [aMutableSet copyWithZone:zone]; + NSLog(@"MutableSet -mutableCopy"); + aMutableSetMutableCopy = [aMutableSet mutableCopyWithZone:zone]; + NSLog(@"Set -copy"); + aSetCopy = [aSet copyWithZone:zone]; + NSLog(@"Set -mutableCopy"); + aSetMutableCopy = [aSet mutableCopyWithZone:zone]; + + NSLog(@"MutableSet: %@", aMutableSet); + NSLog(@"MutableSetCopy: %@", aMutableSetCopy); + NSLog(@"MutableSetMutableCopy: %@", aMutableSetMutableCopy); + NSLog(@"aSet: %@", aSet); + NSLog(@"aSetCopy: %@", aSetCopy); + NSLog(@"aSetCopy: %@", aSetMutableCopy); + + NSLog(@"Test MutableSet against Copy"); + isDeepSetCopy(aMutableSet, aMutableSetCopy); + + NSLog(@"Test MutableSet against MutableCopy"); + isDeepSetCopy(aMutableSet, aMutableSetMutableCopy); + + NSLog(@"Test Set against Copy"); + isDeepSetCopy(aSet, aSetCopy); + + NSLog(@"Test Set against MutableCopy"); + isDeepSetCopy(aSet, aSetMutableCopy); + + if (zone == NSDefaultMallocZone()) + zone = NSCreateZone(NSPageSize(), NSPageSize(), YES); + else + zone = 0; + } + [thePool release]; + + return 0; +} +