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
|
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
|
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
-
-
+
-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
+
-
-
+
+
+
-
-
-
+
+
+
+
-
-
-
-
-
-
+
+
+
+
-
-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
+
-
+
-
-
-
+
+
-
-
-
+
+
-
-
-
+
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
|
*/
#include "config.h"
#include <string.h>
#import "OFMutableArray.h"
#import "OFDataArray.h"
#import "OFMutableCArray.h"
#import "OFAutoreleasePool.h"
#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfRangeException.h"
#import "macros.h"
static struct {
Class isa;
} placeholder;
@implementation OFMutableArrayPlaceholder
- init
{
return (id)[[OFMutableCArray alloc] init];
}
- initWithObject: (id)object
{
return (id)[[OFMutableCArray alloc] initWithObject: object];
}
- initWithObjects: (id)firstObject, ...
{
id ret;
va_list arguments;
va_start(arguments, firstObject);
ret = [[OFMutableCArray alloc] initWithObject: firstObject
arguments: arguments];
va_end(arguments);
return ret;
}
- initWithObject: (id)firstObject
arguments: (va_list)arguments
{
return (id)[[OFMutableCArray alloc] initWithObject: firstObject
arguments: arguments];
}
- initWithArray: (OFArray*)array
{
return (id)[[OFMutableCArray alloc] initWithArray: array];
}
- initWithCArray: (id*)objects
{
return (id)[[OFMutableCArray alloc] initWithCArray: objects];
}
- initWithCArray: (id*)objects
length: (size_t)length
{
return (id)[[OFMutableCArray alloc] initWithCArray: objects
length: length];
}
- initWithSerialization: (OFXMLElement*)element
{
return (id)[[OFMutableCArray 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 OFMutableArray
+ (void)initialize
{
if (self == [OFMutableArray class])
placeholder.isa = [OFMutableArrayPlaceholder class];
}
+ alloc
{
if (self == [OFMutableArray class])
return (id)&placeholder;
return [super alloc];
}
- copy
{
return [[OFArray alloc] initWithArray: self];
}
- (void)addObject: (id)object
{
[array addItem: &object];
[object retain];
[self addObject: object
atIndex: [self count]];
mutations++;
}
- (void)addObject: (id)object
atIndex: (size_t)index
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
}
[array addItem: &object
atIndex: index];
[object retain];
mutations++;
- (void)replaceObjectAtIndex: (size_t)index
withObject: (id)object
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
}
- (void)replaceObject: (id)oldObject
withObject: (id)newObject
{
id *cArray = [array cArray];
size_t i, count = [array count];
size_t i, count = [self count];
for (i = 0; i < count; i++) {
if ([cArray[i] isEqual: oldObject]) {
[newObject retain];
if ([[self objectAtIndex: i] isEqual: oldObject]) {
[self replaceObjectAtIndex: i
[cArray[i] release];
cArray[i] = newObject;
withObject: newObject];
return;
}
}
}
- (void)replaceObjectAtIndex: (size_t)index
withObject: (id)object
{
id *cArray = [array cArray];
id oldObject;
if (index >= [array count])
@throw [OFOutOfRangeException newWithClass: isa];
oldObject = cArray[index];
cArray[index] = [object retain];
[oldObject release];
}
- (void)replaceObjectIdenticalTo: (id)oldObject
withObject: (id)newObject
{
id *cArray = [array cArray];
size_t i, count = [array count];
size_t i, count = [self count];
for (i = 0; i < count; i++) {
if (cArray[i] == oldObject) {
[newObject retain];
if ([self objectAtIndex: i] == oldObject) {
[self replaceObjectAtIndex: i
[cArray[i] release];
cArray[i] = newObject;
withObject: newObject];
return;
}
}
}
- (void)removeObject: (id)object
{
id *cArray = [array cArray];
size_t i, count = [array count];
for (i = 0; i < count; i++) {
if ([cArray[i] isEqual: object]) {
object = cArray[i];
[array removeItemAtIndex: i];
mutations++;
[object release];
return;
}
}
}
- (void)removeObjectIdenticalTo: (id)object
{
id *cArray = [array cArray];
size_t i, count = [array count];
for (i = 0; i < count; i++) {
if (cArray[i] == object) {
[array removeItemAtIndex: i];
mutations++;
[object release];
return;
}
}
}
- (void)removeObjectAtIndex: (size_t)index
{
id object = [self objectAtIndex: index];
@throw [OFNotImplementedException newWithClass: isa
[array removeItemAtIndex: index];
[object release];
selector: _cmd];
mutations++;
}
- (void)removeNObjects: (size_t)nObjects
- (void)removeObject: (id)object
{
id *cArray = [array cArray], *copy;
size_t i, count = [array count];
size_t i, count = [self count];
for (i = 0; i < count; i++) {
if ([[self objectAtIndex: i] isEqual: object]) {
if (nObjects > count)
@throw [OFOutOfRangeException newWithClass: isa];
[self removeObjectAtIndex: i];
return;
}
copy = [self allocMemoryForNItems: nObjects
withSize: sizeof(id)];
memcpy(copy, cArray + (count - nObjects), nObjects * sizeof(id));
@try {
[array removeNItems: nObjects];
}
}
- (void)removeObjectIdenticalTo: (id)object
mutations++;
for (i = 0; i < nObjects; i++)
{
size_t i, count = [self count];
[copy[i] release];
} @finally {
[self freeMemory: copy];
}
}
for (i = 0; i < count; i++) {
if ([self objectAtIndex: i] == object) {
[self removeObjectAtIndex: i];
return;
}
}
}
- (void)removeNObjects: (size_t)nObjects
atIndex: (size_t)index
{
id *cArray = [array cArray], *copy;
size_t i, count = [array count];
size_t count = [self count];
if (nObjects > count - index)
[self removeObjectsInRange: of_range(count - nObjects, nObjects)];
@throw [OFOutOfRangeException newWithClass: isa];
copy = [self allocMemoryForNItems: nObjects
}
withSize: sizeof(id)];
memcpy(copy, cArray + index, nObjects * sizeof(id));
- (void)removeObjectsInRange: (of_range_t)range
{
@try {
[array removeNItems: nObjects
atIndex: index];
size_t i;
mutations++;
for (i = 0; i < nObjects; i++)
for (i = 0; i < range.length; i++)
[copy[i] release];
} @finally {
[self freeMemory: copy];
[self removeObjectAtIndex: range.start];
}
}
- (void)removeLastObject
{
id object = [self objectAtIndex: [array count] - 1];
[array removeLastItem];
[object release];
mutations++;
}
- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
objects: (id*)objects
count: (int)count_
{
int ret = [super countByEnumeratingWithState: state
objects: objects
[self removeNObjects: 1];
count: count_];
}
state->mutationsPtr = &mutations;
return ret;
}
- (OFEnumerator*)objectEnumerator
{
return [[[OFArrayEnumerator alloc]
initWithArray: self
dataArray: array
mutationsPointer: &mutations] autorelease];
}
#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
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(cArray[i], i, &stop);
}
}
- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block
{
id *cArray = [array cArray];
size_t i, count = [array count];
BOOL stop = NO;
unsigned long mutations2 = mutations;
[self enumerateObjectsUsingBlock: ^ (id object, size_t index,
for (i = 0; i < count && !stop; i++) {
id newObject;
BOOL *stop) {
if (mutations != mutations2)
@throw [OFEnumerationMutationException
newWithClass: isa
object: self];
[self replaceObjectAtIndex: index
newObject = block(cArray[i], i, &stop);
withObject: block(object, index, stop)];
if (newObject == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
[newObject retain];
[cArray[i] release];
cArray[i] = newObject;
}
}];
}
#endif
- (OFArray*)makeImmutable
{
return self;
}
@end
|