ObjFW  Check-in [da0d602dc8]

Overview
Comment:Style improvements in OF(Mutable)Dictionary.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: da0d602dc892c4b868d7cf781d6577ceab67f2dcaaa816cfa424cfc8872995f4
User & Date: js on 2011-04-22 16:53:21
Other Links: manifest | tags
Context
2011-04-22
18:00
More style improvements. check-in: 4814266280 user: js tags: trunk
16:53
Style improvements in OF(Mutable)Dictionary. check-in: da0d602dc8 user: js tags: trunk
16:31
Make OFCopying and OFComparing conform to OFObject.
Also make OFMutableCopying conform to OFCopying.
check-in: b46a3eccdb user: js tags: trunk
Changes

Modified src/OFDictionary.h from [ec25f9d317] to [e6a40ac98c].

19
20
21
22
23
24
25
26
27
28




29
30
31
32
33
34
35
19
20
21
22
23
24
25



26
27
28
29
30
31
32
33
34
35
36







-
-
-
+
+
+
+







#import "OFObject.h"
#import "OFCollection.h"
#import "OFEnumerator.h"

@class OFArray;

#ifdef OF_HAVE_BLOCKS
typedef void (^of_dictionary_enumeration_block_t)(id key, id obj, BOOL *stop);
typedef BOOL (^of_dictionary_filter_block_t)(id key, id obj);
typedef id (^of_dictionary_map_block_t)(id key, id obj);
typedef void (^of_dictionary_enumeration_block_t)(id key, id object,
     BOOL *stop);
typedef BOOL (^of_dictionary_filter_block_t)(id key, id object);
typedef id (^of_dictionary_map_block_t)(id key, id object);
#endif

struct of_dictionary_bucket
{
	id <OFCopying> key;
	id object;
	uint32_t hash;
52
53
54
55
56
57
58
59

60
61
62

63
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
53
54
55
56
57
58
59

60
61
62

63
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







-
+


-
+





-
+


-
+






-
+


-
+





-
+


-
+












-
+


-
+






-
+


-
+







-
+


-
+






-
+


-
+





-
-
+
+


-
-
+
+







