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
|
#import "OFMutableDictionary.h"
#import "OFExceptions.h"
#import "OFMacros.h"
#define BUCKET_SIZE sizeof(struct of_dictionary_bucket)
static OF_INLINE void
resize(id self, size_t count, struct of_dictionary_bucket **data, size_t *size)
{
float fill = (float)count / *size;
size_t newsize;
struct of_dictionary_bucket *newdata;
uint32_t i;
/*
* FIXME:
*
* Throw an OFOutOfRangeException if it would overflow (unlikely to
* happen).
*/
if (fill > 0.75)
newsize = *size * 2;
else if (fill < 0.25)
newsize = *size / 2;
else
return;
newdata = [self allocMemoryForNItems: newsize
withSize: BUCKET_SIZE];
memset(newdata, 0, newsize * BUCKET_SIZE);
|
|
>
<
>
|
>
|
<
<
<
<
|
|
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
|
#import "OFMutableDictionary.h"
#import "OFExceptions.h"
#import "OFMacros.h"
#define BUCKET_SIZE sizeof(struct of_dictionary_bucket)
static OF_INLINE void
resize(id self, Class isa, size_t count, struct of_dictionary_bucket **data,
size_t *size)
{
float fill = (float)count / *size;
size_t newsize;
struct of_dictionary_bucket *newdata;
uint32_t i;
if (fill > 0.75) {
if (*size > SIZE_MAX / 2)
@throw [OFOutOfRangeException newWithClass: isa];
newsize = *size * 2;
} else if (fill < 0.25)
newsize = *size / 2;
else
return;
newdata = [self allocMemoryForNItems: newsize
withSize: BUCKET_SIZE];
memset(newdata, 0, newsize * BUCKET_SIZE);
|
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/* In case the last bucket is already used */
if (i >= size)
for (i = 0; i < size && data[i].key != nil &&
![data[i].key isEqual: key]; i++);
/* Key not in dictionary */
if (i >= size || data[i].key == nil) {
resize(self, count + 1, &data, &size);
i = hash & (size - 1);
for (; i < size && data[i].key != nil; i++);
/* In case the last bucket is already used */
if (i >= size)
for (i = 0; i < size && data[i].key != nil; i++);
|
|
|
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/* In case the last bucket is already used */
if (i >= size)
for (i = 0; i < size && data[i].key != nil &&
![data[i].key isEqual: key]; i++);
/* Key not in dictionary */
if (i >= size || data[i].key == nil) {
resize(self, isa, count + 1, &data, &size);
i = hash & (size - 1);
for (; i < size && data[i].key != nil; i++);
/* In case the last bucket is already used */
if (i >= size)
for (i = 0; i < size && data[i].key != nil; i++);
|
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
return self;
[data[i].key release];
[data[i].object release];
data[i].key = nil;
count--;
resize(self, count, &data, &size);
return self;
}
- (id)copy
{
return [[OFDictionary alloc] initWithDictionary: self];
}
@end
|
|
|
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
return self;
[data[i].key release];
[data[i].object release];
data[i].key = nil;
count--;
resize(self, isa, count, &data, &size);
return self;
}
- (id)copy
{
return [[OFDictionary alloc] initWithDictionary: self];
}
@end
|