ObjFW  Diff

Differences From Artifact [c824cfaef7]:

To Artifact [3ddd956ea4]:

  • File src/OFDictionary.m — part of check-in [bbf1f79b8f] at 2009-09-08 16:06:10 on branch trunk — New OFDictionary implementation and removal of a hack in OFList.

    The new implementation is easier to use as it does automatic resizing,
    but therefore it's not realtime-capable anymore. The new implementation
    should also be a little bit faster.

    I decided to change the implementation as only very few need a
    realtime-capable dictionary and those few will most likely write their
    own implementation for their specific case anyway.

    As the new implementation no longer uses OFList, this also made it
    possible to remove a hack from OFList. (user: js, size: 5389) [annotate] [blame] [check-ins using]


14
15
16
17
18
19
20


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <string.h>

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



/* References for static linking */
void _references_to_categories_of_OFDictionary()
{
	_OFIterator_reference = 1;
}

@implementation OFDictionary
+ dictionary;
{
	return [[[self alloc] init] autorelease];
}

+ dictionaryWithDictionary: (OFDictionary*)dict
{
	return [[[self alloc] initWithDictionary: dict] autorelease];
}

+ dictionaryWithHashSize: (int)hashsize
{
	return [[[self alloc] initWithHashSize: hashsize] autorelease];
}

+ dictionaryWithObject: (OFObject*)obj
		forKey: (OFObject <OFCopying>*)key
{
	return [[[self alloc] initWithObject: obj
				      forKey: key] autorelease];
}








>
>

















<
<
<
<
<







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39





40
41
42
43
44
45
46
#include <string.h>

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

#define BUCKET_SIZE sizeof(struct of_dictionary_bucket)

/* References for static linking */
void _references_to_categories_of_OFDictionary()
{
	_OFIterator_reference = 1;
}

@implementation OFDictionary
+ dictionary;
{
	return [[[self alloc] init] autorelease];
}

+ dictionaryWithDictionary: (OFDictionary*)dict
{
	return [[[self alloc] initWithDictionary: dict] autorelease];
}






+ dictionaryWithObject: (OFObject*)obj
		forKey: (OFObject <OFCopying>*)key
{
	return [[[self alloc] initWithObject: obj
				      forKey: key] autorelease];
}

67
68
69
70
71
72
73
74
75
76
77
78
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
105
106
107
108
109
110
111
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
140
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
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
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
273
274
275
276
277
278
279
280
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
315
316
317
318
319
320
321
322
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434


435







436
437
438

439
440
441

442
443
444
445
446
447
448
449
450
	return ret;
}

- init
{
	self = [super init];

	size = 4096;

	@try {
		data = [self allocMemoryForNItems: size
					 withSize: sizeof(OFList*)];
	} @catch (OFException *e) {
		/*
		 * We can't use [super dealloc] on OS X here. Compiler bug?
		 * Anyway, set size to 0 so that [self dealloc] works.
		 */
		size = 0;
		[self dealloc];
		@throw e;
	}
	memset(data, 0, size * sizeof(OFList*));

	return self;
}

- initWithDictionary: (OFDictionary*)dict
{
	OFAutoreleasePool *pool;
	OFIterator *iter;
	of_iterator_pair_t pair;
	of_iterator_pair_t (*next)(id, SEL);

	self = [super init];

	if (dict == nil) {
		Class c = isa;
		size = 0;
		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}

	size = dict->size;

	@try {
		data = [self allocMemoryForNItems: size
					 withSize: sizeof(OFList*)];
		memset(data, 0, size * sizeof(OFList*));

		pool = [[OFAutoreleasePool alloc] init];
		iter = [dict iterator];
		next = (of_iterator_pair_t(*)(id, SEL))
		    [iter methodForSelector: @selector(nextKeyObjectPair)];
	} @catch (OFException *e) {
		/*
		 * We can't use [super dealloc] on OS X here. Compiler bug?
		 * Anyway, set size to 0 so that [self dealloc] works.

		 */
		size = 0;
		[self dealloc];
		@throw e;
	}

	for (;;) {
		uint32_t hash;
		OFObject <OFCopying> *key;

		pair = next(iter, @selector(nextKeyObjectPair));

		if (pair.key == nil || pair.object == nil)
			break;

		hash = pair.hash & (size - 1);

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

		@try {
			of_dictionary_list_object_t *o;

			if (data[hash] == nil)
				data[hash] = [[OFList alloc]
				    initWithListObjectSize:
				    sizeof(of_dictionary_list_object_t)];

			o = (of_dictionary_list_object_t*)
			    [data[hash] append: pair.object];
			o->key = key;
			o->hash = pair.hash;
		} @catch (OFException *e) {
			[key release];
			[self dealloc];
			@throw e;
		}
	}

	[pool release];

	return self;
}