 * \return A new autoreleased OFDictionary
 */
+ dictionary;

/**
 * Creates a new OFDictionary with the specified dictionary.
 *
 * \param dict An OFDictionary
 * \param dictionary An OFDictionary
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithDictionary: (OFDictionary*)dict;
+ dictionaryWithDictionary: (OFDictionary*)dictionary;

/**
 * Creates a new OFDictionary with the specified key and object.
 *
 * \param key The key
 * \param obj The object
 * \param object The object
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithObject: (id)obj
+ dictionaryWithObject: (id)object
		forKey: (id <OFCopying>)key;

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

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

/**
 * Initializes an already allocated OFDictionary.
 *
 * \return An initialized OFDictionary
 */
- init;

/**
 * Initializes an already allocated OFDictionary with the specified
 * OFDictionary.
 *
 * \param dict An OFDictionary
 * \param dictionary An OFDictionary
 * \return An initialized OFDictionary
 */
- initWithDictionary: (OFDictionary*)dict;
- initWithDictionary: (OFDictionary*)dictionary;

/**
 * Initializes an already allocated OFDictionary with the specified key and
 * object.
 *
 * \param key The key
 * \param obj The object
 * \param object The object
 * \return A new initialized OFDictionary
 */
- initWithObject: (id)obj
- initWithObject: (id)object
	  forKey: (id <OFCopying>)key;

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

/**
 * Initializes an already allocated OFDictionary with the specified keys and
 * objects.
 *
 * \param first The first key
 * \param firstKey The first key
 * \return A new initialized OFDictionary
 */
- initWithKeysAndObjects: (id <OFCopying>)first, ...;
- initWithKeysAndObjects: (id <OFCopying>)firstKey, ...;

/**
 * Initializes an already allocated OFDictionary with the specified key and
 * va_list.
 *
 * \param first The first key
 * \param args A va_list of the other arguments
 * \param firstKey The first key
 * \param arguments A va_list of the other arguments
 * \return A new initialized OFDictionary
 */
- initWithKey: (id <OFCopying>)first
      argList: (va_list)args;
-  initWithKey: (id <OFCopying>)firstKey
  argumentList: (va_list)arguments;

/**
 * Returns the object for the given key or nil if the key was not found.
 *
 * The returned object is <i>not</i> retained and autoreleased for performance
 * reasons!
 *

Modified src/OFDictionary.m from [69df15df8e] to [d2ad80013b].

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

45
46

47
48
49

50
51
52

53
54
55
56

57
58
59

60
61
62
63

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
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
451
452
453

454
455
456
457

458
459
460
461
462
463
464
28
29
30
31
32
33
34

35
36
37
38
39
40
41
42

43
44

45
46
47

48
49
50

51
52
53
54

55
56
57

58
59
60
61

62
63
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
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

451
452
453
454
455

456
457
458
459

460
461
462
463
464
465
466
467







-








-
+

-
+


-
+


-
+



-
+


-
+



-
+


-
+

-
-
-
-
+
+
+
+









-
+
+










-
+






-
+



-
-
+
+

-
+


-
-
+
+



-
+

+
-
+


-
-
+
+


-
+

-
+



-
-
-
+
+
+

-
+









-
+






-
+


-
+





-
-
+
+



-
+

-
+



-
-
-
+
+
+

-
+









-
+





-
-
+
+

-
-
+
+





-
+

-
+


-
-
+
+

-
+


-
+




-
+




-
+







-
-
+
+





-
-
+
+



















+
-
-
+
+


-
+

-
+



-
-
-
+
+
+

-
+










-
+




-
+



-
+









-
+


-
+

-
-
-
-
+
+
+
+




-
-
+
+




-
-
-
-
+
+
+
+

-
+





-
+




-
+





-
+

-
+


-
-
+
+

-
+


-
+


-
+


-
+



-
+

-
+



-
-
-
+
+
+

-
+




-
-
+
+

-
+




-
+




-
+







-
+





-
+

















+
-
+



-
+

-
+



-
-
-
+
+
+

-
+










-
+




-
+



-
+







#import "OFInvalidArgumentException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

struct of_dictionary_bucket of_dictionary_deleted_bucket = {};

#define BUCKET struct of_dictionary_bucket
#define DELETED &of_dictionary_deleted_bucket

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

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

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

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

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

	va_start(args, first);
	ret = [[[self alloc] initWithKey: first
				 argList: args] autorelease];
	va_end(args);
	va_start(arguments, firstKey);
	ret = [[[self alloc] initWithKey: firstKey
			    argumentList: arguments] autorelease];
	va_end(arguments);

	return ret;
}

- init
{
	self = [super init];

	@try {
		data = [self allocMemoryWithSize: sizeof(BUCKET*)];
		data = [self allocMemoryWithSize:
		    sizeof(struct of_dictionary_bucket)];
		size = 1;
		data[0] = NULL;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithDictionary: (OFDictionary*)dict
- initWithDictionary: (OFDictionary*)dictionary
{
	self = [super init];

	@try {
		uint32_t i;

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

		data = [self allocMemoryForNItems: dict->size
					 withSize: sizeof(BUCKET*)];
		data = [self allocMemoryForNItems: dictionary->size
					 withSize: sizeof(*data)];

		for (i = 0; i < dict->size; i++)
		for (i = 0; i < dictionary->size; i++)
			data[i] = NULL;

		size = dict->size;
		count = dict->count;
		size = dictionary->size;
		count = dictionary->count;

		for (i = 0; i < size; i++) {
			id <OFCopying> key;
			BUCKET *b;
			struct of_dictionary_bucket *bucket;

			if (dictionary->data[i] == NULL ||
			if (dict->data[i] == NULL || dict->data[i] == DELETED)
			    dictionary->data[i] == DELETED)
				continue;

			b = [self allocMemoryWithSize: sizeof(BUCKET)];
			key = [dict->data[i]->key copy];
			bucket = [self allocMemoryWithSize: sizeof(*bucket)];
			key = [dictionary->data[i]->key copy];

			@try {
				[dict->data[i]->object retain];
				[dictionary->data[i]->object retain];
			} @catch (id e) {
				[(id)key release];
				[key release];
				@throw e;
			}

			b->key = key;
			b->object = dict->data[i]->object;
			b->hash = dict->data[i]->hash;
			bucket->key = key;
			bucket->object = dictionary->data[i]->object;
			bucket->hash = dictionary->data[i]->hash;

			data[i] = b;
			data[i] = bucket;
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObject: (id)obj
- initWithObject: (id)object
	  forKey: (id <OFCopying>)key
{
	self = [super init];

	@try {
		uint32_t i;
		BUCKET *b;
		struct of_dictionary_bucket *bucket;

		data = [self allocMemoryForNItems: 2
					 withSize: sizeof(BUCKET*)];
					 withSize: sizeof(*bucket)];

		size = 2;
		for (i = 0; i < size; i++)
			data[i] = NULL;

		i = [(id)key hash] & 1;
		b = [self allocMemoryWithSize: sizeof(BUCKET)];
		i = [key hash] & 1;
		bucket = [self allocMemoryWithSize: sizeof(*bucket)];
		key = [key copy];

		@try {
			[obj retain];
			[object retain];
		} @catch (id e) {
			[(id)key release];
			[key release];
			@throw e;
		}

		b->key = key;
		b->object = obj;
		b->hash = [(id)key hash];
		bucket->key = key;
		bucket->object = object;
		bucket->hash = [key hash];

		data[i] = b;
		data[i] = bucket;
		count = 1;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObjects: (OFArray*)objs
- initWithObjects: (OFArray*)objects
	  forKeys: (OFArray*)keys
{
	self = [super init];

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

		keys_carray = [keys cArray];
		objs_carray = [objs cArray];
		keysCArray = [keys cArray];
		objectsCArray = [objects cArray];
		count = [keys count];

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

		for (nsize = 1; nsize < count; nsize <<= 1);
		for (newSize = 1; newSize < count; newSize <<= 1);

		if (nsize == 0)
		if (newSize == 0)
			@throw [OFOutOfRangeException newWithClass: isa];

		data = [self allocMemoryForNItems: nsize
					 withSize: sizeof(BUCKET*)];
		data = [self allocMemoryForNItems: newSize
					 withSize: sizeof(*data)];

		for (j = 0; j < nsize; j++)
		for (j = 0; j < newSize; j++)
			data[j] = NULL;

		size = nsize;
		size = newSize;

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

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

			for (j = hash & (size - 1); j < last && data[j] != NULL;
			    j++)
				if ([(id)data[j]->key isEqual: keys_carray[i]])
				if ([data[j]->key isEqual: keysCArray[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 ([(id)data[j]->key
					    isEqual: keys_carray[i]])
					if ([data[j]->key
					    isEqual: keysCArray[i]])
						break;
			}

			/* Key not in dictionary */
			if (j >= last || data[j] == NULL ||
			    ![(id)data[j]->key isEqual: keys_carray[i]]) {
				BUCKET *b;
			    ![data[j]->key isEqual: keysCArray[i]]) {
				struct of_dictionary_bucket *bucket;
				id <OFCopying> key;

				last = size;

				j = hash & (size - 1);
				for (; j < last && data[j] != NULL; j++);

				/* 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 (j >= last)
					@throw [OFOutOfRangeException
					    newWithClass: isa];

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

				@try {
					[objs_carray[i] retain];
					[objectsCArray[i] retain];
				} @catch (id e) {
					[(id)key release];
					[key release];
					@throw e;
				}

				b->key = key;
				b->object = objs_carray[i];
				b->hash = hash;
				bucket->key = key;
				bucket->object = objectsCArray[i];
				bucket->hash = hash;

				data[j] = b;
				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.
			 */
			[objs_carray[i] retain];
			[objectsCArray[i] retain];

			@try {
				[data[j]->object release];
			} @catch (id e) {
				[objs_carray[i] release];
				[objectsCArray[i] release];
				@throw e;
			}

			data[j]->object = objs_carray[i];
			data[j]->object = objectsCArray[i];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

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

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

	return ret;
}

- initWithKey: (id <OFCopying>)key
      argList: (va_list)args
-  initWithKey: (id <OFCopying>)key
  argumentList: (va_list)arguments
{
	self = [super init];

	@try {
		id obj;
		uint32_t i, j, hash, nsize;
		va_list args2;
		BUCKET *b;
		id object;
		uint32_t i, j, hash, newSize;
		va_list argumentsCopy;
		struct of_dictionary_bucket *bucket;

		va_copy(args2, args);
		va_copy(argumentsCopy, arguments);

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

		if ((obj = va_arg(args, id)) == nil)
		if ((object = va_arg(arguments, id)) == nil)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		count = 1;
		for (; va_arg(args2, id) != nil; count++);
		for (; va_arg(argumentsCopy, id) != nil; count++);
		count >>= 1;

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

		for (nsize = 1; nsize < count; nsize <<= 1);
		for (newSize = 1; newSize < count; newSize <<= 1);

		if (nsize == 0)
		if (newSize == 0)
			@throw [OFOutOfRangeException newWithClass: isa];

		data = [self allocMemoryForNItems: nsize
					 withSize: sizeof(BUCKET*)];
		data = [self allocMemoryForNItems: newSize
					 withSize: sizeof(*data)];

		for (j = 0; j < nsize; j++)
		for (j = 0; j < newSize; j++)
			data[j] = NULL;

		size = nsize;
		size = newSize;

		/* Add first key / object pair */
		hash = [(id)key hash];
		hash = [key hash];
		j = hash & (size - 1);

		b = [self allocMemoryWithSize: sizeof(BUCKET)];
		bucket = [self allocMemoryWithSize: sizeof(*bucket)];
		key = [key copy];

		@try {
			[obj retain];
			[object retain];
		} @catch (id e) {
			[(id)key release];
			[key release];
			@throw e;
		}

		b->key = key;
		b->object = obj;
		b->hash = hash;
		bucket->key = key;
		bucket->object = object;
		bucket->hash = hash;

		data[j] = b;
		data[j] = bucket;

		for (i = 1; i < count; i++) {
			uint32_t last;

			key = va_arg(args, id <OFCopying>);
			obj = va_arg(args, id);
			key = va_arg(arguments, id <OFCopying>);
			object = va_arg(arguments, id);

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

			hash = [(id)key hash];
			hash = [key hash];
			last = size;

			for (j = hash & (size - 1); j < last && data[j] != NULL;
			    j++)
				if ([(id)data[j]->key isEqual: key])
				if ([data[j]->key isEqual: key])
					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 ([(id)data[j]->key isEqual: key])
					if ([data[j]->key isEqual: key])
						break;
			}

			/* Key not in dictionary */
			if (j >= last || data[j] == NULL ||
			    ![(id)data[j]->key isEqual: key]) {
			    ![data[j]->key isEqual: key]) {
				last = size;

				j = hash & (size - 1);
				for (; j < last && data[j] != NULL; j++);

				/* 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 (j >= last)
					@throw [OFOutOfRangeException
					    newWithClass: isa];

				bucket =
				b = [self allocMemoryWithSize: sizeof(BUCKET)];
				    [self allocMemoryWithSize: sizeof(*bucket)];
				key = [key copy];

				@try {
					[obj retain];
					[object retain];
				} @catch (id e) {
					[(id)key release];
					[key release];
					@throw e;
				}

				b->key = key;
				b->object = obj;
				b->hash = hash;
				bucket->key = key;
				bucket->object = object;
				bucket->hash = hash;

				data[j] = b;
				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.
			 */
			[obj retain];
			[object retain];

			@try {
				[data[j]->object release];
			} @catch (id e) {
				[obj release];
				[object release];
				@throw e;
			}

			data[j]->object = obj;
			data[j]->object = object;
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
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
500
501
502
503
478
479
480
481
482
483
484

485
486
487
488
489
490
491
492
493
494
495
496
497
498

499
500
501
502
503
504
505
506







-
+













-
+







	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([(id)data[i]->key isEqual: key])
		if ([data[i]->key isEqual: key])
			return data[i]->object;
	}

	if (i < last)
		return nil;

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

	for (i = 0; i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([(id)data[i]->key isEqual: key])
		if ([data[i]->key isEqual: key])
			return data[i]->object;
	}

	return nil;
}

- (size_t)count
618
619
620
621
622
623
624
625

626
627
628
629
630
631


632
633
634
635
636
637
638
639


640
641
642
643
644
645

646
647
648
649
650
651
652


653
654
655
656
657
658
659
660


661
662
663
664
665
666
667
668
669
670

671
672
673
674
675
676
677
621
622
623
624
625
626
627

628
629
630
631
632


633
634
635
636
637
638
639
640


641
642
643
644
645
646
647

648
649
650
651
652
653


654
655
656
657
658
659
660
661


662
663
664
665
666
667
668
669
670
671
672

673
674
675
676
677
678
679
680







-
+




-
-
+
+






-
-
+
+





-
+





-
-
+
+






-
-
+
+









-
+







	}

