ObjFW  Check-in [5413ba3c49]

Overview
Comment:One more convenience method for OFDictionary.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5413ba3c49601bd74332aa4f8df04288835584c54d63804c77840c73efe71363
User & Date: js on 2009-05-19 16:15:31
Other Links: manifest | tags
Context
2009-05-19
16:51
Add - isEqual: for OFList. check-in: 7f7e9715e1 user: js tags: trunk
16:15
One more convenience method for OFDictionary. check-in: 5413ba3c49 user: js tags: trunk
10:33
Better error handling for OFAutoreleasePools. check-in: e5efcb03ac user: js tags: trunk
Changes

Modified src/OFDictionary.h from [966817c8c7] to [315f04accc].

9
10
11
12
13
14
15

16
17
18
19
20
21
22
 * the packaging of this file.
 */

#include <stdarg.h>

#import "OFObject.h"
#import "OFList.h"


/**
 * The OFDictionary class provides a class for using hash tables.
 */
@interface OFDictionary: OFObject
{
	OFList **data;







>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * the packaging of this file.
 */

#include <stdarg.h>

#import "OFObject.h"
#import "OFList.h"
#import "OFArray.h"

/**
 * The OFDictionary class provides a class for using hash tables.
 */
@interface OFDictionary: OFObject
{
	OFList **data;
44
45
46
47
48
49
50










51
52
53
54
55
56
57
 * \param key The key
 * \param obj The object
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithKey: (OFObject <OFCopying>*)key
	  andObject: (OFObject*)obj;











/**
 * Creates a new OFDictionary with the specified keys objects.
 *
 * \param first The first key
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithKeysAndObjects: (OFObject <OFCopying>*)first, ...;







>
>
>
>
>
>
>
>
>
>







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 * \param key The key
 * \param obj The object
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithKey: (OFObject <OFCopying>*)key
	  andObject: (OFObject*)obj;

/**
 * Creates a new OFDictionary with the specified keys and objects.
 *
 * \param keys An array of keys
 * \param objs An array of objects
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithKeys: (OFArray*)keys
	  andObjects: (OFArray*)objs;

/**
 * Creates a new OFDictionary with the specified keys objects.
 *
 * \param first The first key
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithKeysAndObjects: (OFObject <OFCopying>*)first, ...;
78
79
80
81
82
83
84











85
86
87
88
89
90
91
 * \param key The key
 * \param obj The object
 * \return A new initialized OFDictionary
 */
- initWithKey: (OFObject <OFCopying>*)key
    andObject: (OFObject*)obj;












/**
 * Initializes an already allocated OFDictionary with the specified keys and
 * objects.
 *
 * \param first The first key
 * \return A new initialized OFDictionary
 */







>
>
>
>
>
>
>
>
>
>
>







89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 * \param key The key
 * \param obj The object
 * \return A new initialized OFDictionary
 */
- initWithKey: (OFObject <OFCopying>*)key
    andObject: (OFObject*)obj;

/**
 * Initializes an already allocated OFDictionary with the specified keys and
 * objects.
 *
 * \param keys An array of keys
 * \param objs An array of objects
 * \return A new initialized OFDictionary
 */
- initWithKeys: (OFArray*)keys
    andObjects: (OFArray*)objs;

/**
 * Initializes an already allocated OFDictionary with the specified keys and
 * objects.
 *
 * \param first The first key
 * \return A new initialized OFDictionary
 */

Modified src/OFDictionary.m from [bbacfd711b] to [7df4d449e0].

36
37
38
39
40
41
42







43
44
45
46
47
48
49

+ dictionaryWithKey: (OFObject <OFCopying>*)key
	  andObject: (OFObject*)obj
{
	return [[[self alloc] initWithKey: key
				andObject: obj] autorelease];
}








+ dictionaryWithKeysAndObjects: (OFObject <OFCopying>*)first, ...
{
	id ret;
	va_list args;

	va_start(args, first);







>
>
>
>
>
>
>







36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

+ dictionaryWithKey: (OFObject <OFCopying>*)key
	  andObject: (OFObject*)obj
{
	return [[[self alloc] initWithKey: key
				andObject: obj] autorelease];
}

+ dictionaryWithKeys: (OFArray*)keys
	  andObjects: (OFArray*)objs
{
	return [[[self alloc] initWithKeys: keys
				andObjects: objs] autorelease];
}

+ dictionaryWithKeysAndObjects: (OFObject <OFCopying>*)first, ...
{
	id ret;
	va_list args;

	va_start(args, first);
141
142
143
144
145
146
147


























































148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

165
166
167
168
169
170
171
172
173
174
175
176
177
		@throw e;
	} @finally {
		[key release];
	}

	return self;
}



























































- initWithKeysAndObjects: (OFObject <OFCopying>*)first, ...
{
	id ret;
	va_list args;

	va_start(args, first);
	ret = [self initWithKey: first
		     andArgList: args];
	va_end(args);

	return ret;
}

- initWithKey: (OFObject <OFCopying>*)first
   andArgList: (va_list)args
{

	id key, obj;
	Class c;
	uint32_t hash;

	self = [self init];
	obj = va_arg(args, id);

	if (first == nil || obj == nil) {
		c = isa;
		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];
	}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

















>
|




|







148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
		@throw e;
	} @finally {
		[key release];
	}