- initWithHashSize: (int)hashsize
{
	self = [super init];

	if (hashsize < 8 || hashsize >= 28) {
		Class c = isa;
		[super dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}

	size = (size_t)1 << hashsize;

	@try {
		data = [self allocMemoryForNItems: size
					 withSize: sizeof(OFList*)];
	} @catch (OFException *e) {
		/*
		 * We can't use [super dealloc] on OS X here. Compiler bug?
		 * Anyway, set size to 0 so that [self dealloc] works.
		 */
		size = 0;
		[self dealloc];
		@throw e;
	}
	memset(data, 0, size * sizeof(OFList*));

	return self;
}

- initWithObject: (OFObject*)obj
	  forKey: (OFObject <OFCopying>*)key
{
	Class c;
	uint32_t fullhash, hash;

	self = [self init];

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

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

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

	@try {
		of_dictionary_list_object_t *o;

		data[hash] = [[OFList alloc] initWithListObjectSize:
		    sizeof(of_dictionary_list_object_t)];

		o = (of_dictionary_list_object_t*)[data[hash] append: obj];
		o->key = key;
		o->hash = fullhash;
	} @catch (OFException *e) {
		[key release];
		[self dealloc];
		@throw e;
	}

	return self;
}

- initWithObjects: (OFArray*)objs
	  forKeys: (OFArray*)keys
{
	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
						       selector: _cmd];
	}

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

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

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

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

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

		}

		@try {
			of_dictionary_list_object_t *o;

			if (data[hash] == nil)
				data[hash] = [[OFList alloc]
				    initWithListObjectSize:
				    sizeof(of_dictionary_list_object_t)];

			o = (of_dictionary_list_object_t*)
			    [data[hash] append: objs_data[i]];
			o->key = key;
			o->hash = fullhash;
		} @catch (OFException *e) {
			[key release];
			[self dealloc];
			@throw e;
		}
	}

	return self;
}

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

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

	return ret;
}

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

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

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

	fullhash = [first hash];
	hash = fullhash & (size - 1);

	@try {

		key = [first copy];

	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	}

	@try {
		of_dictionary_list_object_t *o;

		if (data[hash] == nil)
			data[hash] = [[OFList alloc] initWithListObjectSize:
			    sizeof(of_dictionary_list_object_t)];

		o = (of_dictionary_list_object_t*)[data[hash] append: obj];
		o->key = key;
		o->hash = fullhash;
	} @catch (OFException *e) {
		[key release];
		[self dealloc];
		@throw e;
	}

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

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

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

		@try {
			of_dictionary_list_object_t *o;

			if (data[hash] == nil)
				data[hash] = [[OFList alloc]
				    initWithListObjectSize:
				    sizeof(of_dictionary_list_object_t)];

			o = (of_dictionary_list_object_t*)
			    [data[hash] append: obj];
			o->key = key;
			o->hash = fullhash;
		} @catch (OFException *e) {
			[key release];
			[self dealloc];
			@throw e;
		}
	}

	return self;
}

- (float)averageItemsPerBucket
{
	size_t items, buckets, i;

	items = 0;
	buckets = 0;

	for (i = 0; i < size; i++) {
		if (data[i] != nil) {
			items += [data[i] count];
			buckets++;
		}
	}

	return (float)items / buckets;
}

- (id)objectForKey: (OFObject*)key
{
	uint32_t hash;
	of_dictionary_list_object_t *iter;

	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

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



	if (data[hash] == nil)







		return nil;

	for (iter = (of_dictionary_list_object_t*)[data[hash] first];

	    iter != NULL; iter = iter->next)
		if ([iter->key isEqual: key])
			return iter->object;


	return nil;
}

- (id)copy
{
	return [self retain];
}








|


|
<









|






<
<
|
<











<
<

|
|
<
<
<
<
<
<



|
>

<



|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
|
<
|
<
<
|
<
<
<
|
<
<
<
<
<
<
|
<
|
<
<
<
<
<
<
<
<
|
<
<

<







|
|



<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
|
<
<
<
<
<
<
<
<
<

<










<
<
|
|
>
>


<

<
<
<
<
<
<
|

|
<
<
<
<
<
<
<
<
<
<
<

<
<
|
<
|
<
|
|
>
|

|
<
|
<
<
<
<
|
<
<
<
<
|
<
|
|
<


















|


|
|
<
<


<

<
<
<
<
<
<
<
<
<
<

>
|
>





<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|

|
<





|

>
>
|
>
>
>
>
>
>
>


|
>
|
<
<
>
|
|







64
65
66
67
68
69
70
71
72
73
74

75
76
77
78
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
105






