From 4bd83219c25040c317c889df1f55b7e5cdeae669 Mon Sep 17 00:00:00 2001 From: CaS Date: Mon, 7 Apr 2003 08:26:40 +0000 Subject: [PATCH] Tidyups and fixes. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@16384 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++ Headers/gnustep/base/GSCategories.h | 141 ++++++++++------------------ Source/NSDictionary.m | 103 ++------------------ Testing/nsarray.m | 2 +- Testing/nsdictionary.m | 11 +++ 5 files changed, 72 insertions(+), 191 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8d26632bf..0c7260794 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2003-04-07 Richard Frith-Macdonald + + * Headers/gnustep/base/GSCategories.h (GS_USEIDLIST and + GS_USEIDPAIRLIST): Complete implementation and testing. + * Source/NSDictionary.m: Use macro. + 2003-04-06 23:30 Alexander Malmberg * Headers/gnustep/base/GSCategories.h (GS_USEIDLIST): Make the diff --git a/Headers/gnustep/base/GSCategories.h b/Headers/gnustep/base/GSCategories.h index 3438917e7..f00a5842e 100644 --- a/Headers/gnustep/base/GSCategories.h +++ b/Headers/gnustep/base/GSCategories.h @@ -93,6 +93,7 @@ /** * The number of objects to try to get from varargs into an array on * the stack ... if there are more than this, use the heap. + * NB. This MUST be a multiple of 2 */ #define GS_MAX_OBJECTS_FROM_STACK 128 #endif @@ -122,101 +123,59 @@ va_list __ap; \ unsigned int __max = GS_MAX_OBJECTS_FROM_STACK; \ unsigned int __count = 0; \ - id __buf[__max/2]; \ + id __buf[__max]; \ id *__objects = __buf; \ - if (isPaired == YES) \ + id *__pairs = &__objects[__max/2]; \ + id __obj = firstObject; \ + va_start(__ap, firstObject); \ + while (__obj != nil && __count < __max) \ { \ - id __buf2[__max/2]; \ - id *__pairs = __buf2; \ - while (__count < __max) \ + if ((__count % 2) == 0) \ { \ - id __tmp = va_arg(__ap, id); \ - if (__tmp == nil) \ + __objects[__count/2] = __obj; \ + } \ + else \ + { \ + __pairs[__count/2] = __obj; \ + } \ + __obj = va_arg(__ap, id); \ + if (++__count == __max) \ + { \ + while (__obj != nil) \ { \ - break; \ + __count++; \ + __obj = va_arg(__ap, id); \ } \ - if ((__count % 2) == 0) \ + } \ + } \ + if ((__count % 2) == 1) \ + { \ + __pairs[__count/2] = nil; \ + __count++; \ + } \ + va_end(__ap); \ + if (__count > __max) \ + { \ + unsigned int __tmp; \ + __objects = (id*)objc_malloc(__count*sizeof(id)); \ + __pairs = &__objects[__count/2]; \ + __objects[0] = firstObject; \ + va_start(__ap, firstObject); \ + for (__tmp = 1; __tmp < __count; __tmp++) \ + { \ + if ((__tmp % 2) == 0) \ { \ - __objects[__count/2] = __tmp; \ + __objects[__tmp/2] = va_arg(__ap, id); \ } \ else \ { \ - __pairs[__count/2] = __tmp; \ - } \ - if (++__count == __max) \ - { \ - __tmp = va_arg(__ap, id); \ - while (__tmp != nil) \ - { \ - __count++; \ - __tmp = va_arg(__ap, id); \ - } \ - } \ - else if ((__count % 2) == 1) \ - { \ - __count++; \ - __pairs[__count/2] = nil; \ + __pairs[__tmp/2] = va_arg(__ap, id); \ } \ } \ va_end(__ap); \ - if (__count > __max) \ - { \ - unsigned int __tmp; \ - if ((__count % 2) == 1) __count++; \ - __objects = (id*)objc_malloc(__count*sizeof(id)*2); \ - __pairs = &__objects[__count/2]; \ - va_start(__ap, firstObject); \ - for (__tmp = 0; __tmp < __count; __tmp++) \ - { \ - if ((__count % 2) == 0) \ - { \ - __objects[__count/2] = va_arg(__ap, id); \ - } \ - else \ - { \ - __pairs[__count/2] = va_arg(__ap, id); \ - } \ - } \ - va_end(__ap); \ - } \ - code; \ - if (__objects != __buf) objc_free(__objects); \ - } \ - else \ - { \ - va_start(__ap, firstObject); \ - while (__count < __max) \ - { \ - __objects[__count] = va_arg(__ap, id); \ - if (__objects[__count] == nil) \ - { \ - break; \ - } \ - if (++__count == __max) \ - { \ - id __tmp = va_arg(__ap, id); \ - while (__tmp != nil) \ - { \ - __count++; \ - __tmp = va_arg(__ap, id); \ - } \ - } \ - } \ - va_end(__ap); \ - if (__count > __max) \ - { \ - unsigned int __tmp; \ - __objects = (id*)objc_malloc(__count*sizeof(id)); \ - va_start(__ap, firstObject); \ - for (__tmp = 0; __tmp < __count; __tmp++) \ - { \ - __objects[__tmp] = va_arg(__ap, id); \ - } \ - va_end(__ap); \ - } \ - code; \ - if (__objects != __buf) objc_free(__objects); \ } \ + code; \ + if (__objects != __buf) objc_free(__objects); \ }) /** @@ -243,24 +202,18 @@ unsigned int __count = 0; \ id __buf[__max]; \ id *__objects = __buf; \ + id __obj = firstObject; \ va_start(__ap, firstObject); \ - while (__count < __max) \ + while (__obj != nil && __count < __max) \ { \ - if (__count) \ - __objects[__count] = va_arg(__ap, id); \ - else \ - __objects[__count] = firstObject; \ - if (__objects[__count] == nil) \ - { \ - break; \ - } \ + __objects[__count] = __obj; \ + __obj = va_arg(__ap, id); \ if (++__count == __max) \ { \ - id __tmp = va_arg(__ap, id); \ - while (__tmp != nil) \ + while (__obj != nil) \ { \ __count++; \ - __tmp = va_arg(__ap, id); \ + __obj = va_arg(__ap, id); \ } \ } \ } \ diff --git a/Source/NSDictionary.m b/Source/NSDictionary.m index 5ab3158ee..fdeb3d9bf 100644 --- a/Source/NSDictionary.m +++ b/Source/NSDictionary.m @@ -38,6 +38,7 @@ #include #include #include +#include "gnustep/base/GSCategories.h" #include "GSPrivate.h" @implementation NSDictionary @@ -290,56 +291,8 @@ static SEL appSel; */ - (id) initWithObjectsAndKeys: (id)firstObject, ... { - va_list ap; - int capacity = 16; - int num_pairs = 0; - id *objects; - id *keys; - id arg; - int argi = 1; - - va_start (ap, firstObject); - if (firstObject == nil) - { - return [self init]; - } - /* Gather all the arguments in a simple array, in preparation for - calling the designated initializer. */ - objects = (id*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(id) * capacity); - keys = (id*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(id) * capacity); - - objects[num_pairs] = firstObject; - /* Keep grabbing arguments until we get a nil... */ - while ((arg = va_arg (ap, id))) - { - if (num_pairs >= capacity) - { - /* Must increase capacity in order to fit additional ARG's. */ - capacity *= 2; - objects = (id*)NSZoneRealloc(NSDefaultMallocZone(), objects, - sizeof(id) * capacity); - keys = (id*)NSZoneRealloc(NSDefaultMallocZone(), keys, - sizeof(id) * capacity); - } - /* ...and alternately dump them into OBJECTS and KEYS */ - if (argi++ % 2 == 0) - objects[num_pairs] = arg; - else - { - keys[num_pairs] = arg; - num_pairs++; - } - } - if (argi %2 != 0) - { - NSZoneFree(NSDefaultMallocZone(), objects); - NSZoneFree(NSDefaultMallocZone(), keys); - [NSException raise: NSInvalidArgumentException - format: @"init dictionary with nil key"]; - } - self = [self initWithObjects: objects forKeys: keys count: num_pairs]; - NSZoneFree(NSDefaultMallocZone(), objects); - NSZoneFree(NSDefaultMallocZone(), keys); + GS_USEIDPAIRLIST(firstObject, + self = [self initWithObjects: __objects forKeys: __pairs count: __count/2]); return self; } @@ -350,53 +303,11 @@ static SEL appSel; */ + (id) dictionaryWithObjectsAndKeys: (id)firstObject, ... { - va_list ap; - int capacity = 16; - int num_pairs = 0; - id *objects; - id *keys; - id arg; - int argi = 1; + id o = [self allocWithZone: NSDefaultMallocZone()]; - va_start (ap, firstObject); - /* Gather all the arguments in a simple array, in preparation for - calling the designated initializer. */ - objects = (id*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(id) * capacity); - keys = (id*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(id) * capacity); - if (firstObject != nil) - { - NSDictionary *d; - objects[num_pairs] = firstObject; - /* Keep grabbing arguments until we get a nil... */ - while ((arg = va_arg (ap, id))) - { - if (num_pairs >= capacity) - { - /* Must increase capacity in order to fit additional ARG's. */ - capacity *= 2; - objects = (id*)NSZoneRealloc(NSDefaultMallocZone(), objects, - sizeof(id) * capacity); - keys = (id*)NSZoneRealloc(NSDefaultMallocZone(), keys, - sizeof(id) * capacity); - } - /* ...and alternately dump them into OBJECTS and KEYS */ - if (argi++ % 2 == 0) - objects[num_pairs] = arg; - else - { - keys[num_pairs] = arg; - num_pairs++; - } - } - NSAssert (argi % 2 == 0, NSInvalidArgumentException); - d = AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] - initWithObjects: objects forKeys: keys count: num_pairs]); - NSZoneFree(NSDefaultMallocZone(), objects); - NSZoneFree(NSDefaultMallocZone(), keys); - return d; - } - /* FIRSTOBJECT was nil; just return an empty NSDictionary object. */ - return [self dictionary]; + GS_USEIDPAIRLIST(firstObject, + o = [o initWithObjects: __objects forKeys: __pairs count: __count/2]); + return AUTORELEASE(o); } + (id) dictionaryWithObjects: (NSArray*)objects forKeys: (NSArray*)keys diff --git a/Testing/nsarray.m b/Testing/nsarray.m index 6e79d353c..1c1b1fbb8 100644 --- a/Testing/nsarray.m +++ b/Testing/nsarray.m @@ -186,7 +186,7 @@ main() // Joining string elements printf("Method: -componentsJoinedByString:\n"); i = [c componentsJoinedByString: @"/"]; - if ([i isEqual: @"//"]) + if ([i isEqual: @"NSObject/NSArray/NSMutableArray"]) printf("%s is correct\n", [i cString]); else { diff --git a/Testing/nsdictionary.m b/Testing/nsdictionary.m index 4e1f3899d..f3b8dd748 100644 --- a/Testing/nsdictionary.m +++ b/Testing/nsdictionary.m @@ -55,6 +55,17 @@ main(int argc, char** argv, char** envp) keys = [NSArray arrayWithObjects: @"cow", @"fish", @"horse", @"chicken", nil]; a = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; + b = [NSDictionary dictionaryWithObjectsAndKeys: + @"vache", + @"cow", + @"poisson", + @"fish", + @"cheval", + @"horse", + @"poulet", + @"chicken", nil]; + + printf("Match is %d\n", [a isEqual: b]); printf("NSDictionary has count %d\n", [a count]); key = @"fish";