	[pool release];
}

- (OFDictionary*)mappedDictionaryUsingBlock: (of_dictionary_map_block_t)block
{
	OFMutableDictionary *dict = [OFMutableDictionary dictionary];
	OFMutableDictionary *new = [OFMutableDictionary dictionary];
	size_t i;

	for (i = 0; i < size; i++)
		if (data[i] != NULL && data[i] != DELETED)
			[dict setObject: block(data[i]->key, data[i]->object)
				 forKey: data[i]->key];
			[new setObject: block(data[i]->key, data[i]->object)
				forKey: data[i]->key];

	/*
	 * Class swizzle the dictionary to be immutable. We declared the return
	 * type to be OFDictionary*, so it can't be modified anyway. But not
	 * swizzling it would create a real copy each time -[copy] is called.
	 */
	dict->isa = [OFDictionary class];
	return dict;
	new->isa = [OFDictionary class];
	return new;
}

- (OFDictionary*)filteredDictionaryUsingBlock:
    (of_dictionary_filter_block_t)block
{
	OFMutableDictionary *dict = [OFMutableDictionary dictionary];
	OFMutableDictionary *new = [OFMutableDictionary dictionary];
	size_t i;

	for (i = 0; i < size; i++)
		if (data[i] != NULL && data[i] != DELETED)
			if (block(data[i]->key, data[i]->object))
				[dict setObject: data[i]->object
					 forKey: data[i]->key];
				[new setObject: data[i]->object
					forKey: data[i]->key];

	/*
	 * Class swizzle the dictionary to be immutable. We declared the return
	 * type to be OFDictionary*, so it can't be modified anyway. But not
	 * swizzling it would create a real copy each time -[copy] is called.
	 */
	dict->isa = [OFDictionary class];
	return dict;
	new->isa = [OFDictionary class];
	return new;
}
#endif

