mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-31 00:30:53 +00:00
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:
parent
e4472e049d
commit
4bd83219c2
5 changed files with 72 additions and 191 deletions
|
@ -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>
|
2003-04-06 23:30 Alexander Malmberg <alexander@malmberg.org>
|
||||||
|
|
||||||
* Headers/gnustep/base/GSCategories.h (GS_USEIDLIST): Make the
|
* Headers/gnustep/base/GSCategories.h (GS_USEIDLIST): Make the
|
||||||
|
|
|
@ -93,6 +93,7 @@
|
||||||
/**
|
/**
|
||||||
* The number of objects to try to get from varargs into an array on
|
* 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.
|
* 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
|
#define GS_MAX_OBJECTS_FROM_STACK 128
|
||||||
#endif
|
#endif
|
||||||
|
@ -122,101 +123,59 @@
|
||||||
va_list __ap; \
|
va_list __ap; \
|
||||||
unsigned int __max = GS_MAX_OBJECTS_FROM_STACK; \
|
unsigned int __max = GS_MAX_OBJECTS_FROM_STACK; \
|
||||||
unsigned int __count = 0; \
|
unsigned int __count = 0; \
|
||||||
id __buf[__max/2]; \
|
id __buf[__max]; \
|
||||||
id *__objects = __buf; \
|
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]; \
|
if ((__count % 2) == 0) \
|
||||||
id *__pairs = __buf2; \
|
|
||||||
while (__count < __max) \
|
|
||||||
{ \
|
{ \
|
||||||
id __tmp = va_arg(__ap, id); \
|
__objects[__count/2] = __obj; \
|
||||||
if (__tmp == nil) \
|
} \
|
||||||
|
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 \
|
else \
|
||||||
{ \
|
{ \
|
||||||
__pairs[__count/2] = __tmp; \
|
__pairs[__tmp/2] = va_arg(__ap, id); \
|
||||||
} \
|
|
||||||
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; \
|
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
va_end(__ap); \
|
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; \
|
unsigned int __count = 0; \
|
||||||
id __buf[__max]; \
|
id __buf[__max]; \
|
||||||
id *__objects = __buf; \
|
id *__objects = __buf; \
|
||||||
|
id __obj = firstObject; \
|
||||||
va_start(__ap, firstObject); \
|
va_start(__ap, firstObject); \
|
||||||
while (__count < __max) \
|
while (__obj != nil && __count < __max) \
|
||||||
{ \
|
{ \
|
||||||
if (__count) \
|
__objects[__count] = __obj; \
|
||||||
__objects[__count] = va_arg(__ap, id); \
|
__obj = va_arg(__ap, id); \
|
||||||
else \
|
|
||||||
__objects[__count] = firstObject; \
|
|
||||||
if (__objects[__count] == nil) \
|
|
||||||
{ \
|
|
||||||
break; \
|
|
||||||
} \
|
|
||||||
if (++__count == __max) \
|
if (++__count == __max) \
|
||||||
{ \
|
{ \
|
||||||
id __tmp = va_arg(__ap, id); \
|
while (__obj != nil) \
|
||||||
while (__tmp != nil) \
|
|
||||||
{ \
|
{ \
|
||||||
__count++; \
|
__count++; \
|
||||||
__tmp = va_arg(__ap, id); \
|
__obj = va_arg(__ap, id); \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
#include <Foundation/NSDebug.h>
|
#include <Foundation/NSDebug.h>
|
||||||
#include <Foundation/NSObjCRuntime.h>
|
#include <Foundation/NSObjCRuntime.h>
|
||||||
#include <Foundation/NSValue.h>
|
#include <Foundation/NSValue.h>
|
||||||
|
#include "gnustep/base/GSCategories.h"
|
||||||
#include "GSPrivate.h"
|
#include "GSPrivate.h"
|
||||||
|
|
||||||
@implementation NSDictionary
|
@implementation NSDictionary
|
||||||
|
@ -290,56 +291,8 @@ static SEL appSel;
|
||||||
*/
|
*/
|
||||||
- (id) initWithObjectsAndKeys: (id)firstObject, ...
|
- (id) initWithObjectsAndKeys: (id)firstObject, ...
|
||||||
{
|
{
|
||||||
va_list ap;
|
GS_USEIDPAIRLIST(firstObject,
|
||||||
int capacity = 16;
|
self = [self initWithObjects: __objects forKeys: __pairs count: __count/2]);
|
||||||
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);
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,53 +303,11 @@ static SEL appSel;
|
||||||
*/
|
*/
|
||||||
+ (id) dictionaryWithObjectsAndKeys: (id)firstObject, ...
|
+ (id) dictionaryWithObjectsAndKeys: (id)firstObject, ...
|
||||||
{
|
{
|
||||||
va_list ap;
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
||||||
int capacity = 16;
|
|
||||||
int num_pairs = 0;
|
|
||||||
id *objects;
|
|
||||||
id *keys;
|
|
||||||
id arg;
|
|
||||||
int argi = 1;
|
|
||||||
|
|
||||||
va_start (ap, firstObject);
|
GS_USEIDPAIRLIST(firstObject,
|
||||||
/* Gather all the arguments in a simple array, in preparation for
|
o = [o initWithObjects: __objects forKeys: __pairs count: __count/2]);
|
||||||
calling the designated initializer. */
|
return AUTORELEASE(o);
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (id) dictionaryWithObjects: (NSArray*)objects forKeys: (NSArray*)keys
|
+ (id) dictionaryWithObjects: (NSArray*)objects forKeys: (NSArray*)keys
|
||||||
|
|
|
@ -186,7 +186,7 @@ main()
|
||||||
// Joining string elements
|
// Joining string elements
|
||||||
printf("Method: -componentsJoinedByString:\n");
|
printf("Method: -componentsJoinedByString:\n");
|
||||||
i = [c componentsJoinedByString: @"/"];
|
i = [c componentsJoinedByString: @"/"];
|
||||||
if ([i isEqual: @"<NSObject>/<NSArray>/<NSMutableArray>"])
|
if ([i isEqual: @"NSObject/NSArray/NSMutableArray"])
|
||||||
printf("%s is correct\n", [i cString]);
|
printf("%s is correct\n", [i cString]);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -55,6 +55,17 @@ main(int argc, char** argv, char** envp)
|
||||||
keys = [NSArray arrayWithObjects:
|
keys = [NSArray arrayWithObjects:
|
||||||
@"cow", @"fish", @"horse", @"chicken", nil];
|
@"cow", @"fish", @"horse", @"chicken", nil];
|
||||||
a = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
|
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]);
|
printf("NSDictionary has count %d\n", [a count]);
|
||||||
key = @"fish";
|
key = @"fish";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue