bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSMutableArray copy does a deep copy


From: James Knight
Subject: NSMutableArray copy does a deep copy
Date: Tue, 14 Jan 2003 18:29:39 -0500

For some reason, NSMutableArray's copyWithZone does a deep copy of the array, calling copy on all it's elements. NSArray does not do this. This makes absolutely no sense at all: the mutability of the array itself has nothing to do with the necessity of copying its elements. If a deep copy is desired, perhaps a deepCopy method would be useful, but it's easy enough to implement in code which needs it.

The same problem occurs with NSMutableDictionary and NSCountedSet.

As a point of reference, MacOSX's implementation does a shallow copy on both classes. Thus, it would seem to me that the current implementation is both incorrect and non-sensical. Is there some reason for it being that way?

A simple patch follows:
Index: NSArray.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/base/Source/NSArray.m,v
retrieving revision 1.113
diff -u -r1.113 NSArray.m
--- NSArray.m   4 Oct 2002 09:08:08 -0000       1.113
+++ NSArray.m   23 Oct 2002 09:04:25 -0000
@@ -1154,25 +1154,14 @@

 - (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;
 }

Index: NSCountedSet.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/base/Source/NSCountedSet.m,v
retrieving revision 1.28
diff -u -r1.28 NSCountedSet.m
--- NSCountedSet.m      5 Oct 2002 17:47:53 -0000       1.28
+++ NSCountedSet.m      23 Oct 2002 09:04:25 -0000
@@ -114,7 +114,7 @@

 - (id) copyWithZone: (NSZone*)z
 {
- return [[[self class] allocWithZone: z] initWithSet: self copyItems: YES]; + return [[[self class] allocWithZone: z] initWithSet: self copyItems: NO];
 }

 - (id) mutableCopyWithZone: (NSZone*)z
Index: NSDictionary.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/base/Source/NSDictionary.m,v
retrieving revision 1.90
diff -u -r1.90 NSDictionary.m
--- NSDictionary.m      4 Oct 2002 09:08:09 -0000       1.90
+++ NSDictionary.m      23 Oct 2002 09:04:25 -0000
@@ -1105,7 +1105,6 @@

 - (id) copyWithZone: (NSZone*)z
 {
-  /* a deep copy */
   unsigned     count = [self count];
   id           keys[count];
   id           objects[count];
@@ -1120,18 +1119,11 @@
     {
       keys[i] = key;
       objects[i] = (*objImp)(self, objSel, key);
-      objects[i] = [objects[i] copyWithZone: z];
     }
   newDictionary = [[GSDictionaryClass allocWithZone: z]
          initWithObjects: objects
                  forKeys: keys
                    count: count];
-#if    !GS_WITH_GC
-  while (i > 0)
-    {
-      [objects[--i] release];
-    }
-#endif
   return newDictionary;
 }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]