ObjFW  Check-in [63fc941b66]

Overview
Comment:Remove code duplication for fast enumeration of mutable collections.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 63fc941b66aa37300f0727baa4e7588c9c7340d6549f4cd1004a354df8faf19e
User & Date: js on 2011-07-20 18:23:41
Other Links: manifest | tags
Context
2011-07-21
19:03
Add -[allKeys] and -[allObjects] to OFDictionary. check-in: cc87fcff36 user: js tags: trunk
2011-07-20
18:23
Remove code duplication for fast enumeration of mutable collections. check-in: 63fc941b66 user: js tags: trunk
16:50
Check for mutations during fast enumeration in OFMutableSet. check-in: dbd145b124 user: js tags: trunk
Changes

Modified src/OFMutableArray.m from [9e095a5f89] to [4897ca4a74].

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
200
201
202
203
204
205
206


207




208
209

210


211
212

213
214
215
216
217
218
219
220







-
-
+
-
-
-
-
+
+
-

-
-


-
+







	mutations++;
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count_
{
	size_t count = [array count];

	int ret = [super countByEnumeratingWithState: state
	if (count > INT_MAX)
		@throw [OFOutOfRangeException newWithClass: isa];

	if (state->state >= count)
					     objects: objects
					       count: count_];
		return 0;

	state->state = count;
	state->itemsPtr = [array cArray];
	state->mutationsPtr = &mutations;

	return (int)count;
	return ret;
}

- (OFEnumerator*)objectEnumerator
{
	return [[[OFArrayEnumerator alloc]
	       initWithArray: self
		   dataArray: array

Modified src/OFMutableDictionary.m from [77b424abe9] to [7bf06a0039].

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
244
245
246
247
248
249
250


251
252




253






254


255
256

257
258
259
260
261
262
263
264







-
-
+
+
-
-
-
-
+
-
-
-
-
-
-
+
-
-


-
+







	return [[OFDictionary alloc] initWithDictionary: self];
}

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

	int ret = [super countByEnumeratingWithState: state
					     objects: objects
	for (i = 0; i < count_; i++) {
		for (; state->state < size && (data[state->state] == NULL ||
		    data[state->state] == DELETED); state->state++);

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


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

	return i;
	return ret;
}

- (OFEnumerator*)objectEnumerator
{
	return [[[OFDictionaryObjectEnumerator alloc]
	    initWithDictionary: self
			 data: data

Modified src/OFMutableSet.m from [44aa66377a] to [4ebb225162].

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
40
41
42
43
44
45
46



47
48
49
50

51
52


























53
54
55







-
-
-
+
+
+

-


-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+


	mutations++;
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count
{
	OFAutoreleasePool *pool = state->extra.pointers[0];
	OFEnumerator *enumerator = state->extra.pointers[1];
	int i;
	int ret = [super countByEnumeratingWithState: state
					     objects: objects
					       count: count];

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

	if (state->state == -1)
		return 0;

	if (state->state == 0) {
		pool = [[OFAutoreleasePool alloc] init];
		enumerator = [dictionary keyEnumerator];

		state->extra.pointers[0] = pool;
		state->extra.pointers[1] = enumerator;

		state->state = 1;
	}

	for (i = 0; i < count; i++) {
		id object = [enumerator nextObject];

		if (object == nil) {
			[pool release];
			state->state = -1;
			return i;
		}

		objects[i] = object;
	}

	return count;
	return ret;
}
@end