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];
}
|