Tidyups and fixes.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@16384 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2003-04-07 08:26:40 +00:00
parent 71aa6ee47c
commit d1e9050ec3
5 changed files with 72 additions and 191 deletions

View file

@ -1,3 +1,9 @@
2003-04-07 Richard Frith-Macdonald <rfm@gnu.org>
* 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 <alexander@malmberg.org>
* Headers/gnustep/base/GSCategories.h (GS_USEIDLIST): Make the

View file

@ -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); \
} \
} \
} \

View file

@ -38,6 +38,7 @@
#include <Foundation/NSDebug.h>
#include <Foundation/NSObjCRuntime.h>
#include <Foundation/NSValue.h>
#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

View file

@ -186,7 +186,7 @@ main()
// Joining string elements
printf("Method: -componentsJoinedByString:\n");
i = [c componentsJoinedByString: @"/"];
if ([i isEqual: @"<NSObject>/<NSArray>/<NSMutableArray>"])
if ([i isEqual: @"NSObject/NSArray/NSMutableArray"])
printf("%s is correct\n", [i cString]);
else
{

View file

@ -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";