ObjFW  Diff

Differences From Artifact [c6cdd10ae1]:

To Artifact [68b8ff9c5e]:


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
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







-
-
+
+






-
+
















-
-
+
+
+
+


-
+




-
-
+
+



-
+


-
+





-
+


-
+
-
-




















-
-
+
+


-
+







-
-
+
+


-
+





-
-
+
+
+
+





-
-
+
+





-
-
+
+





+
+
+
-
+
+
+
+
+



+




-
-
-
+
+
+
+






-
-
+
+















-
-
+
+


-
-
-
-
+
+
+
+
+















-
-
+
+


-
-
-
-
+
+
+
+
+








#include <string.h>

#import "OFMutableDictionary.h"
#import "OFExceptions.h"
#import "macros.h"

#define BUCKET_SIZE sizeof(struct of_dictionary_bucket)
#define DELETED (id)&of_dictionary_deleted_bucket
#define BUCKET struct of_dictionary_bucket
#define DELETED &of_dictionary_deleted_bucket

@implementation OFMutableDictionary
- (void)_resizeForCount: (size_t)newcount
{
	size_t fill = newcount * 4 / size;
	size_t newsize;
	struct of_dictionary_bucket *newdata;
	struct of_dictionary_bucket **newdata;
	uint32_t i;

	if (newcount > UINT32_MAX)
		@throw [OFOutOfRangeException newWithClass: isa];

	if (fill >= 3)
		newsize = size << 1;
	else if (fill <= 1)
		newsize = size >> 1;
	else
		return;

	if (newsize == 0)
		@throw [OFOutOfRangeException newWithClass: isa];

	newdata = [self allocMemoryForNItems: newsize
				    withSize: BUCKET_SIZE];
	memset(newdata, 0, newsize * BUCKET_SIZE);
				    withSize: sizeof(BUCKET*)];

	for (i = 0; i < newsize; i++)
		newdata[i] = NULL;

	for (i = 0; i < size; i++) {
		if (data[i].key != nil && data[i].key != DELETED) {
		if (data[i] != NULL && data[i] != DELETED) {
			uint32_t j, last;

			last = newsize;

			j = data[i].hash & (newsize - 1);
			for (; j < last && newdata[j].key != nil; j++);
			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);
				last = data[i]->hash & (newsize - 1);

				for (j = 0; j < last &&
				    newdata[j].key != nil; i++);
				    newdata[j] != NULL; i++);
			}

			if (j >= last) {
				[self freeMemory: newdata];
				@throw [OFOutOfRangeException
				    newWithClass: [self class]];
				    newWithClass: isa];
			}

			newdata[j].key = data[i].key;
			newdata[j] = data[i];
			newdata[j].object = data[i].object;
			newdata[j].hash = data[i].hash;
		}
	}

	[self freeMemory: data];
	data = newdata;
	size = newsize;
}

- setObject: (OFObject*)obj
     forKey: (OFObject <OFCopying>*)key
{
	uint32_t i, hash, last;

	if (key == nil || obj == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i].key != nil; i++) {
		if (data[i].key == DELETED)
	for (i = hash & (size - 1); i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([data[i].key isEqual: key])
		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].key != nil; i++) {
			if (data[i].key == DELETED)
		for (i = 0; i < last && data[i] != NULL; i++) {
			if (data[i] == DELETED)
				continue;

			if ([data[i].key isEqual: key])
			if ([data[i]->key isEqual: key])
				break;
		}
	}

	/* Key not in dictionary */
	if (i >= last || data[i].key == nil || data[i].key == DELETED ||
	    ![data[i].key isEqual: key]) {
	if (i >= last || data[i] == NULL || data[i] == DELETED ||
	    ![data[i]->key isEqual: key]) {
		BUCKET *b;

		[self _resizeForCount: count + 1];

		mutations++;
		last = size;

		for (i = hash & (size - 1); i < last && data[i].key != nil &&
		    data[i].key != DELETED; i++);
		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].key != nil &&
			    data[i].key != DELETED; i++);
			for (i = 0; i < last && data[i] != NULL &&
			    data[i] != DELETED; i++);
		}

		if (i >= last)
			@throw [OFOutOfRangeException newWithClass: isa];

		b = [self allocMemoryWithSize: sizeof(BUCKET)];

		@try {
		key = [key copy];
			key = [key copy];
		} @catch (OFException *e) {
			[self freeMemory: b];
		}

		@try {
			[obj retain];
		} @catch (OFException *e) {
			[self freeMemory: b];
			[key release];
			@throw e;
		}

		data[i].key = key;
		data[i].object = obj;
		data[i].hash = hash;
		b->key = key;
		b->object = obj;
		b->hash = hash;
		data[i] = b;
		count++;

		return self;
	}

	[obj retain];
	[data[i].object release];
	data[i].object = obj;
	[data[i]->object release];
	data[i]->object = obj;

	return self;
}

- removeObjectForKey: (OFObject*)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].key != nil; i++) {
		if (data[i].key == DELETED)
	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];
			data[i].key = DELETED;
		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 self;
		}
	}

	if (i < last)
		return self;

	/* In case the last bucket is already used */
	last = hash & (size - 1);

	for (i = 0; i < last && data[i].key != nil; i++) {
		if (data[i].key == DELETED)
	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];
			data[i].key = DELETED;
		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 self;
		}
219
220
221
222
223
224
225
226
227


228
229
230

231
232
233
234
235
236
237
232
233
234
235
236
237
238


239
240
241
242

243
244
245
246
247
248
249
250







-
-
+
+


-
+







- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count_
{
	int i;

	for (i = 0; i < count_; i++) {
		for (; state->state < size && (data[state->state].key == nil ||
		    data[state->state].key == DELETED); state->state++);
		for (; state->state < size && (data[state->state] == NULL ||
		    data[state->state] == DELETED); state->state++);

		if (state->state < size) {
			objects[i] = data[state->state].key;
			objects[i] = data[state->state]->key;
			state->state++;
		} else
			break;
	}

	state->itemsPtr = objects;
	state->mutationsPtr = &mutations;