ObjFW  Check-in [d42a56787b]

Overview
Comment:Add +[OFDictionary dictionaryWithObjects:forKeys:count:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d42a56787bcded84d6a3dc786bc13f7c1e36e96a7cc03aaed3bdb71517757df3
User & Date: js on 2012-03-12 12:14:32
Other Links: manifest | tags
Context
2012-03-12
12:18
Make dictionary literals work. check-in: 6428a08de1 user: js tags: trunk
12:14
Add +[OFDictionary dictionaryWithObjects:forKeys:count:]. check-in: d42a56787b user: js tags: trunk
12:05
Make array literals work. check-in: a18e732a06 user: js tags: trunk
Changes

Modified src/OFDictionary.h from [d9d9397b7f] to [7d0cda50b6].

79
80
81
82
83
84
85












86
87
88
89
90
91
92
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104







+
+
+
+
+
+
+
+
+
+
+
+







 * \param keys An array of keys
 * \param objects An array of objects
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithObjects: (OFArray*)objects
		forKeys: (OFArray*)keys;

/**
 * \brief Creates a new OFDictionary with the specified keys and objects.
 *
 * \param keys An array of keys
 * \param objects An array of objects
 * \param count The number of objects in the arrays
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithObjects: (id*)objects
		forKeys: (id*)keys
		  count: (size_t)count;

/**
 * \brief Creates a new OFDictionary with the specified keys objects.
 *
 * \param firstKey The first key
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithKeysAndObjects: (id)firstKey, ...;
125
126
127
128
129
130
131













132
133
134
135
136
137
138
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163







+
+
+
+
+
+
+
+
+
+
+
+
+







 * \param keys An array of keys
 * \param objects An array of objects
 * \return A new initialized OFDictionary
 */
- initWithObjects: (OFArray*)objects
	  forKeys: (OFArray*)keys;

/**
 * \brief Initializes an already allocated OFDictionary with the specified keys
 *	  and objects.
 *
 * \param keys An array of keys
 * \param objects An array of objects
 * \param count The number of objects in the arrays
 * \return A new initialized OFDictionary
 */
- initWithObjects: (id*)objects
	  forKeys: (id*)keys
	    count: (size_t)count;

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

Modified src/OFDictionary.m from [29eff2abae] to [150bd32bb7].

55
56
57
58
59
60
61









62
63
64
65
66
67
68
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77







+
+
+
+
+
+
+
+
+








- initWithObjects: (OFArray*)objects
	  forKeys: (OFArray*)keys
{
	return (id)[[OFDictionary_hashtable alloc] initWithObjects: objects
							   forKeys: keys];
}

- initWithObjects: (id*)objects
	  forKeys: (id*)keys
	    count: (size_t)count
{
	return (id)[[OFDictionary_hashtable alloc] initWithObjects: objects
							   forKeys: keys
							     count: count];
}

- initWithKeysAndObjects: (id <OFCopying>)firstKey, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, firstKey);
142
143
144
145
146
147
148









149
150
151
152
153
154
155
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173







+
+
+
+
+
+
+
+
+








+ dictionaryWithObjects: (OFArray*)objects
		forKeys: (OFArray*)keys
{
	return [[[self alloc] initWithObjects: objects
				      forKeys: keys] autorelease];
}

+ dictionaryWithObjects: (id*)objects
		forKeys: (id*)keys
		  count: (size_t)count
{
	return [[[self alloc] initWithObjects: objects
				      forKeys: keys
					count: count] autorelease];
}

+ dictionaryWithKeysAndObjects: (id)firstKey, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, firstKey);
185
186
187
188
189
190
191










192
193
194
195
196
197
198
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226







+
+
+
+
+
+
+
+
+
+







{
	return [self initWithKeysAndObjects: key, object, nil];
}

- initWithObjects: (OFArray*)objects
	  forKeys: (OFArray*)keys
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithObjects: (id*)objects
	  forKeys: (id*)keys
	    count: (size_t)count
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithKeysAndObjects: (id)firstKey, ...

Modified src/OFDictionary_hashtable.m from [dc60bfa33c] to [fd15d2ff3e].

231
232
233
234
235
236
237






















238
239
240
241
242
243
244
245
246

247
248
249
250
251
252
253
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

263
264



265
266
267
268
269
270
271
272







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



-


-
-
-
+








	return self;
}