106
107
108
109
110
111

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

140
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
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
244
245
246
	return ret;
}

- init
{
	self = [super init];

	size = 1;

	@try {
		data = [self allocMemoryWithSize: BUCKET_SIZE];

	} @catch (OFException *e) {
		/*
		 * We can't use [super dealloc] on OS X here. Compiler bug?
		 * Anyway, set size to 0 so that [self dealloc] works.
		 */
		size = 0;
		[self dealloc];
		@throw e;
	}
	data[0].key = nil;

	return self;
}

- initWithDictionary: (OFDictionary*)dict
{


	size_t i;


	self = [super init];

	if (dict == nil) {
		Class c = isa;
		size = 0;
		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}



	@try {
		data = [self allocMemoryForNItems: dict->size
					 withSize: BUCKET_SIZE];






	} @catch (OFException *e) {
		/*
		 * We can't use [super dealloc] on OS X here. Compiler bug?
		 * Anyway, we didn't do anything yet anyway, so [self dealloc]
		 * works.
		 */

		[self dealloc];
		@throw e;
	}
	size = dict->size;




































	for (i = 0; i < size; i++) {

		if (dict->data[i].key != nil) {


			data[i].key = [dict->data[i].key copy];



			data[i].object = [dict->data[i].object retain];






			data[i].hash = dict->data[i].hash;

		} else








			data[i].key = nil;


	}


	return self;
}

- initWithObject: (OFObject*)obj
	  forKey: (OFObject <OFCopying>*)key
{
	const SEL sel = @selector(setObject:forKey:);
	IMP set = [OFMutableDictionary instanceMethodForSelector: sel];

	self = [self init];











	@try {





		set(self, sel, obj, key);









	} @catch (OFException *e) {

		[self dealloc];
		@throw e;
	}

	return self;
}

- initWithObjects: (OFArray*)objs
	  forKeys: (OFArray*)keys
{


	id *objs_data, *keys_data;
	size_t objs_count, i;
	const SEL sel = @selector(setObject:forKey:);
	IMP set = [OFMutableDictionary instanceMethodForSelector: sel];

	self = [self init];








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














	if (objs == nil || keys == nil || objs_count != [keys count]) {

		Class c = isa;

		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}

	@try {

		for (i = 0; i < objs_count; i++)




			set(self, sel, objs_data[i], keys_data[i]);




	} @catch (OFException *e) {

		[self dealloc];
		@throw e;

	}

	return self;
}

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

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

	return ret;
}

- initWithKey: (OFObject <OFCopying>*)key
      argList: (va_list)args
{
	const SEL sel = @selector(setObject:forKey:);
	IMP set = [OFMutableDictionary instanceMethodForSelector: sel];



	self = [self init];












	@try {
		set(self, sel, va_arg(args, OFObject*), key);
		while ((key = va_arg(args, OFObject <OFCopying>*)) != nil)
			set(self, sel, va_arg(args, OFObject*), key);
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	}






















































	return self;
}


















- (id)objectForKey: (OFObject <OFCopying>*)key
{
	uint32_t i, hash;


	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];

	for (i = hash & (size - 1); i < size && data[i].key != nil &&
	    ![data[i].key isEqual: key]; i++);

	/* In case the last bucket is already used */
	if (i >= size)
		for (i = 0; i < size && data[i].key != nil &&
		    ![data[i].key isEqual: key]; i++);

	/* Key not in dictionary */
	if (i >= size || data[i].key == nil)
		return nil;

	return data[i].object;
}



- (size_t)count
{
	return count;
}

- (id)copy
{
	return [self retain];
}

460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
*/

- (void)dealloc
{
	size_t i;

	for (i = 0; i < size; i++) {
		if (data[i] != nil) {
			of_dictionary_list_object_t *iter;

			for (iter = (of_dictionary_list_object_t*)
			    [data[i] first]; iter != NULL; iter = iter->next)
				[iter->key release];

			[data[i] release];
		}
	}

	[super dealloc];
}

- setObject: (OFObject*)obj
     forKey: (OFObject <OFCopying>*)key
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- removeObjectForKey: (OFObject*)key
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- changeHashSize: (int)hashsize
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}
@end







|
<
<
<
<
|
<
|















<
<
<
<
<
<




256
257
258
259
260
261
262
263




264

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280






281
282
283
284
*/

- (void)dealloc
{
	size_t i;

	for (i = 0; i < size; i++) {
		if (data[i].key != nil) {




			[data[i].key release];

			[data[i].object release];
		}
	}

	[super dealloc];
}

- setObject: (OFObject*)obj
     forKey: (OFObject <OFCopying>*)key
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- removeObjectForKey: (OFObject*)key
{






	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}
@end