- (void)dealloc
{
	uint32_t i;

	for (i = 0; i < size; i++) {
		if (data[i] != NULL && data[i] != DELETED) {
			[(id)data[i]->key release];
			[data[i]->key release];
			[data[i]->object release];
		}
	}

	[super dealloc];
}

Modified src/OFMutableDictionary.h from [c2a4a5e1d5] to [fcd7b9f568].

13
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
13
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







-
+















-
+

-
+







 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFDictionary.h"

#ifdef OF_HAVE_BLOCKS
typedef id (^of_dictionary_replace_block_t)(id key, id obj, BOOL *stop);
typedef id (^of_dictionary_replace_block_t)(id key, id object, BOOL *stop);
#endif

/**
 * \brief A class for using mutable hash tables.
 */
@interface OFMutableDictionary: OFDictionary
{
	unsigned long mutations;
}

/**
 * Sets an object for a key.
 * A key can be any object.
 *
 * \param key The key to set
 * \param obj The object to set the key to
 * \param object The object to set the key to
 */
- (void)setObject: (id)obj
- (void)setObject: (id)object
	   forKey: (id <OFCopying>)key;

/**
 * Remove the object with the given key from the dictionary.
 *
 * \param key The key whose object should be removed
 */

Modified src/OFMutableDictionary.m from [2759ebe7dd] to [0467bbb9de].

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
50

