33 NSAutoreleasePool *pool =
nil;
34 NSMutableSet *objects =
nil;
36 if (
object ==
nil)
return nil;
38 pool = [[NSAutoreleasePool alloc] init];
39 objects = [NSMutableSet set];
41 object = [object ooDeepCopyWithSharedObjects:objects];
49@implementation NSObject (OODeepCopy)
51- (id) ooDeepCopyWithSharedObjects:(NSMutableSet *)objects
53 if ([
self conformsToProtocol:
@protocol(NSCopying)])
66@implementation NSString (OODeepCopy)
68- (id) ooDeepCopyWithSharedObjects:(NSMutableSet *)objects
70 NSUInteger length = [
self length];
71 if (length == 0)
return [[NSString string] retain];
72 if (length > 128)
return [
self copy];
74 id object = [objects member:self];
75 if (
object !=
nil && [
object isKindOfClass:[NSString
class]])
77 return [object retain];
82 [objects addObject:object];
90@implementation NSValue (OODeepCopy)
92- (id) ooDeepCopyWithSharedObjects:(NSMutableSet *)objects
94 id object = [objects member:self];
95 if (
object !=
nil && [
object isKindOfClass:[NSValue
class]])
97 return [object retain];
101 object = [
self copy];
102 [objects addObject:object];
110@implementation NSArray (OODeepCopy)
112- (id) ooDeepCopyWithSharedObjects:(NSMutableSet *)objects
116 NSArray *result =
nil;
117 BOOL tempObjects = NO;
119 count = [
self count];
120 if (
count == 0)
return [[NSArray array] retain];
122 members = calloc(
sizeof *members,
count);
125 [NSException raise:NSMallocException format:@"Failed to allocate space for %lu objects in %s.", (unsigned long)count, __PRETTY_FUNCTION__];
131 objects = [[NSMutableSet alloc] init];
135 [
self getObjects:members];
139 for (i = 0; i <
count; i++)
141 members[i] = [members[i] ooDeepCopyWithSharedObjects:objects];
145 result = [[NSArray alloc] initWithObjects:members count:count];
150 for (i = 0; i <
count; i++)
152 [members[i] release];
156 if (tempObjects) [objects release];
166@implementation NSSet (OODeepCopy)
168- (id) ooDeepCopyWithSharedObjects:(NSMutableSet *)objects
173 BOOL tempObjects = NO;
175 count = [
self count];
176 if (
count == 0)
return [[NSSet set] retain];
178 members = malloc(
sizeof *members *
count);
181 [NSException raise:NSMallocException format:@"Failed to allocate space for %lu objects in %s.", (unsigned long)count, __PRETTY_FUNCTION__];
187 objects = [[NSMutableSet alloc] init];
196 foreach (member,
self)
198 members[i] = [member ooDeepCopyWithSharedObjects:objects];
203 result = [[NSSet alloc] initWithObjects:members count:count];
208 for (i = 0; i <
count; i++)
210 [members[i] release];
214 if (tempObjects) [objects release];
224@implementation NSDictionary (OODeepCopy)
226- (id) ooDeepCopyWithSharedObjects:(NSMutableSet *)objects
231 NSDictionary *result =
nil;
232 BOOL tempObjects = NO;
234 count = [
self count];
235 if (
count == 0)
return [[NSDictionary dictionary] retain];
237 keys = malloc(
sizeof *keys *
count);
238 values = malloc(
sizeof *values *
count);
239 if (keys == NULL || values == NULL)
243 [NSException raise:NSMallocException format:@"Failed to allocate space for %lu objects in %s.", (unsigned long)count, __PRETTY_FUNCTION__];
249 objects = [[NSMutableSet alloc] init];
260 keys[i] = [key ooDeepCopyWithSharedObjects:objects];
261 values[i] = [[
self objectForKey:key] ooDeepCopyWithSharedObjects:objects];
266 result = [[NSDictionary alloc] initWithObjects:values forKeys:keys count:count];
271 for (i = 0; i <
count; i++)
279 if (tempObjects) [objects release];
#define foreachkey(VAR, DICT)