	return self;
}

- initWithKeys: (OFArray*)keys
    andObjects: (OFArray*)objs
{
	Class c;
	OFObject <OFCopying> **keys_data;
	OFObject **objs_data;
	size_t count, i;

	self = [self init];
	count = [keys count];

	if (keys == nil || objs == nil || count != [objs count]) {
		c = isa;
		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];
	}

	keys_data = [keys data];
	objs_data = [objs data];

	for (i = 0; i < count; i++) {
		uint32_t hash;
		OFObject <OFCopying> *key;

		if (keys_data[i] == nil || objs_data[i] == nil) {
			c = isa;
			[self dealloc];
			@throw [OFInvalidArgumentException newWithClass: isa
							    andSelector: _cmd];
		}

		hash = [keys_data[i] hash] & (size - 1);

		@try {
			key = [keys_data[i] copy];
		} @catch (OFException *e) {
			[self dealloc];
			@throw e;
		}

		@try {
			if (data[hash] == nil)
				data[hash] = [[OFList alloc] init];

			[data[hash] append: key];
			[data[hash] append: objs_data[i]];
		} @catch (OFException *e) {
			[self dealloc];
			@throw e;
		} @finally {
			[key release];
		}
	}

	return self;
}

- initWithKeysAndObjects: (OFObject <OFCopying>*)first, ...
{
	id ret;
	va_list args;

	va_start(args, first);
	ret = [self initWithKey: first
		     andArgList: args];
	va_end(args);

	return ret;
}

- initWithKey: (OFObject <OFCopying>*)first
   andArgList: (va_list)args
{
	OFObject <OFCopying> *key;
	OFObject *obj;
	Class c;
	uint32_t hash;

	self = [self init];
	obj = va_arg(args, OFObject*);

	if (first == nil || obj == nil) {
		c = isa;
		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];
	}
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	} @finally {
		[key release];
	}

	while ((key = va_arg(args, id)) != nil) {
		if ((obj = va_arg(args, id)) == nil) {
			c = isa;
			[self dealloc];
			@throw [OFInvalidArgumentException newWithClass: isa
							    andSelector: _cmd];
		}

		hash = [key hash] & (size - 1);







|
|







260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	} @finally {
		[key release];
	}

	while ((key = va_arg(args, OFObject <OFCopying>*)) != nil) {
		if ((obj = va_arg(args, OFObject*)) == nil) {
			c = isa;
			[self dealloc];
			@throw [OFInvalidArgumentException newWithClass: isa
							    andSelector: _cmd];
		}

		hash = [key hash] & (size - 1);

Modified tests/OFDictionary/OFDictionary.m from [cda6d24680] to [ba48f04406].

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string.h>

#import "OFAutoreleasePool.h"
#import "OFDictionary.h"
#import "OFString.h"
#import "OFExceptions.h"

#define TESTS 10

int
main()
{
	int i = 0;

	OFDictionary *dict = [OFMutableDictionary dictionaryWithHashSize: 16];







|







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string.h>

#import "OFAutoreleasePool.h"
#import "OFDictionary.h"
#import "OFString.h"
#import "OFExceptions.h"

#define TESTS 12

int
main()
{
	int i = 0;

	OFDictionary *dict = [OFMutableDictionary dictionaryWithHashSize: 16];
112
113
114
115
116
117
118
















119
120
121
122
123
	[dict release];
	dict = [OFDictionary dictionaryWithKey: @"foo"
				     andObject: @"bar"];
	if (![[dict get: @"foo"] isEqual: @"bar"]) {
		printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
		return 1;
	}

















	printf("\033[1;32mTests successful: %d/%d\033[0m\n", i, TESTS);

	return 0;
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	[dict release];
	dict = [OFDictionary dictionaryWithKey: @"foo"
				     andObject: @"bar"];
	if (![[dict get: @"foo"] isEqual: @"bar"]) {
		printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
		return 1;
	}

	i++;
	[dict release];
	dict = [OFDictionary
	    dictionaryWithKeys: [OFArray arrayWithObjects: @"k1", @"k2", nil]
		    andObjects: [OFArray arrayWithObjects: @"o1", @"o2", nil]];
	if (![[dict get: @"k1"] isEqual: @"o1"]) {
		printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
		return 1;
	}

	i++;
	if (![[dict get: @"k2"] isEqual: @"o2"]) {
		printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
		return 1;
	}

	printf("\033[1;32mTests successful: %d/%d\033[0m\n", i, TESTS);

	return 0;
}