51
52
53
54


55
56
57


58
59
60
61
62
63

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
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
50
51


52
53
54


55
56
57
58
59
60
61

62
63


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







-



-
+

-
-
-
+
+
+

-
+


-
-
-
-
+
+
+
+



-
+


-
-
+
+

-
-
+
+





-
+

-
-
+
+



-
+


-
+



-
+




-
+




-
-
+
+


-
+





-
+



-
+






-
+











-
+






-
-
+
+




















-
+




-
+




-
+

-
-
+
+



-
-
-
-
+
+
+
+






-
+


















-
-
+
+








#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

#define BUCKET struct of_dictionary_bucket
#define DELETED &of_dictionary_deleted_bucket

@implementation OFMutableDictionary
- (void)_resizeForCount: (size_t)newcount
- (void)_resizeForCount: (size_t)newCount
{
	size_t fill = newcount * 4 / size;
	struct of_dictionary_bucket **newdata;
	uint32_t i, newsize;
	size_t fullness = newCount * 4 / size;
	struct of_dictionary_bucket **newData;
	uint32_t i, newSize;

	if (newcount > UINT32_MAX)
	if (newCount > UINT32_MAX)
		@throw [OFOutOfRangeException newWithClass: isa];

	if (fill >= 3)
		newsize = size << 1;
	else if (fill <= 1)
		newsize = size >> 1;
	if (fullness >= 3)
		newSize = size << 1;
	else if (fullness <= 1)
		newSize = size >> 1;
	else
		return;

	if (newsize == 0)
	if (newSize == 0)
		@throw [OFOutOfRangeException newWithClass: isa];

	newdata = [self allocMemoryForNItems: newsize
				    withSize: sizeof(BUCKET*)];
	newData = [self allocMemoryForNItems: newSize
				    withSize: sizeof(*newData)];

	for (i = 0; i < newsize; i++)
		newdata[i] = NULL;
	for (i = 0; i < newSize; i++)
		newData[i] = NULL;

	for (i = 0; i < size; i++) {
		if (data[i] != NULL && data[i] != DELETED) {
			uint32_t j, last;

			last = newsize;
			last = newSize;

			j = data[i]->hash & (newsize - 1);
			for (; j < last && newdata[j] != NULL; j++);
			j = data[i]->hash & (newSize - 1);
			for (; j < last && newData[j] != NULL; j++);

			/* In case the last bucket is already used */
			if (j >= last) {
				last = data[i]->hash & (newsize - 1);
				last = data[i]->hash & (newSize - 1);

				for (j = 0; j < last &&
				    newdata[j] != NULL; j++);
				    newData[j] != NULL; j++);
			}

			if (j >= last) {
				[self freeMemory: newdata];
				[self freeMemory: newData];
				@throw [OFOutOfRangeException
				    newWithClass: isa];
			}

			newdata[j] = data[i];
			newData[j] = data[i];
		}
	}

	[self freeMemory: data];
	data = newdata;
	size = newsize;
	data = newData;
	size = newSize;
}

