︙ | | |
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
-
+
-
+
-
+
|
#import "OFMapTable+Private.h"
#import "OFEnumerator.h"
#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFOutOfRangeException.h"
extern unsigned long OFHashSeed;
extern uint32_t OFHashSeed;
static const unsigned long minCapacity = 16;
static const uint32_t minCapacity = 16;
struct OFMapTableBucket {
void *key, *object;
unsigned long hash;
uint32_t hash;
};
static struct OFMapTableBucket deletedBucket = { 0 };
static void *
defaultRetain(void *object)
{
return object;
|
︙ | | |
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
-
+
|
return (object1 == object2);
}
OF_DIRECT_MEMBERS
@interface OFMapTableEnumerator ()
- (instancetype)of_initWithMapTable: (OFMapTable *)mapTable
buckets: (struct OFMapTableBucket **)buckets
capacity: (unsigned long)capacity
capacity: (uint32_t)capacity
mutationsPointer: (unsigned long *)mutationsPtr
OF_METHOD_FAMILY(init);
@end
@interface OFMapTableKeyEnumerator: OFMapTableEnumerator
@end
|
︙ | | |
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
|
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
|
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
|
SET_DEFAULT(_objectFunctions.retain, defaultRetain);
SET_DEFAULT(_objectFunctions.release, defaultRelease);
SET_DEFAULT(_objectFunctions.hash, defaultHash);
SET_DEFAULT(_objectFunctions.equal, defaultEqual);
#undef SET_DEFAULT
if (capacity > ULONG_MAX / sizeof(*_buckets) ||
capacity > ULONG_MAX / 8)
if (capacity > UINT32_MAX / sizeof(*_buckets) ||
capacity > UINT32_MAX / 8)
@throw [OFOutOfRangeException exception];
for (_capacity = 1; _capacity < capacity;) {
if (_capacity > ULONG_MAX / 2)
if (_capacity > UINT32_MAX / 2)
@throw [OFOutOfRangeException exception];
_capacity *= 2;
}
if (capacity * 8 / _capacity >= 6)
if (_capacity <= ULONG_MAX / 2)
if (_capacity <= UINT32_MAX / 2)
_capacity *= 2;
if (_capacity < minCapacity)
_capacity = minCapacity;
_buckets = OFAllocZeroedMemory(_capacity, sizeof(*_buckets));
if (OFHashSeed != 0)
_rotate = OFRandom16() & 31;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
for (unsigned long i = 0; i < _capacity; i++) {
for (uint32_t i = 0; i < _capacity; i++) {
if (_buckets[i] != NULL && _buckets[i] != &deletedBucket) {
_keyFunctions.release(_buckets[i]->key);
_objectFunctions.release(_buckets[i]->object);
OFFreeMemory(_buckets[i]);
}
}
OFFreeMemory(_buckets);
[super dealloc];
}
static void
resizeForCount(OFMapTable *self, unsigned long count)
resizeForCount(OFMapTable *self, uint32_t count)
{
unsigned long fullness, capacity;
uint32_t fullness, capacity;
struct OFMapTableBucket **buckets;
if (count > ULONG_MAX / sizeof(*self->_buckets) ||
count > ULONG_MAX / 8)
if (count > UINT32_MAX / sizeof(*self->_buckets) ||
count > UINT32_MAX / 8)
@throw [OFOutOfRangeException exception];
fullness = count * 8 / self->_capacity;
if (fullness >= 6) {
if (self->_capacity > ULONG_MAX / 2)
if (self->_capacity > UINT32_MAX / 2)
return;
capacity = self->_capacity * 2;
} else if (fullness <= 1)
capacity = self->_capacity / 2;
else
return;
/*
* Don't downsize if we have an initial capacity or if we would fall
* below the minimum capacity.
*/
if ((capacity < self->_capacity && count > self->_count) ||
capacity < minCapacity)
return;
buckets = OFAllocZeroedMemory(capacity, sizeof(*buckets));
for (unsigned long i = 0; i < self->_capacity; i++) {
for (uint32_t i = 0; i < self->_capacity; i++) {
if (self->_buckets[i] != NULL &&
self->_buckets[i] != &deletedBucket) {
unsigned long j, last;
uint32_t j, last;
last = capacity;
for (j = self->_buckets[i]->hash & (capacity - 1);
j < last && buckets[j] != NULL; j++);
/* In case the last bucket is already used */
|
︙ | | |
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
-
+
-
-
+
|
OFFreeMemory(self->_buckets);
self->_buckets = buckets;
self->_capacity = capacity;
}
static void
setObject(OFMapTable *restrict self, void *key, void *object,
setObject(OFMapTable *restrict self, void *key, void *object, uint32_t hash)
unsigned long hash)
{
unsigned long i, last;
uint32_t i, last;
void *old;
if (key == NULL || object == NULL)
@throw [OFInvalidArgumentException exception];
hash = OFRotateLeft(hash, self->_rotate);
last = self->_capacity;
|
︙ | | |
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
-
+
|
mapTable = object;
if (mapTable->_count != _count ||
mapTable->_keyFunctions.equal != _keyFunctions.equal ||
mapTable->_objectFunctions.equal != _objectFunctions.equal)
return false;
for (unsigned long i = 0; i < _capacity; i++) {
for (uint32_t i = 0; i < _capacity; i++) {
if (_buckets[i] != NULL && _buckets[i] != &deletedBucket) {
void *objectIter =
[mapTable objectForKey: _buckets[i]->key];
if (!_objectFunctions.equal(objectIter,
_buckets[i]->object))
return false;
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
{
OFMapTable *copy = [[OFMapTable alloc]
initWithKeyFunctions: _keyFunctions
objectFunctions: _objectFunctions
capacity: _capacity];
@try {
for (unsigned long i = 0; i < _capacity; i++)
for (uint32_t i = 0; i < _capacity; i++)
if (_buckets[i] != NULL &&
_buckets[i] != &deletedBucket)
setObject(copy, _buckets[i]->key,
_buckets[i]->object,
OFRotateRight(_buckets[i]->hash, _rotate));
} @catch (id e) {
[copy release];
@throw e;
}
return copy;
}
- (size_t)count
{
return _count;
}
- (void *)objectForKey: (void *)key
{
unsigned long i, hash, last;
uint32_t i, hash, last;
if (key == NULL)
@throw [OFInvalidArgumentException exception];
hash = OFRotateLeft(_keyFunctions.hash(key), _rotate);
hash = OFRotateLeft((uint32_t)_keyFunctions.hash(key), _rotate);
last = _capacity;
for (i = hash & (_capacity - 1); i < last && _buckets[i] != NULL; i++) {
if (_buckets[i] == &deletedBucket)
continue;
if (_keyFunctions.equal(_buckets[i]->key, key))
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
}
return NULL;
}
- (void)setObject: (void *)object forKey: (void *)key
{
setObject(self, key, object,_keyFunctions.hash(key));
setObject(self, key, object, (uint32_t)_keyFunctions.hash(key));
}
- (void)removeObjectForKey: (void *)key
{
unsigned long i, hash, last;
uint32_t i, hash, last;
if (key == NULL)
@throw [OFInvalidArgumentException exception];
hash = OFRotateLeft(_keyFunctions.hash(key), _rotate);
hash = OFRotateLeft((uint32_t)_keyFunctions.hash(key), _rotate);
last = _capacity;
for (i = hash & (_capacity - 1); i < last && _buckets[i] != NULL; i++) {
if (_buckets[i] == &deletedBucket)
continue;
if (_keyFunctions.equal(_buckets[i]->key, key)) {
|
︙ | | |
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
-
+
|
return;
}
}
}
- (void)removeAllObjects
{
for (unsigned long i = 0; i < _capacity; i++) {
for (uint32_t i = 0; i < _capacity; i++) {
if (_buckets[i] != NULL) {
if (_buckets[i] == &deletedBucket) {
_buckets[i] = NULL;
continue;
}
_keyFunctions.release(_buckets[i]->key);
|
︙ | | |
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
|
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
|
-
+
-
+
|
}
- (bool)containsObject: (void *)object
{
if (object == NULL || _count == 0)
return false;
for (unsigned long i = 0; i < _capacity; i++)
for (uint32_t i = 0; i < _capacity; i++)
if (_buckets[i] != NULL && _buckets[i] != &deletedBucket)
if (_objectFunctions.equal(_buckets[i]->object, object))
return true;
return false;
}
- (bool)containsObjectIdenticalTo: (void *)object
{
if (object == NULL || _count == 0)
return false;
for (unsigned long i = 0; i < _capacity; i++)
for (uint32_t i = 0; i < _capacity; i++)
if (_buckets[i] != NULL && _buckets[i] != &deletedBucket)
if (_buckets[i]->object == object)
return true;
return false;
}
|
︙ | | |
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
-
+
|
- (instancetype)init
{
OF_INVALID_INIT_METHOD
}
- (instancetype)of_initWithMapTable: (OFMapTable *)mapTable
buckets: (struct OFMapTableBucket **)buckets
capacity: (unsigned long)capacity
capacity: (uint32_t)capacity
mutationsPointer: (unsigned long *)mutationsPtr
{
self = [super init];
_mapTable = [mapTable retain];
_buckets = buckets;
_capacity = capacity;
|
︙ | | |