forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSNSMutableArray.m
334 lines (268 loc) · 11.7 KB
/
FSNSMutableArray.m
1
2
3
4
5
6
7
8
9
10
11
12
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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* FSNSMutableArray.m Copyright (c) 2003-2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "FSNSMutableArray.h"
#import "FSBooleanPrivate.h"
#import "FSArray.h"
#import "FSMiscTools.h"
#import "FScriptFunctions.h"
#import "NumberPrivate.h"
#import "FSNSArrayPrivate.h"
@implementation NSMutableArray(FSNSMutableArray)
- (void)add:(id)elem
{
[self addObject:elem];
}
- (id)at:(id)index put:(id)elem
{
double ind;
NSUInteger count = [self count];
if (!index) FSExecError(@"index of an array must not be nil");
if ([index isKindOfClass:[NSIndexSet class]])
{
NSMutableArray *newIndex = [FSArray array];
NSUInteger currentIndex = [index firstIndex];
while (currentIndex != NSNotFound)
{
[newIndex addObject:[NSNumber numberWithDouble:currentIndex]];
currentIndex = [index indexGreaterThanIndex:currentIndex];
}
index = newIndex;
}
if ([index isKindOfClass:[NSNumber class]])
{
ind = [index doubleValue];
if (ind < 0) FSExecError(@"index of an array must be a number greater or equal to 0");
if (ind >= count) FSExecError(@"index of an array must be a number less than the size of the array");
[self replaceObjectAtIndex:ind withObject:elem];
}
else if ([index isKindOfClass:[NSArray class]])
{
id elem_index;
NSUInteger i = 0;
NSUInteger j = 0;
NSUInteger nb = [index count];
NSUInteger elem_count = 0; // elem_count is initialized in order to avoid a false warning "might be used uninitialized"
BOOL elemIsArray = [elem isKindOfClass:[NSArray class]];
//if (![elem isKindOfClass:[NSArray class]])
// FSExecError(@"method \"at:put:\", argument 1 is an array, argument 2 must be an array too");
if (elemIsArray) elem_count = [elem count];
while (i < nb && ![index objectAtIndex:i]) i++; // ignore the nil value
if (i == nb)
{
if ((nb == count || nb == 0) && (!elemIsArray || elem_count == 0)) return elem;
else FSExecError(@"invalid index");
}
elem_index = [index objectAtIndex:i];
if ([elem_index isKindOfClass:[FSBoolean class]])
{
NSUInteger k,trueCount;
if (nb != count) FSExecError(@"indexing with an array of boolean of bad size");
if (elemIsArray)
{
for (k=i, trueCount=0; k<nb; k++)
{
elem_index = [index objectAtIndex:k];
if (elem_index == fsTrue || (elem_index != fsFalse && [elem_index isKindOfClass:[FSBoolean class]] && [elem_index isTrue]))
trueCount++;
}
if (elem_count < trueCount) FSExecError(@"method \"at:put:\", not enough elements in argument 2");
else if (elem_count > trueCount) FSExecError(@"method \"at:put:\", too many elements in argument 2");
}
while (i < nb)
{
elem_index = [index objectAtIndex:i];
if (elem_index == fsTrue || (elem_index != fsFalse && [elem_index isKindOfClass:[FSBoolean class]] && [elem_index isTrue]) )
{
[self replaceObjectAtIndex:i withObject:elemIsArray ? [elem objectAtIndex:j] : elem];
j++;
}
else if (elem_index != fsFalse && ![elem_index isKindOfClass:[FSBoolean class]]) FSExecError(@"indexing with a mixed array");
i++;
while (i < nb && ![index objectAtIndex:i]) i++; // ignore the nil value
}
return elem;
}
else if ([elem_index isKindOfClass:[NSNumber class]])
{
NSUInteger k;
if (elemIsArray && nb != elem_count) FSExecError(@"method \"at:put:\", argument 1 and argument 2 must be arrays of same size");
for (k=i; k<nb; k++)
{
elem_index = [index objectAtIndex:k];
if (![elem_index isKindOfClass:[NSNumber class]]) FSExecError(@"array indexing by a mixed array");
ind = [elem_index doubleValue];
if (ind < 0) FSExecError(@"index of an array must be a number greater or equal to 0");
else if (ind >= count) FSExecError(@"index of an array must be a number less than the size of the array");
}
while (i < nb)
{
elem_index = [index objectAtIndex:i];
ind = [elem_index doubleValue];
[self replaceObjectAtIndex:ind withObject:elemIsArray ? [elem objectAtIndex:i] : elem];
i++;
while (i < nb && ![index objectAtIndex:i]) i++; // ignore the nil value
}
}
else // elem_index is neither an NSNumber nor a FSBoolean
FSExecError([NSString stringWithFormat:@"array indexing by an array containing %@", descriptionForFSMessage(elem_index)]);
}
else
FSExecError([NSString stringWithFormat:@"array indexing by %@, number, array or index set expected", descriptionForFSMessage(index)]);
return elem;
}
-(void)insert:(id)obj at:(NSNumber *)index
{
double ind;
FSVerifClassArgs(@"insert:at:",2,obj,(id)nil,(NSInteger)1,index,NSNumberClass,(NSInteger)0);
ind = [index doubleValue];
if (ind < 0) FSExecError(@"argument 2 of method \"insert:at:\" must be a number greater or equal to 0");
if (ind > [self count]) FSExecError(@"argument 2 of method \"insert:at:\" must be a number less or equal to the size of the array");
if (ind != (NSInteger)ind) FSExecError(@"argument 2 of method \"insert:at:\" must be an integer");
[self insertObject:obj atIndex:(NSUInteger)ind];
return;
}
- (void)removeAt:(id)index
{
NSUInteger count = [self count];
id indexSet;
if (!index) FSExecError(@"index of an array must not be nil");
if ([index isKindOfClass:[NSNumber class]])
{
double ind = [index doubleValue];
if (ind < 0) FSExecError(@"argument of method \"removeAt:\" must be a number greater or equal to 0");
if (ind >= count) FSExecError(@"argument of method \"removeAt:\" must be a number less than the size of the array");
if (ind != (NSInteger)ind) FSExecError(@"argument of method \"removeAt:\" must be an integer");
[self removeObjectAtIndex:(NSUInteger)ind];
return;
}
else if ([index isKindOfClass:[NSArray class]])
{
NSUInteger nb = [index count];
NSUInteger i = 0;
indexSet = [NSMutableIndexSet indexSet];
while (i < nb && ![index objectAtIndex:i]) i++; // ignore the nil value
if (i == nb)
{
if (nb == count || nb == 0) return;
else FSExecError(@"invalid index");
}
id elem_index = [index objectAtIndex:i];
if ([elem_index isKindOfClass:[FSBoolean class]])
{
if (nb != count) FSExecError(@"indexing with an array of boolean of bad size");
while (i < nb)
{
elem_index = [index objectAtIndex:i];
if (elem_index == fsTrue || (elem_index != fsFalse && [elem_index isKindOfClass:[FSBoolean class]] && [elem_index isTrue]) )
[indexSet addIndex:i];
else if (elem_index != fsFalse && ![elem_index isKindOfClass:[FSBoolean class]])
FSExecError(@"indexing with a mixed array");
i++;
while (i < nb && ![index objectAtIndex:i]) i++; // ignore the nil value
}
}
else if ([elem_index isKindOfClass:[NSNumber class]])
{
NSUInteger k;
double ind;
for (k=i; k<nb; k++)
{
elem_index = [index objectAtIndex:k];
if (![elem_index isKindOfClass:[NSNumber class]]) FSExecError(@"indexing with a mixed array");
ind = [elem_index doubleValue];
if (ind < 0) FSExecError(@"index of an array must be a number greater or equal to 0");
else if (ind >= count) FSExecError(@"index of an array must be a number less than the size of the array");
}
while (i < nb)
{
elem_index = [index objectAtIndex:i];
ind = [elem_index doubleValue];
[indexSet addIndex:ind];
i++;
while (i < nb && ![index objectAtIndex:i]) i++; // ignore the nil value
}
}
else // elem_index is neither an NSNumber nor a FSBoolean
FSExecError([NSString stringWithFormat:@"indexing with an array containing %@",descriptionForFSMessage(elem_index)]);
}
else if ([index isKindOfClass:[NSIndexSet class]])
{
indexSet = index;
}
else
FSExecError([NSString stringWithFormat:@"indexing by %@, number, array or index set expected", descriptionForFSMessage(index)]);
NSUInteger currentIndex = [indexSet lastIndex];
while (currentIndex != NSNotFound)
{
[self removeObjectAtIndex:currentIndex];
currentIndex = [indexSet indexLessThanIndex:currentIndex];
}
}
- (void)removeWhere:(NSArray *)booleans
{
NSUInteger count = [self count];
Class FSBooleanClass = [FSBoolean class];
if (!booleans) FSExecError(@"argument of method \"removeWhere:\" must not be nil");
if (![booleans isKindOfClass:[NSArray class]]) FSExecError([NSString stringWithFormat:@"argument of method \"removeWhere:\" is %@. An array was expected", descriptionForFSMessage(booleans)]);
if (count != [booleans count]) FSExecError(@"receiver and argument of method \"removeWhere:\" must be arrays of same size");
for (NSUInteger i = 0; i < count; i++)
{
id boolean = [booleans objectAtIndex:i];
if (boolean != fsFalse && boolean != fsTrue && boolean != nil && ![boolean isKindOfClass:FSBooleanClass])
FSExecError(@"argument of method \"removeWhere:\" must be an array of booleans");
}
for (NSUInteger i = count; i > 0; i--)
{
id boolean = [booleans objectAtIndex:i-1];
if (boolean == fsFalse || boolean == nil)
continue;
else if (boolean == fsTrue || ([boolean isKindOfClass:FSBooleanClass] && [boolean isTrue]))
[self removeObjectAtIndex:i-1];
}
}
- (void)setValue:(NSArray *)operand
{
VERIF_OP_NSARRAY(@"setValue:");
[self setArray:operand];
}
- (FSArray *)where:(NSArray *)booleans put:(id)elem
{
NSUInteger count = [self count];
BOOL elemIsArray = [elem isKindOfClass:[NSArray class]];
Class FSBooleanClass = [FSBoolean class];
NSUInteger trueCount = 0;
if (!booleans) FSExecError(@"argument 1 of method \"where:put:\" must not be nil");
if (![booleans isKindOfClass:[NSArray class]]) FSExecError([NSString stringWithFormat:@"argument 1 of method \"where:put:\" is %@. An array was expected", descriptionForFSMessage(booleans)]);
if (count != [booleans count]) FSExecError(@"receiver and argument 1 of method \"where:put:\" must be arrays of same size");
for (NSUInteger i = 0; i < count; i++)
{
id boolean = [booleans objectAtIndex:i];
if (boolean == fsFalse || boolean == nil)
continue;
else if (boolean == fsTrue )
trueCount++;
else if ([boolean isKindOfClass:FSBooleanClass])
{
if ([boolean isTrue])
trueCount++;
}
else
FSExecError(@"argument 1 of method \"where:put:\" must be an array of booleans");
}
if (elemIsArray)
{
if ([elem count] < trueCount) FSExecError(@"argument 2 of method \"where:put:\" has not enough elements");
else if ([elem count] > trueCount) FSExecError(@"argument 2 of method \"where:put:\" has too many elements");
}
for (NSUInteger i = 0, j = 0; i < count; i++)
{
id boolean = [booleans objectAtIndex:i];
if (boolean == fsTrue || (boolean != fsFalse && [boolean isKindOfClass:FSBooleanClass] && [boolean isTrue]))
{
[self replaceObjectAtIndex:i withObject:elemIsArray ? [elem objectAtIndex:j] : elem];
j++;
}
}
return elem;
}
@end