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
|
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
|
-
+
-
-
-
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
+
-
+
-
-
+
-
+
-
+
-
-
+
+
-
+
-
-
-
-
+
+
+
+
-
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
+
-
-
-
-
+
+
+
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFOutOfRangeException.h"
@implementation OFMutableArray
- copy
{
OFArray *new = [[OFArray alloc] init];
OFArray *copy = [[OFArray alloc] init];
id *objs;
size_t count, i;
objs = [array cArray];
count = [array count];
id *cArray = [array cArray];
size_t i, count = [array count];
[new->array addNItems: count
fromCArray: objs];
[copy->array addNItems: count
fromCArray: cArray];
for (i = 0; i < count; i++)
[objs[i] retain];
[cArray[i] retain];
return new;
return copy;
}
- (void)addObject: (id)obj
- (void)addObject: (id)object
{
[array addItem: &obj];
[obj retain];
[array addItem: &object];
[object retain];
mutations++;
}
}
- (void)addObject: (id)obj
- (void)addObject: (id)object
atIndex: (size_t)index
{
[array addItem: &obj
[array addItem: &object
atIndex: index];
[obj retain];
[object retain];
mutations++;
}
- (void)replaceObject: (id)old
withObject: (id)new
- (void)replaceObject: (id)oldObject
withObject: (id)newObject
{
id *objs = [array cArray];
id *cArray = [array cArray];
size_t i, count = [array count];
for (i = 0; i < count; i++) {
if ([objs[i] isEqual: old]) {
[new retain];
[objs[i] release];
objs[i] = new;
if ([cArray[i] isEqual: oldObject]) {
[newObject retain];
[cArray[i] release];
cArray[i] = newObject;
return;
}
}
}
- (void)replaceObjectAtIndex: (size_t)index
withObject: (id)obj
withObject: (id)object
{
id *objs = [array cArray];
id old;
id *cArray = [array cArray];
id oldObject;
if (index >= [array count])
@throw [OFOutOfRangeException newWithClass: isa];
old = objs[index];
objs[index] = [obj retain];
[old release];
oldObject = cArray[index];
cArray[index] = [object retain];
[oldObject release];
}
- (void)replaceObjectIdenticalTo: (id)old
withObject: (id)new
- (void)replaceObjectIdenticalTo: (id)oldObject
withObject: (id)newObject
{
id *objs = [array cArray];
id *cArray = [array cArray];
size_t i, count = [array count];
for (i = 0; i < count; i++) {
if (objs[i] == old) {
[new retain];
[objs[i] release];
objs[i] = new;
if (cArray[i] == oldObject) {
[newObject retain];
[cArray[i] release];
cArray[i] = newObject;
return;
}
}
}
- (void)removeObject: (id)obj
- (void)removeObject: (id)object
{
id *objs = [array cArray];
id *cArray = [array cArray];
size_t i, count = [array count];
for (i = 0; i < count; i++) {
if ([objs[i] isEqual: obj]) {
id obj = objs[i];
if ([cArray[i] isEqual: object]) {
object = cArray[i];
[array removeItemAtIndex: i];
mutations++;
[obj release];
[object release];
return;
}
}
}
- (void)removeObjectIdenticalTo: (id)obj
- (void)removeObjectIdenticalTo: (id)object
{
id *objs = [array cArray];
id *cArray = [array cArray];
size_t i, count = [array count];
for (i = 0; i < count; i++) {
if (objs[i] == obj) {
if (cArray[i] == object) {
[array removeItemAtIndex: i];
mutations++;
[obj release];
[object release];
return;
}
}
}
- (void)removeObjectAtIndex: (size_t)index
{
id old = [self objectAtIndex: index];
id object = [self objectAtIndex: index];
[array removeItemAtIndex: index];
[old release];
[object release];
mutations++;
}
- (void)removeNObjects: (size_t)nobjects
- (void)removeNObjects: (size_t)nObjects
{
id *objs = [array cArray], *copy;
id *cArray = [array cArray], *copy;
size_t i, count = [array count];
if (nobjects > count)
if (nObjects > count)
@throw [OFOutOfRangeException newWithClass: isa];
copy = [self allocMemoryForNItems: nobjects
copy = [self allocMemoryForNItems: nObjects
withSize: sizeof(id)];
memcpy(copy, objs + (count - nobjects), nobjects * sizeof(id));
memcpy(copy, cArray + (count - nObjects), nObjects * sizeof(id));
@try {
[array removeNItems: nobjects];
[array removeNItems: nObjects];
mutations++;
for (i = 0; i < nobjects; i++)
for (i = 0; i < nObjects; i++)
[copy[i] release];
} @finally {
[self freeMemory: copy];
}
}
- (void)removeNObjects: (size_t)nobjects
- (void)removeNObjects: (size_t)nObjects
atIndex: (size_t)index
{
id *objs = [array cArray], *copy;
id *cArray = [array cArray], *copy;
size_t i, count = [array count];
if (nobjects > count - index)
if (nObjects > count - index)
@throw [OFOutOfRangeException newWithClass: isa];
copy = [self allocMemoryForNItems: nobjects
copy = [self allocMemoryForNItems: nObjects
withSize: sizeof(id)];
memcpy(copy, objs + index, nobjects * sizeof(id));
memcpy(copy, cArray + index, nObjects * sizeof(id));
@try {
[array removeNItems: nobjects
[array removeNItems: nObjects
atIndex: index];
mutations++;
for (i = 0; i < nobjects; i++)
for (i = 0; i < nObjects; i++)
[copy[i] release];
} @finally {
[self freeMemory: copy];
}
}
- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
|
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
|
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
|
-
+
-
+
-
+
+
+
-
+
-
+
-
-
-
+
+
+
|
mutationsPointer: &mutations] autorelease];
}
#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
id *objs = [array cArray];
id *cArray = [array cArray];
size_t i, count = [array count];
BOOL stop = NO;
unsigned long mutations2 = mutations;
for (i = 0; i < count && !stop; i++) {
if (mutations != mutations2)
@throw [OFEnumerationMutationException
newWithClass: isa
object: self];
block(objs[i], i, &stop);
block(cArray[i], i, &stop);
[pool releaseObjects];
}
[pool release];
}
- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
id *objs = [array cArray];
id *cArray = [array cArray];
size_t i, count = [array count];
BOOL stop = NO;
unsigned long mutations2 = mutations;
for (i = 0; i < count && !stop; i++) {
id newObject;
if (mutations != mutations2)
@throw [OFEnumerationMutationException
newWithClass: isa
object: self];
id new = block(objs[i], i, &stop);
newObject = block(cArray[i], i, &stop);
if (new == nil)
if (newObject == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
[new retain];
[objs[i] release];
objs[i] = new;
[newObject retain];
[cArray[i] release];
cArray[i] = newObject;
[pool releaseObjects];
}
[pool release];
}
#endif
@end
|