forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathArrayRepId.m
765 lines (617 loc) · 20 KB
/
ArrayRepId.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/* ArrayRepId.m Copyright (c) 1998-2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "build_config.h"
#import "ArrayRepId.h"
#import "FSArray.h"
#import "FScriptFunctions.h"
#import <Foundation/Foundation.h>
#import "FSBooleanPrivate.h"
#import "FSCompiler.h"
#import "FSVoid.h"
#import <objc/objc-runtime.h>
#import <objc/objc-auto.h>
#import "FSBlock.h"
#import "BlockRep.h"
#import "BlockPrivate.h"
#import "BlockInspector.h"
#import "FSExecEngine.h"
#import "FSPattern.h"
#import "FSExecEngine.h" // sendMsg
#import "FSNumber.h"
#import "NumberPrivate.h"
#import <string.h>
#import "ArrayPrivate.h"
#import "ArrayRepDouble.h"
#import "ArrayRepBoolean.h"
#import "FSMiscTools.h"
#ifndef MAX
#define MAX(a, b) \
({typeof(a) _a = (a); typeof(b) _b = (b); \
_a > _b ? _a : _b; })
#endif
#ifndef MIN
#define MIN(a, b) \
({typeof(a) _a = (a); typeof(b) _b = (b); \
_a < _b ? _a : _b; })
#endif
@interface ArrayRepId(ArrayRepIdPrivate)
- (void) addObjectsFromFSArray:(FSArray *)otherArray;
@end
@implementation ArrayRepId
/*typedef struct {
unsigned long state;
id *itemsPtr;
unsigned long *mutationsPtr;
unsigned long extra[5];
} NSFastEnumerationState;*/
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)statep objects:(id *)stackbuf count:(NSUInteger)len
{
if (statep->state == 0)
{
statep->state = 1;
statep->itemsPtr = t;
statep->mutationsPtr = (unsigned long *)self;
return count;
}
else return 0;
}
///////////////////////////////////// USER METHODS
- (id)at:(id)index put:(id)elem
{
double ind;
id oldElem;
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");
oldElem = t[(NSUInteger)ind];
t[(NSUInteger)ind] = [elem retain];
[oldElem release];
}
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]) )
{
oldElem = t[i];
t[i] = [(elemIsArray ? [elem objectAtIndex:j] : elem) retain];
[oldElem release];
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];
oldElem = t[(NSUInteger)ind];
t[(NSUInteger)ind] = [(elemIsArray ? [elem objectAtIndex:i] : elem) retain];
[oldElem release];
i++;
while (i < nb && ![index objectAtIndex:i]) i++; // ignore the nil values
}
}
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;
}
- (id)operator_backslash:(FSBlock*)bl // may raise
{
NSUInteger i;
id args[3];
args[0] = t[0];
if ([bl isCompact])
{
SEL selector = [bl selector];
NSString *selectorStr = [bl selectorStr];
FSMsgContext *msgContext = [bl msgContext];
args[1] = (id)(selector ? selector : [FSCompiler selectorFromString:selectorStr]);
for (i = 1; i < count; i++)
{
args[2] = t[i];
args[0] = sendMsg(args[0], selector, 3, args, nil, msgContext, nil);
}
}
else
{
BlockRep *blRep = [bl blockRep];
for (i = 1; i < count; i++)
{
args[2] = t[i];
args[0] = [blRep body_notCompact_valueArgs:args count:3 block:bl];
}
}
return args[0];
}
- (FSArray *)replicateWithArray:(FSArray *)operand
{
FSArray *r;
NSUInteger i,j;
FSArray *index = operand;
assert(![operand isProxy]);
r = [FSArray array];
switch ([index type])
{
case FS_ID:
{
id *indexData = [operand dataPtr];
for (i=0; i < count; i++)
{
double opElemDouble;
if (![indexData[i] isKindOfClass:NSNumberClass]) FSExecError(@"argument 1 of method \"replicate:\" must be an array of numbers");
opElemDouble = [(NSNumber *)(indexData[i]) doubleValue];
if (opElemDouble < 0) FSExecError(@"argument 1 of method \"replicate:\" must not contain negative numbers");
if (opElemDouble > NSUIntegerMax) FSExecError([NSString stringWithFormat:@"argument of method \"replicate:\" must contain numbers less or equal to %lu", (unsigned long)NSUIntegerMax]);
for (j=0; j < opElemDouble; j++) [r addObject:t[i]];
}
break;
}
case DOUBLE:
{
double *indexData = [(ArrayRepDouble *)[operand arrayRep] doublesPtr];
for (i=0; i < count; i++)
{
double opElemDouble = indexData[i];
if (opElemDouble < 0) FSExecError(@"argument 1 of method \"replicate:\" must not contain negative numbers");
if (opElemDouble > NSUIntegerMax)
FSExecError([NSString stringWithFormat:@"argument of method \"replicate:\" must contain numbers less or equal to %lu", (unsigned long)NSUIntegerMax]);
for (j=0; j < opElemDouble; j++) [r addObject:t[i]];
}
break;
}
case BOOLEAN:
if ([operand count] != 0) FSExecError(@"argument 1 of method \"replicate:\" is an array of Booleans. An array of numbers was expected");
break;
case EMPTY: break;
case FETCH_REQUEST:
[operand becomeArrayOfId];
return [self indexWithArray:operand];
} // end switch
return r;
}
- (FSArray *)rotatedBy:(NSNumber *)operand
{
FSArray *r;
NSUInteger i;
NSInteger op;
VERIF_OP_NSNUMBER(@"rotatedBy:");
if (count == 0) return [FSArray array];
op = [operand doubleValue];
r = [FSArray arrayWithCapacity:count];
if (op >= 0)
{
for(i = op % count ; i < count; i++)
[r addObject:t[i]];
for (i = 0; i < op % count; i++)
[r addObject:t[i]];
}
else
{
op = -op;
for(i = count-(op % count) ; i < count; i++)
[r addObject:t[i]];
for (i = 0; i < count-(op % count); i++)
[r addObject:t[i]];
}
return r;
}
///////////////////////////////////////////////////////////////////////////
//////////////////////////////////// OTHER METHOD /////////////////////////
///////////////////////////////////////////////////////////////////////////
- (void)addObject:(id)anObject
{
count++;
if (count > capacity)
{
capacity = (capacity+1)*2;
t = (id *)NSReallocateCollectable(t, capacity * sizeof(id), NSScannedOption);
}
t[count-1] = [anObject retain];
}
- (void)addObjectsFromFSArray:(FSArray *)otherArray
{
NSUInteger i;
NSUInteger oldCount = count;
id *otherArrayData = [otherArray dataPtr];
NSUInteger otherArrayCount = [otherArray count];
count += otherArrayCount;
if (count > capacity)
{
capacity = count;
t = (id *)NSReallocateCollectable(t, capacity * sizeof(id), NSScannedOption);
}
for (i = 0; i < otherArrayCount; i++)
t[oldCount+i] = [otherArrayData[i] retain];
}
- (ArrayRepId *) asArrayRepId
{ return self;}
- (id)copyWithZone:(NSZone *)zone
{
return [[[self class] allocWithZone:zone] initWithObjects:t count:count];
}
- (NSUInteger)count
{ return count; }
- (void *)dataPtr
{return t;}
- (void)dealloc
{
NSUInteger i;
//printf("\n arrayRep : dealloc\n");
for (i = 0; i < count; i++) [t[i] release];
free(t);
[super dealloc];
}
- (NSString *)descriptionLimited:(NSUInteger)nbElem
{
NSMutableString *str = [[@"{" mutableCopy] autorelease];
NSString *elemStr = @""; // W
NSUInteger i;
NSUInteger lim = MIN(count,nbElem);
if (lim > 0)
{
elemStr = printString(t[0]);
[str appendString:elemStr];
}
for (i = 1; i < lim; i++)
{
[str appendString:@", "];
if ([elemStr length] > 20) [str appendString:@"\n"];
elemStr = printString(t[i]);
[str appendString:elemStr];
}
if (count > nbElem) [str appendFormat:@", ... (%lu more elements)",(unsigned long)(count-nbElem)];
[str appendString:@"}"];
return str;
}
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range identical:(BOOL)identical
{
if (range.length == 0) return NSNotFound;
else
{
NSUInteger i = range.location;
NSUInteger end = (range.location + range.length -1);
while ( i <= end && !( identical ? t[i] == anObject : ([t[i] isEqual:anObject] || (t[i] == nil && anObject == nil)) ) )
{
i++;
}
return (i > end) ? NSNotFound : i;
}
}
- (id)indexWithArray:(FSArray *)index
{
switch ([index type])
{
case FS_ID:
{
FSArray *r;
id *indexData;
NSUInteger i = 0;
NSUInteger nb = [index count];
indexData = [index dataPtr];
while (i < nb && !indexData[i]) i++; // ignore the nil value
if (i == nb)
{
if (nb == count || nb == 0) return [FSArray array];
else FSExecError(@"invalid index");
}
if (indexData[i] == fsTrue || indexData[i] == fsFalse || [indexData[i] isKindOfClass:[FSBoolean class]])
{
if (nb != count) FSExecError(@"indexing with an array of booleans of bad size");
r = [FSArray array];
while (i < nb)
{
if (indexData[i] == fsTrue) [r addObject:t[i]];
else if (indexData[i] != fsFalse)
{
if (![indexData[i] isKindOfClass:[FSBoolean class]]) FSExecError(@"indexing with a mixed array");
else if ([indexData[i] isTrue]) [r addObject:t[i]];
}
i++;
while (i < nb && !indexData[i]) i++; // ignore the nil value
}
}
else if ([indexData[i] isKindOfClass:NSNumberClass])
{
r = [FSArray arrayWithCapacity:nb];
while (i < nb)
{
double ind;
if (![indexData[i] isKindOfClass:NSNumberClass]) FSExecError(@"indexing with a mixed array");
ind = [indexData[i] doubleValue];
/*
if (ind != (int)ind)
FSExecError(@"l'indice d'un tableau doit etre un nombre sans "
@"partie fractionnaire");
*/
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");
[r addObject:t[(NSUInteger)ind]];
i++;
while (i < nb && !indexData[i]) i++; // ignore the nil value
}
}
else // indexData[i] is neither an NSNumber nor a FSBoolean
{
FSExecError([NSString stringWithFormat:@"array indexing by an array containing %@", descriptionForFSMessage(indexData[i])]);
return nil; // W
}
return r;
}
case BOOLEAN:
{
NSUInteger i;
FSArray *r;
char *indexData;
if ([index count] == 0) return [FSArray array];
if ([index count] != count) FSExecError(@"indexing with an array of booleans of bad size");
r = [FSArray array];
indexData = [(ArrayRepBoolean *)[index arrayRep] booleansPtr];
for (i = 0; i < count; i++) if (indexData[i]) [r addObject:t[i]];
return r;
}
case DOUBLE:
{
FSArray *r;
double *indexData;
NSUInteger i = 0;
NSUInteger nb = [index count];
if (i == nb) return [FSArray array];
indexData = [(ArrayRepDouble *)[index arrayRep] doublesPtr];
r = [FSArray arrayWithCapacity:nb];
while (i < nb)
{
double ind = indexData[i];
/*
if (ind != (int)ind)
FSExecError(@"l'indice d'un tableau doit etre un nombre sans "
@"partie fractionnaire");
*/
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");
[r addObject:t[(NSUInteger)ind]];
i++;
}
return r;
}
case EMPTY: return [FSArray array];
case FETCH_REQUEST:
[index becomeArrayOfId];
return [self indexWithArray:index];
} // end switch
return nil; // W
}
- (id)init
{
return [self initWithCapacity:0];
}
/*- initFrom:(unsigned)from to:(unsigned)to step:(unsigned)step
{
if (to < from) return [self init];
if ([self initWithCapacity:1+((to-from)/step)])
{
double valcou = from;
do
{
t[count++] = [[[Number alloc] initWithDouble:valcou] retain];
valcou += step;
}
while (valcou <= to);
return self;
}
return nil;
} */
- (id)initFilledWith:(id)elem count:(NSUInteger)nb
{
if (self = [self initWithCapacity:nb])
{
for(count = 0; count < nb; count++) t[count] = [elem retain];
return self;
}
return nil;
}
- (id)initWithCapacity:(NSUInteger)aNumItems
{
if ((self = [super init]))
{
t = NSAllocateCollectable(aNumItems*sizeof(id), NSScannedOption);
if (!t)
{
[super dealloc];
return nil;
}
retainCount = 1;
capacity = aNumItems;
count = 0;
return self;
}
return nil;
}
- (id)initWithObjectsNoCopy:(id *)tab count:(NSUInteger)nb
{
if ((self = [super init]))
{
retainCount = 1;
t = tab;
capacity = nb;
count = nb;
return self;
}
return nil;
}
- (id)initWithObjects:(id *)objects count:(NSUInteger)nb
{
NSUInteger i;
if (self = [self initWithCapacity:nb])
{
for (i = 0; i < nb; i++)
{
t[i] = [objects[i] retain];
}
count = nb;
return self;
}
return nil;
}
- (void)insertObject:anObject atIndex:(NSUInteger)index
{
if (index > count) [NSException raise:NSRangeException format:@"index beyond the end of the array in method -insertObject:atIndex"];
count++ ;
if (count > capacity)
{
capacity = (capacity+1)*2;
t = (id*)NSReallocateCollectable(t, capacity * sizeof(id), NSScannedOption);
}
objc_memmove_collectable( &(t[index+1]), &(t[index]), ((count-1)-index) * sizeof(id));
t[index] = [anObject retain];
}
- (id)objectAtIndex:(NSUInteger)index
{
if (index >= count) [NSException raise:NSRangeException format:@"index beyond the end of the array in method -ObjectAtIndex:"];
return t[index];
}
- (void)removeLastElem
{
[self removeLastObject];
}
- (void)removeLastObject
{
if (count == 0) [NSException raise:NSRangeException format:@"-removeLastObject called on an empty array"];
[t[count-1] release];
count--;
if (capacity/2 >= count+100)
{
capacity = capacity/2;
t = (id*)NSReallocateCollectable(t, capacity * sizeof(id), NSScannedOption);
}
}
- (void)removeElemAtIndex:(NSUInteger)index
{
[self removeObjectAtIndex:index];
}
- (void)removeObjectAtIndex:(NSUInteger)index
{
if (index >= count) [NSException raise:NSRangeException format:@"index beyond the end of the array in method -removeObjectAtIndex:"];
[t[index] release];
count--;
objc_memmove_collectable( &(t[index]), &(t[index+1]), (count-index) * sizeof(id) );
if (capacity/2 >= count+100)
{
capacity = capacity/2;
t = (id*)NSReallocateCollectable(t, capacity * sizeof(id), NSScannedOption);
}
}
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
{
if (index >= count) [NSException raise:NSRangeException format:@"index beyond the end of the array in method -replaceObjectAtIndex:withObject:"];
[anObject retain];
[t[index] release];
t[index] = anObject;
}
- (id)retain { retainCount++; return self;}
- (NSUInteger)retainCount { return retainCount;}
- (oneway void)release { if (--retainCount == 0) [self dealloc];}
- (NSArray *)subarrayWithRange:(NSRange)range
{
ArrayRepId *resRep;
FSArray *r;
resRep = [[ArrayRepId alloc] initWithObjects:t+range.location count:range.length];
r = [FSArray arrayWithRep:resRep];
[resRep release];
return r;
}
- (enum ArrayRepType)repType { return FS_ID;}
- (FSArray *)where:(NSArray *)booleans // precondition: booleans is actualy an array and is of same size as the receiver
{
FSArray *result = [FSArray array];
if ([booleans isKindOfClass:[FSArray class]] && [(FSArray *)booleans type] == BOOLEAN)
{
char *rawBooleans = [(ArrayRepBoolean *)[(FSArray *)booleans arrayRep] booleansPtr];
for (NSUInteger i = 0; i < count; i++) if (rawBooleans[i]) [result addObject:t[i]];
}
else
{
id boolean;
for (NSUInteger i = 0; i < count; i++)
{
boolean = [booleans objectAtIndex:i];
if (boolean == fsFalse || boolean == nil)
continue;
else if (boolean == fsTrue)
[result addObject:t[i]];
else if ([boolean isKindOfClass:[FSBoolean class]])
{
if ([boolean isTrue])
[result addObject:t[i]];
}
else
FSExecError(@"argument of method \"where:\" must be an array of booleans");
}
}
return result;
}
@end