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
|
* 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>
#import "OFMutableDictionary.h"
#import "OFAutoreleasePool.h"
#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFOutOfRangeException.h"
#import "macros.h"
#define DELETED &of_dictionary_deleted_bucket
@implementation OFMutableDictionary
- (void)_resizeForCount: (size_t)newCount
{
size_t fullness = newCount * 4 / size;
struct of_dictionary_bucket **newData;
uint32_t i, newSize;
if (newCount > UINT32_MAX)
@throw [OFOutOfRangeException newWithClass: isa];
if (fullness >= 3)
newSize = size << 1;
else if (fullness <= 1)
newSize = size >> 1;
else
return;
if (newSize == 0)
@throw [OFOutOfRangeException newWithClass: isa];
newData = [self allocMemoryForNItems: newSize
withSize: sizeof(*newData)];
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;
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);
for (j = 0; j < last &&
newData[j] != NULL; j++);
}
if (j >= last) {
[self freeMemory: newData];
@throw [OFOutOfRangeException
newWithClass: isa];
}
newData[j] = data[i];
}
}
[self freeMemory: data];
data = newData;
size = newSize;
}
- (void)_setObject: (id)object
forKey: (id)key
copyKey: (BOOL)copyKey
{
uint32_t i, hash, last;
id old;
if (key == nil || object == 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 ([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 ([data[i]->key isEqual: key])
break;
}
}
/* Key not in dictionary */
if (i >= last || data[i] == NULL || data[i] == DELETED ||
![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];
bucket = [self allocMemoryWithSize: sizeof(*bucket)];
@try {
key = (copyKey ? [key copy] : [key retain]);
} @catch (id e) {
[self freeMemory: bucket];
@throw e;
}
@try {
[object retain];
} @catch (id e) {
[self freeMemory: bucket];
[key release];
@throw e;
}
bucket->key = key;
bucket->object = object;
bucket->hash = hash;
data[i] = bucket;
count++;
return;
}
old = data[i]->object;
data[i]->object = [object retain];
[old release];
}
- (void)setObject: (id)object
forKey: (id <OFCopying>)key
{
[self _setObject: object
forKey: key
copyKey: YES];
}
- (void)removeObjectForKey: (id <OFCopying>)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 ([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];
return;
}
}
if (i < last)
return;
/* 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]) {
[data[i]->key release];
[data[i]->object release];
[self freeMemory: data[i]];
data[i] = DELETED;
count--;
mutations++;
[self _resizeForCount: count];
return;
}
}
}
- copy
{
return [[OFDictionary alloc] initWithDictionary: self];
}
- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
objects: (id*)objects
count: (int)count_
{
int ret = [super countByEnumeratingWithState: state
objects: objects
count: count_];
state->mutationsPtr = &mutations;
return ret;
}
- (OFEnumerator*)objectEnumerator
{
return [[[OFDictionaryObjectEnumerator alloc]
initWithDictionary: self
data: data
size: size
mutationsPointer: &mutations] autorelease];
}
- (OFEnumerator*)keyEnumerator
{
return [[[OFDictionaryKeyEnumerator alloc]
initWithDictionary: self
data: data
size: size
mutationsPointer: &mutations] autorelease];
}
#ifdef OF_HAVE_BLOCKS
- (void)enumerateKeysAndObjectsUsingBlock:
(of_dictionary_enumeration_block_t)block
{
size_t i;
BOOL stop = NO;
unsigned long mutations2 = mutations;
for (i = 0; i < size && !stop; i++) {
if (mutations != mutations2)
@throw [OFEnumerationMutationException
newWithClass: isa
object: self];
if (data[i] != NULL && data[i] != DELETED)
block(data[i]->key, data[i]->object, &stop);
}
}
- (void)replaceObjectsUsingBlock: (of_dictionary_replace_block_t)block
{
size_t i;
BOOL stop = NO;
unsigned long mutations2 = mutations;
for (i = 0; i < size && !stop; i++) {
if (mutations != mutations2)
@throw [OFEnumerationMutationException
newWithClass: isa
object: self];
if (data[i] != NULL && data[i] != DELETED) {
id new = block(data[i]->key, data[i]->object, &stop);
if (new == nil)
@throw [OFInvalidArgumentException
newWithClass: isa
selector: _cmd];
[new retain];
[data[i]->object release];
data[i]->object = new;
}
}
}
#endif
- (void)makeImmutable
{
isa = [OFDictionary class];
}
@end
|
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
|
>
|
>
>
>
|
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
|
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
<
<
<
|
<
<
|
<
<
<
<
<
<
|
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
|
<
<
<
|
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
|
|
<
<
<
<
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
|
<
<
|
<
<
<
<
|
<
<
<
|
<
<
|
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
|
* 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"
#import "OFMutableDictionary_hashtable.h"
#import "OFAutoreleasePool.h"
#import "OFNotImplementedException.h"
#import "macros.h"
static struct {
Class isa;
} placeholder;
@implementation OFMutableDictionary_placeholder
- init
{
return (id)[[OFMutableDictionary_hashtable alloc] init];
}
- initWithDictionary: (OFDictionary*)dictionary
{
return (id)[[OFMutableDictionary_hashtable alloc]
initWithDictionary: dictionary];
}
- initWithObject: (id)object
forKey: (id <OFCopying>)key
{
return (id)[[OFMutableDictionary_hashtable alloc] initWithObject: object
forKey: key];
}
- initWithObjects: (OFArray*)objects
forKeys: (OFArray*)keys
{
return (id)[[OFMutableDictionary_hashtable alloc]
initWithObjects: objects
forKeys: keys];
}
- initWithKeysAndObjects: (id <OFCopying>)firstKey, ...
{
id ret;
va_list arguments;
va_start(arguments, firstKey);
ret = (id)[[OFMutableDictionary_hashtable alloc]
initWithKey: firstKey
arguments: arguments];
va_end(arguments);
return ret;
}
- initWithKey: (id <OFCopying>)firstKey
arguments: (va_list)arguments
{
return (id)[[OFMutableDictionary_hashtable alloc]
initWithKey: firstKey
arguments: arguments];
}
- initWithSerialization: (OFXMLElement*)element
{
return (id)[[OFMutableDictionary_hashtable alloc]
initWithSerialization: element];
}
- retain
{
return self;
}
- autorelease
{
return self;
}
- (void)release
{
}
- (void)dealloc
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
[super dealloc]; /* Get rid of a stupid warning */
}
@end
@implementation OFMutableDictionary
+ (void)initialize
{
if (self == [OFMutableDictionary class])
placeholder.isa = [OFMutableDictionary_placeholder class];
}
+ alloc
{
if (self == [OFMutableDictionary class])
return (id)&placeholder;
return [super alloc];
}
- (void)setObject: (id)object
forKey: (id <OFCopying>)key
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
}
- (void)removeObjectForKey: (id <OFCopying>)key
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
}
- copy
{
return [[OFDictionary alloc] initWithDictionary: self];
}
#ifdef OF_HAVE_BLOCKS
- (void)replaceObjectsUsingBlock: (of_dictionary_replace_block_t)block
{
[self enumerateKeysAndObjectsUsingBlock: ^ (id key, id object,
BOOL *stop) {
[self setObject: block(key, object, stop)
forKey: key];
}];
}
#endif
- (void)makeImmutable
{
}
@end
|