12
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
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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
12
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
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
|
-
-
-
+
-
-
-
+
+
+
+
-
-
+
-
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
-
-
+
+
+
+
-
+
-
-
+
+
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
+
-
-
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
-
-
-
+
+
+
+
-
+
-
-
-
-
+
-
-
-
-
-
-
+
-
-
+
-
-
-
-
+
-
-
-
-
+
+
+
-
-
-
-
-
+
-
-
-
+
-
-
-
+
-
-
-
-
+
-
-
-
+
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
-
+
-
-
-
+
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
+
-
-
-
+
-
-
+
+
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <string.h>
#include <assert.h>
#import "OFDictionary_hashtable.h"
#import "OFMutableDictionary_hashtable.h"
#import "OFEnumerator.h"
#import "OFMapTable.h"
#import "OFArray.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfRangeException.h"
#import "autorelease.h"
#import "macros.h"
static void*
copy(void *value)
{
struct of_dictionary_hashtable_bucket
of_dictionary_hashtable_deleted_bucket = {};
return [(id)value copy];
#define DELETED &of_dictionary_hashtable_deleted_bucket
@implementation OFDictionary_hashtable
- init
}
static void*
retain(void *value)
{
self = [super init];
@try {
return [(id)value retain];
}
data = [self allocMemoryWithSize: sizeof(*data)];
size = 1;
data[0] = NULL;
} @catch (id e) {
[self release];
static void
release(void *value)
{
[(id)value release];
@throw e;
}
}
static uint32_t
hash(void *value)
{
return self;
return [(id)value hash];
}
- OF_initWithDictionary: (OFDictionary*)dictionary
copyKeys: (BOOL)copyKeys
static BOOL
equal(void *value1, void *value2)
{
self = [super init];
return [(id)value1 isEqual: (id)value2];
@try {
uint32_t i;
OFDictionary_hashtable *hashtable;
}
if (![dictionary isKindOfClass:
[OFDictionary_hashtable class]] &&
![dictionary isKindOfClass:
[OFMutableDictionary_hashtable class]])
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]
selector: _cmd];
hashtable = (OFDictionary_hashtable*)dictionary;
data = [self allocMemoryWithSize: sizeof(*data)
count: hashtable->size];
static of_map_table_functions_t keyFunctions = {
.retain = copy,
.release = release,
.hash = hash,
.equal = equal
for (i = 0; i < hashtable->size; i++)
data[i] = NULL;
size = hashtable->size;
count = hashtable->count;
};
static of_map_table_functions_t valueFunctions = {
.retain = retain,
.release = release,
.hash = hash,
.equal = equal
};
for (i = 0; i < size; i++) {
struct of_dictionary_hashtable_bucket *bucket;
@implementation OFDictionary_hashtable
- init
{
if (hashtable->data[i] == NULL ||
hashtable->data[i] == DELETED)
continue;
bucket = [self allocMemoryWithSize: sizeof(*bucket)];
return [self initWithCapacity: 0];
}
bucket->key = (copyKeys
? [hashtable->data[i]->key copy]
: [hashtable->data[i]->key retain]);
bucket->object = [hashtable->data[i]->object retain];
bucket->hash = hashtable->data[i]->hash;
data[i] = bucket;
}
- initWithCapacity: (size_t)capacity
{
self = [super init];
@try {
mapTable = [[OFMapTable alloc]
initWithKeyFunctions: keyFunctions
valueFunctions: valueFunctions
capacity: capacity];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithDictionary: (OFDictionary*)dictionary
{
size_t count;
if (dictionary == nil)
return [self init];
if ([dictionary class] == [OFDictionary_hashtable class] ||
[dictionary class] == [OFMutableDictionary_hashtable class])
[dictionary class] == [OFMutableDictionary_hashtable class]) {
return [self OF_initWithDictionary: dictionary
copyKeys: YES];
self = [super init];
self = [super init];
@try {
void *pool;
@try {
OFDictionary_hashtable *dictionary_ =
(OFDictionary_hashtable*)dictionary;
OFEnumerator *enumerator;
id key;
uint32_t i, newSize;
if (dictionary == nil)
mapTable = [dictionary_->mapTable copy];
} @catch (id e) {
[self release];
@throw e;
}
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]];
return self;
}
@try {
count = [dictionary count];
} @catch (id e) {
if (count > UINT32_MAX)
@throw [OFOutOfRangeException
exceptionWithClass: [self class]];
[self release];
for (newSize = 1; newSize < count; newSize <<= 1);
if (count * 4 / newSize >= 3)
newSize <<= 1;
if (newSize == 0)
@throw [OFOutOfRangeException
@throw e;
exceptionWithClass: [self class]];
}
data = [self allocMemoryWithSize: sizeof(*data)
count: newSize];
for (i = 0; i < newSize; i++)
data[i] = NULL;
self = [self initWithCapacity: count];
size = newSize;
pool = objc_autoreleasePoolPush();
@try {
void *pool = objc_autoreleasePoolPush();
OFEnumerator *keyEnumerator, *objectEnumerator;
enumerator = [dictionary keyEnumerator];
while ((key = [enumerator nextObject]) != nil) {
uint32_t hash, last;
struct of_dictionary_hashtable_bucket *bucket;
id object;
id key, object;
hash = [key hash];
last = size;
keyEnumerator = [dictionary keyEnumerator];
for (i = hash & (size - 1); i < last && data[i] != NULL;
i++);
objectEnumerator = [dictionary objectEnumerator];
/* In case the last bucket is already used */
if (i >= last) {
last = hash & (size - 1);
while ((key = [keyEnumerator nextObject]) != nil &&
for (i = 0; i < last && data[i] != NULL; i++);
}
(object = [objectEnumerator nextObject]) != nil)
if (data[i] != NULL)
@throw [OFOutOfRangeException
exceptionWithClass: [self class]];
[mapTable setValue: object
bucket = [self allocMemoryWithSize: sizeof(*bucket)];
object = [dictionary objectForKey: key];
forKey: key];
bucket->key = [key copy];
bucket->object = [object retain];
bucket->hash = hash;
data[i] = bucket;
}
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithObject: (id)object
forKey: (id)key
{
self = [super init];
self = [self initWithCapacity: 1];
@try {
uint32_t i;
struct of_dictionary_hashtable_bucket *bucket;
[mapTable setValue: object
if (key == nil || object == nil)
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]
selector: _cmd];
forKey: key];
data = [self allocMemoryWithSize: sizeof(*data)
count: 2];
size = 2;
for (i = 0; i < size; i++)
data[i] = NULL;
i = [key hash] & 1;
bucket = [self allocMemoryWithSize: sizeof(*bucket)];
bucket->key = [key copy];
bucket->object = [object retain];
bucket->hash = [key hash];
data[i] = bucket;
count = 1;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithObjects: (OFArray*)objects
forKeys: (OFArray*)keys
{
id ret;
@try {
if ([objects count] != [keys count])
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]];
ret = [self initWithObjects: [objects objects]
forKeys: [keys objects]
count: [objects count]];
} @catch (id e) {
[self release];
@throw e;
}
return ret;
}
- initWithObjects: (id const*)objects
forKeys: (id const*)keys
count: (size_t)count_
count: (size_t)count
{
self = [super init];
self = [self initWithCapacity: count];
@try {
uint32_t i, j, newSize;
size_t i;
count = count_;
if (count > UINT32_MAX)
@throw [OFOutOfRangeException
exceptionWithClass: [self class]];
for (newSize = 1; newSize < count; newSize <<= 1);
if (count * 4 / newSize >= 3)
newSize <<= 1;
if (newSize == 0)
@throw [OFOutOfRangeException
exceptionWithClass: [self class]];
data = [self allocMemoryWithSize: sizeof(*data)
count: newSize];
for (j = 0; j < newSize; j++)
data[j] = NULL;
size = newSize;
for (i = 0; i < count; i++) {
for (i = 0; i < count; i++)
uint32_t hash, last;
[mapTable setValue: objects[i]
if (keys[i] == nil || objects[i] == nil)
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]];
hash = [keys[i] hash];
last = size;
for (j = hash & (size - 1); j < last && data[j] != NULL;
j++)
if ([data[j]->key isEqual: keys[i]])
forKey: 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 isEqual: keys[i]])
break;
}
/* Key not in dictionary */
if (j >= last || data[j] == NULL ||
![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++);
/* 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
exceptionWithClass: [self class]];
bucket =
[self allocMemoryWithSize: sizeof(*bucket)];
key = [keys[i] copy];
bucket->key = key;
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.
*/
[objects[i] retain];
[data[j]->object release];
data[j]->object = objects[i];
}
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithKey: (id)firstKey
arguments: (va_list)arguments
{
self = [super init];
@try {
va_list argumentsCopy;
id key, object;
uint32_t i, j, hash, newSize;
size_t i, count;
va_list argumentsCopy;
struct of_dictionary_hashtable_bucket *bucket;
va_copy(argumentsCopy, arguments);
if (firstKey == nil)
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]
selector: _cmd];
key = firstKey;
if ((object = va_arg(arguments, id)) == nil)
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]
selector: _cmd];
count = 1;
for (; va_arg(argumentsCopy, id) != nil; count++);
count >>= 1;
if (count > UINT32_MAX)
@throw [OFOutOfRangeException
exceptionWithClass: [self class]];
mapTable = [[OFMapTable alloc]
for (newSize = 1; newSize < count; newSize <<= 1);
if (count * 4 / newSize >= 3)
newSize <<= 1;
initWithKeyFunctions: keyFunctions
if (newSize == 0)
@throw [OFOutOfRangeException
exceptionWithClass: [self class]];
valueFunctions: valueFunctions
data = [self allocMemoryWithSize: sizeof(*data)
count: newSize];
capacity: count];
for (j = 0; j < newSize; j++)
data[j] = NULL;
size = newSize;
[mapTable setValue: object
/* Add first key / object pair */
hash = [key hash];
j = hash & (size - 1);
forKey: key];
bucket = [self allocMemoryWithSize: sizeof(*bucket)];
bucket->key = [key copy];
bucket->object = [object retain];
bucket->hash = hash;
data[j] = bucket;
for (i = 1; i < count; i++) {
uint32_t last;
key = va_arg(arguments, id);
object = va_arg(arguments, id);
if (key == nil || object == nil)
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]
selector: _cmd];
hash = [key hash];
last = size;
[mapTable setValue: object
for (j = hash & (size - 1); j < last && data[j] != NULL;
j++)
if ([data[j]->key isEqual: key])
break;
forKey: key];
/* 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 isEqual: key])
break;
}
/* Key not in dictionary */
if (j >= last || data[j] == NULL ||
![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
exceptionWithClass: [self class]];
bucket =
[self allocMemoryWithSize: sizeof(*bucket)];
bucket->key = [key copy];
bucket->object = [object 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.
*/
[object retain];
[data[j]->object release];
data[j]->object = object;
count--;
}
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithSerialization: (OFXMLElement*)element
{
self = [super init];
@try {
void *pool = objc_autoreleasePoolPush();
OFMutableDictionary *dictionary;
OFArray *keys, *objects;
OFEnumerator *keyEnumerator, *objectEnumerator;
OFXMLElement *keyElement, *objectElement;
if ((![[element name] isEqual: @"OFDictionary"] &&
![[element name] isEqual: @"OFMutableDictionary"]) ||
![[element namespace] isEqual: OF_SERIALIZATION_NS])
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]
selector: _cmd];
dictionary = [OFMutableDictionary dictionary];
keys = [element elementsForName: @"key"
namespace: OF_SERIALIZATION_NS];
objects = [element elementsForName: @"object"
namespace: OF_SERIALIZATION_NS];
if ([keys count] != [objects count])
@throw [OFInvalidFormatException
exceptionWithClass: [self class]];
mapTable = [[OFMapTable alloc]
initWithKeyFunctions: keyFunctions
valueFunctions: valueFunctions
capacity: [keys count]];
keyEnumerator = [keys objectEnumerator];
objectEnumerator = [objects objectEnumerator];
while ((keyElement = [keyEnumerator nextObject]) != nil &&
(objectElement = [objectEnumerator nextObject]) != nil) {
void *pool2 = objc_autoreleasePoolPush();
OFXMLElement *key, *object;
key = [[keyElement elementsForNamespace:
OF_SERIALIZATION_NS] firstObject];
object = [[objectElement elementsForNamespace:
OF_SERIALIZATION_NS] firstObject];
if (key == nil || object == nil)
@throw [OFInvalidFormatException
exceptionWithClass: [self class]];
[dictionary setObject: [object objectByDeserializing]
forKey: [key objectByDeserializing]];
[mapTable setValue: [object objectByDeserializing]
forKey: [key objectByDeserializing]];
objc_autoreleasePoolPop(pool2);
}
self = [self initWithDictionary: dictionary];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (id)objectForKey: (id)key
{
uint32_t i, hash, last;
if (key == nil)
@throw [OFInvalidArgumentException
exceptionWithClass: [self class]
selector: _cmd];
hash = [key hash];
last = size;
for (i = hash & (size - 1); i < last && data[i] != NULL; i++) {
if (data[i] == DELETED)
continue;
if ([data[i]->key isEqual: key])
return data[i]->object;
}
if (i < last)
return nil;
return [mapTable valueForKey: key];
}
/* 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 ([data[i]->key isEqual: key])
return data[i]->object;
}
return nil;
}
- (size_t)count
{
return count;
return [mapTable count];
}
- (BOOL)isEqual: (id)dictionary
{
uint32_t i;
OFDictionary_hashtable *dictionary_;
if ([self class] != [OFDictionary_hashtable class] &&
[self class] != [OFMutableDictionary_hashtable class])
return [super isEqual: dictionary];
if ([dictionary count] != count)
return NO;
dictionary_ = (OFDictionary_hashtable*)dictionary;
for (i = 0; i < size; i++)
if (data[i] != NULL && data[i] != DELETED &&
![[dictionary objectForKey: data[i]->key]
isEqual: data[i]->object])
return NO;
return YES;
return [dictionary_->mapTable isEqual: mapTable];
}
- (BOOL)containsObject: (id)object
{
uint32_t i;
if (object == nil || count == 0)
return NO;
return [mapTable containsValue: object];
for (i = 0; i < size; i++)
if (data[i] != NULL && data[i] != DELETED &&
[data[i]->object isEqual: object])
return YES;
return NO;
}
- (BOOL)containsObjectIdenticalTo: (id)object
{
uint32_t i;
return [mapTable containsValueIdenticalTo: object];
if (object == nil || count == 0)
return NO;
for (i = 0; i < size; i++)
if (data[i] != NULL && data[i] != DELETED &&
data[i]->object == object)
return YES;
return NO;
}
- (OFArray*)allKeys
{
OFArray *ret;
id *keys;
size_t count;
count = [mapTable count];
id *keys = [self allocMemoryWithSize: sizeof(*keys)
count: count];
size_t i, j;
keys = [self allocMemoryWithSize: sizeof(*keys)
count: count];
@try {
void *pool = objc_autoreleasePoolPush();
OFMapTableEnumerator *enumerator;
id key;
size_t i;
for (i = j = 0; i < size; i++)
if (data[i] != NULL && data[i] != DELETED)
keys[j++] = data[i]->key;
assert(j == count);
i = 0;
enumerator = [mapTable keyEnumerator];
while ((key = [enumerator nextValue]) != nil) {
assert(i < count);
keys[i++] = key;
}
objc_autoreleasePoolPop(pool);
@try {
ret = [OFArray arrayWithObjects: keys
count: count];
} @finally {
[self freeMemory: keys];
}
return ret;
}
- (OFArray*)allObjects
{
OFArray *ret;
id *objects;
size_t count;
count = [mapTable count];
id *objects = [self allocMemoryWithSize: sizeof(*objects)
count: count];
size_t i, j;
objects = [self allocMemoryWithSize: sizeof(*objects)
count: count];
@try {
void *pool = objc_autoreleasePoolPush();
OFMapTableEnumerator *enumerator;
id object;
size_t i;
for (i = j = 0; i < size; i++)
if (data[i] != NULL && data[i] != DELETED)
objects[j++] = data[i]->object;
assert(j == count);
i = 0;
enumerator = [mapTable valueEnumerator];
while ((object = [enumerator nextValue]) != nil) {
assert(i < count);
objects[i++] = object;
}
objc_autoreleasePoolPop(pool);
@try {
ret = [OFArray arrayWithObjects: objects
count: count];
} @finally {
[self freeMemory: objects];
}
return ret;
}
- (OFEnumerator*)keyEnumerator
{
return [[[OFDictionaryKeyEnumerator_hashtable alloc]
initWithDictionary: self
return [[[OFMapTableEnumeratorWrapper alloc]
initWithEnumerator: [mapTable keyEnumerator]
data: data
size: size
mutationsPointer: NULL] autorelease];
object: self] autorelease];
}
- (OFEnumerator*)objectEnumerator
{
return [[[OFDictionaryObjectEnumerator_hashtable alloc]
initWithDictionary: self
return [[[OFMapTableEnumeratorWrapper alloc]
initWithEnumerator: [mapTable valueEnumerator]
data: data
size: size
mutationsPointer: NULL] autorelease];
object: self] autorelease];
}
- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
objects: (id*)objects
count: (int)count_
{
int i;
return [mapTable countByEnumeratingWithState: state
for (i = 0; i < count_; i++) {
for (; state->state < size && (data[state->state] == NULL ||
data[state->state] == DELETED); state->state++);
if (state->state < size) {
objects[i] = data[state->state]->key;
state->state++;
} else
break;
}
state->itemsPtr = objects;
objects: objects
state->mutationsPtr = (unsigned long*)self;
count: count_];
return i;
}
#ifdef OF_HAVE_BLOCKS
- (void)enumerateKeysAndObjectsUsingBlock:
(of_dictionary_enumeration_block_t)block
{
size_t i;
BOOL stop = NO;
@try {
[mapTable enumerateKeysAndValuesUsingBlock:
^ (void *key, void *value, BOOL *stop) {
block(key, value, stop);
}];
} @catch (OFEnumerationMutationException *e) {
@throw [OFEnumerationMutationException
exceptionWithClass: [self class]
object: self];
}
for (i = 0; i < size && !stop; i++)
if (data[i] != NULL && data[i] != DELETED)
block(data[i]->key, data[i]->object, &stop);
}
#endif
- (void)dealloc
{
uint32_t i;
[mapTable dealloc];
for (i = 0; i < size; i++) {
if (data[i] != NULL && data[i] != DELETED) {
[data[i]->key release];
[data[i]->object release];
}
}
[super dealloc];
}
- (uint32_t)hash
{
uint32_t i, hash = 0;
for (i = 0; i < size; i++) {
if (data[i] != NULL && data[i] != DELETED) {
hash += data[i]->hash;
hash += [data[i]->object hash];
}
}
return hash;
return [mapTable hash];
}
@end
@implementation OFDictionaryEnumerator_hashtable
- initWithDictionary: (OFDictionary_hashtable*)dictionary_
data: (struct of_dictionary_hashtable_bucket**)data_
size: (uint32_t)size_
mutationsPointer: (unsigned long*)mutationsPtr_
{
self = [super init];
dictionary = [dictionary_ retain];
data = data_;
size = size_;
mutations = (mutationsPtr_ != NULL ? *mutationsPtr_ : 0);
mutationsPtr = mutationsPtr_;
return self;
}
- (void)dealloc
{
[dictionary release];
[super dealloc];
}
- (void)reset
{
if (mutationsPtr != NULL && *mutationsPtr != mutations)
@throw [OFEnumerationMutationException
exceptionWithClass: [dictionary class]
object: dictionary];
pos = 0;
}
@end
@implementation OFDictionaryObjectEnumerator_hashtable
- (id)nextObject
{
if (mutationsPtr != NULL && *mutationsPtr != mutations)
@throw [OFEnumerationMutationException
exceptionWithClass: [dictionary class]
object: dictionary];
for (; pos < size && (data[pos] == NULL ||
data[pos] == DELETED); pos++);
if (pos < size)
return data[pos++]->object;
else
return nil;
}
@end
@implementation OFDictionaryKeyEnumerator_hashtable
- (id)nextObject
{
if (mutationsPtr != NULL && *mutationsPtr != mutations)
@throw [OFEnumerationMutationException
exceptionWithClass: [dictionary class]
object: dictionary];
for (; pos < size && (data[pos] == NULL ||
data[pos] == DELETED); pos++);
if (pos < size)
return data[pos++]->key;
else
return nil;
}
@end
|