- (void)setObject: (id)obj
- (void)setObject: (id)object
	   forKey: (id <OFCopying>)key
{
	uint32_t i, hash, last;
	id old;

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

	hash = [(id)key hash];
	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([(id)data[i]->key isEqual: key])
		if ([data[i]->key isEqual: key])
			break;
	}

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

		for (i = 0; i < last && data[i] != NULL; i++) {
			if (data[i] == DELETED)
				continue;

			if ([(id)data[i]->key isEqual: key])
			if ([data[i]->key isEqual: key])
				break;
		}
	}

	/* Key not in dictionary */
	if (i >= last || data[i] == NULL || data[i] == DELETED ||
	    ![(id)data[i]->key isEqual: key]) {
		BUCKET *b;
	    ![data[i]->key isEqual: key]) {
		struct of_dictionary_bucket *bucket;

		[self _resizeForCount: count + 1];

		mutations++;
		last = size;

		for (i = hash & (size - 1); i < last && data[i] != NULL &&
		    data[i] != DELETED; i++);

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

			for (i = 0; i < last && data[i] != NULL &&
			    data[i] != DELETED; i++);
		}

		if (i >= last)
			@throw [OFOutOfRangeException newWithClass: isa];

		b = [self allocMemoryWithSize: sizeof(BUCKET)];
		bucket = [self allocMemoryWithSize: sizeof(*bucket)];

		@try {
			key = [key copy];
		} @catch (id e) {
			[self freeMemory: b];
			[self freeMemory: bucket];
			@throw e;
		}

		@try {
			[obj retain];
			[object retain];
		} @catch (id e) {
			[self freeMemory: b];
			[(id)key release];
			[self freeMemory: bucket];
			[key release];
			@throw e;
		}

		b->key = key;
		b->object = obj;
		b->hash = hash;
		data[i] = b;
		bucket->key = key;
		bucket->object = object;
		bucket->hash = hash;
		data[i] = bucket;
		count++;

		return;
	}

	old = data[i]->object;
	data[i]->object = [obj retain];
	data[i]->object = [object retain];
	[old release];
}

- (void)removeObjectForKey: (id)key
{
	uint32_t i, hash, last;

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

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([(id)data[i]->key isEqual: key]) {
			[(id)data[i]->key release];
		if ([data[i]->key isEqual: key]) {
			[data[i]->key release];
			[data[i]->object release];
			[self freeMemory: data[i]];
			data[i] = DELETED;

			count--;
			mutations++;
			[self _resizeForCount: count];
212
213
214
215
216
217
218
219
220


221
222
223
224
225
226
227
211
212
213
214
215
216
217


218
219
220
221
222
223
224
225
226







-
-
+
+







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

	for (i = 0; i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([(id)data[i]->key isEqual: key]) {
			[(id)data[i]->key release];
		if ([data[i]->key isEqual: key]) {
			[data[i]->key release];
			[data[i]->object release];
			[self freeMemory: data[i]];
			data[i] = DELETED;

			count--;
			mutations++;
			[self _resizeForCount: count];