- initWithObjects: (OFArray*)objects
	  forKeys: (OFArray*)keys
{
	id ret;

	@try {
		if ([objects count] != [keys count])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa];

		ret = [self initWithObjects: [objects objects]
				    forKeys: [keys objects]
				      count: [objects count]];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return ret;
}

- initWithObjects: (id*)objects
	  forKeys: (id*)keys
	    count: (size_t)count_
{
	self = [super init];

	@try {
		id *objectsCArray, *keysCArray;
		uint32_t i, j, newSize;

		keysCArray = [keys objects];
		objectsCArray = [objects objects];
		count = [keys count];
		count = count_;

		if (count > UINT32_MAX)
			@throw [OFOutOfRangeException exceptionWithClass: isa];

		for (newSize = 1; newSize < count; newSize <<= 1);
		if (count * 4 / newSize >= 3)
			newSize <<= 1;
262
263
264
265
266
267
268
269

270
271
272
273
274

275
276
277
278
279
280
281
282

283
284
285
286
287
288
289

290
291
292
293
294
295
296
281
282
283
284
285
286
287

288
289
290
291
292

293
294
295
296
297
298
299
300

301

302
303
304
305
306

307
308
309
310
311
312
313
314







-
+




-
+







-
+
-





-
+







			data[j] = NULL;

		size = newSize;

		for (i = 0; i < count; i++) {
			uint32_t hash, last;

			hash = [keysCArray[i] hash];
			hash = [keys[i] hash];
			last = size;

			for (j = hash & (size - 1); j < last && data[j] != NULL;
			    j++)
				if ([data[j]->key isEqual: keysCArray[i]])
				if ([data[j]->key isEqual: keys[i]])
					break;

			/* In case the last bucket is already used */
			if (j >= last) {
				last = hash & (size - 1);

				for (j = 0; j < last && data[j] != NULL; j++)
					if ([data[j]->key
					if ([data[j]->key isEqual: keys[i]])
					    isEqual: keysCArray[i]])
						break;
			}

			/* Key not in dictionary */
			if (j >= last || data[j] == NULL ||
			    ![data[j]->key isEqual: keysCArray[i]]) {
			    ![data[j]->key isEqual: keys[i]]) {
				struct of_dictionary_hashtable_bucket *bucket;
				id key;

				last = size;

				j = hash & (size - 1);
				for (; j < last && data[j] != NULL; j++);
305
306
307
308
309
310
311
312

313
314
315

316
317
318
319
320
321
322
323
324
325
326
327
328
329

330
331

332
333
334
335
336
337
338
323
324
325
326
327
328
329

330
331
332

333
334
335
336
337
338
339
340
341
342
343
344
345
346

347
348

349
350
351
352
353
354
355
356







-
+


-
+













-
+

-
+








				if (j >= last)
					@throw [OFOutOfRangeException
					    exceptionWithClass: isa];

				bucket =
				    [self allocMemoryWithSize: sizeof(*bucket)];
				key = [keysCArray[i] copy];
				key = [keys[i] copy];

				bucket->key = key;
				bucket->object = [objectsCArray[i] retain];
				bucket->object = [objects[i] retain];
				bucket->hash = hash;

				data[j] = bucket;

				continue;
			}

			/*
			 * The key is already in the dictionary. However, we
			 * just replace it so that the programmer gets the same
			 * behavior as if he'd call setObject:forKey: for each
			 * key/object pair.
			 */
			[objectsCArray[i] retain];
			[objects[i] retain];
			[data[j]->object release];
			data[j]->object = objectsCArray[i];
			data[j]->object = objects[i];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;

Modified src/OFMutableDictionary.m from [f8225615a5] to [09218a8546].

50
51
52
53
54
55
56










57
58
59
60
61
62
63
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73







+
+
+
+
+
+
+
+
+
+







- initWithObjects: (OFArray*)objects
	  forKeys: (OFArray*)keys
{
	return (id)[[OFMutableDictionary_hashtable alloc]
	    initWithObjects: objects
		    forKeys: keys];
}

- initWithObjects: (id*)objects
	  forKeys: (id*)keys
	    count: (size_t)count
{
	return (id)[[OFMutableDictionary_hashtable alloc]
	    initWithObjects: objects
		    forKeys: keys
		      count: count];
}

- initWithKeysAndObjects: (id)firstKey, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, firstKey);