forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathArrayRepBoolean.m
324 lines (260 loc) · 7.73 KB
/
ArrayRepBoolean.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
/* ArrayRepBoolean.m Copyright (c) 1998-2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "build_config.h"
#import "ArrayRepBoolean.h"
#import "BlockPrivate.h"
#import "BlockRep.h"
#import "BlockInspector.h"
#import "string.h" // memcpy()
#import "ArrayPrivate.h"
#import "FSNumber.h"
#import "FScriptFunctions.h"
#import "FSBooleanPrivate.h"
#import "ArrayRepId.h"
#import "FSCompiler.h"
#import "FSExecEngine.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
@implementation ArrayRepBoolean
////////////////////////////// USER METHODS SUPPORT /////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
- (id)operator_backslash:(FSBlock*)bl // May raise
{
NSUInteger i;
id args[3];
if ([bl isCompact])
{
SEL selector = [bl selector];
NSString *selectorStr = [bl selectorStr];
FSMsgContext *msgContext = [bl msgContext];
long acu = t[0];
args[1] = (id)(selector ? selector : [FSCompiler selectorFromString:selectorStr]);
if (selector == @selector(operator_ampersand:))
{
for (i = 0; i < count && t[i]; i++);
return (i == count ? (id)fsTrue : (id)fsFalse);
}
else if (selector == @selector(operator_bar:))
{
for (i = 0; i < count && !t[i]; i++);
return (i == count ? (id)fsFalse : (id)fsTrue);
}
else if (selector == @selector(operator_plus:))
{
for (i = 1; i < count; i++) acu += (t[i] ? 1 : 0);
return [FSNumber numberWithDouble:acu];
}
else
{
args[0] = (t[0] ? (id)fsTrue : (id)fsFalse);
for (i = 1; i < count; i++)
{
args[2] = (t[i] ? (id)fsTrue : (id)fsFalse);
args[0] = sendMsg(args[0], selector, 3, args, nil, msgContext, nil); // May raise
}
} // end if
}
else
{
BlockRep *blRep = [bl blockRep];
args[0] = (t[0] ? (id)fsTrue : (id)fsFalse);
for (i = 1; i < count; i++)
{
args[2] = (t[i] ? (id)fsTrue : (id)fsFalse);
args[0] = [blRep body_notCompact_valueArgs:args count:3 block:bl];
}
}
return args[0];
}
///////////////////////////// OPTIM ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
/*-------------------------------------- double loop ----------------------------------*/
// note: for all the doubleLoop... methods, precondition contains: [[operand arrayRep] isKindOfClass:[ArrayRepBoolean class]] && ![operand isProxy]
- (FSArray *)doubleLoop_operator_ampersand:(FSArray *)operand
{
char *opData = [[operand arrayRep] booleansPtr];
char *resTab;
NSUInteger i;
NSUInteger nb = MIN(count, [operand count]);
//NSLog(@"doubleLoop_operator_ampersand:");
resTab = malloc(nb*sizeof(char));
for(i=0; i < nb; i++) resTab[i] = t[i] && opData[i];
return [[[FSArray alloc] initWithRepNoRetain:[[ArrayRepBoolean alloc] initWithBooleansNoCopy:resTab count:nb]] autorelease];
}
- (FSArray *)doubleLoop_operator_bar:(FSArray *)operand
{
char *resTab;
NSUInteger i;
NSUInteger nb = MIN(count, [operand count]);
char *opData = [[operand arrayRep] booleansPtr];
//NSLog(@"doubleLoop_operator_bar:");
resTab = malloc(nb*sizeof(char));
for(i=0; i < nb; i++)
{
resTab[i] = t[i] || opData[i];
}
return [[[FSArray alloc] initWithRepNoRetain:[[ArrayRepBoolean alloc] initWithBooleansNoCopy:resTab count:nb]] autorelease];
}
/*-------------------------------------- simple loop ----------------------------------*/
- (FSArray *)simpleLoop_not
{
char *resTab;
NSUInteger i;
//NSLog(@"simpleLoop_operator_not");
resTab = malloc(count*sizeof(char));
for(i=0;i<count;i++) resTab[i] = !t[i];
return [[[FSArray alloc] initWithRepNoRetain:[[ArrayRepBoolean alloc] initWithBooleansNoCopy:resTab count:count]] autorelease];
}
/////////////////////////////// OTHER METHODS //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
+ (void)initialize
{
static BOOL tooLate = NO;
if ( !tooLate ) {
tooLate = YES;
}
}
- (void)addBoolean:(char)aBoolean
{
count++;
if (count > capacity)
{
capacity = (capacity+1)*2;
t = (char *)realloc(t, capacity * sizeof(char));
}
t[count-1] = aBoolean;
}
- (ArrayRepId *) asArrayRepId
{
NSUInteger i;
id *tab = (id *) NSAllocateCollectable(count * sizeof(id), NSScannedOption);
ArrayRepId *r = [[[ArrayRepId alloc] initWithObjectsNoCopy:tab count:count] autorelease];
for (i = 0; i < count; i++) tab[i] = (t[i] ? (id)fsTrue : (id)fsFalse);
return r;
}
- (char *)booleansPtr {return t;}
- (id)copyWithZone:(NSZone *)zone
{
return [[ArrayRepBoolean allocWithZone:zone] initWithBooleans:t count:count];
}
- (NSUInteger)count {return count;}
- (void)dealloc
{
free(t);
[super dealloc];
}
- (void)finalize
{
free(t);
[super finalize];
}
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range identical:(BOOL)identical
{
if (range.length == 0 || (identical && !(anObject == fsTrue || anObject == fsFalse)) || ![anObject isKindOfClass:[FSBoolean class]]) return NSNotFound;
else
{
NSUInteger i = range.location;
NSUInteger end = (range.location + range.length -1);
char val = ([anObject isTrue] ? 1:0);
while (i <= end && t[i] != val)
{
i++;
}
return (i > end) ? NSNotFound : i;
}
}
- (id)init { return [self initWithCapacity:0]; }
- (id)initFilledWithBoolean:(char)elem count:(NSUInteger)nb // contract: a return value of nil means not enough memory
{
if (self = [self initWithCapacity:nb])
{
for (count = 0; count < nb; count++) t[count] = elem;
return self;
}
return nil;
}
- (id)initWithCapacity:(NSUInteger)aNumItems // contract: a return value of nil means not enough memory
{
if ((self = [super init]))
{
t = malloc(aNumItems*sizeof(char));
if (!t)
{
[super dealloc];
return nil;
}
retainCount = 1;
capacity = aNumItems;
count = 0;
return self;
}
return nil;
}
- (id)initWithBooleans:(char *)elems count:(NSUInteger)nb
{
if (self = [self initWithCapacity:nb])
{
memcpy(t,elems,nb*sizeof(char));
count = nb;
return self;
}
return nil;
}
- (id)initWithBooleansNoCopy:(char *)tab count:(NSUInteger)nb
{
if ((self = [super init]))
{
retainCount = 1;
t = tab;
capacity = nb;
count = nb;
return self;
}
return nil;
}
- (void)removeLastElem
{
count--;
if (capacity/2 >= count+100)
{
capacity = capacity/2;
t = (char *)realloc(t, capacity * sizeof(char));
}
}
- (void)removeElemAtIndex:(NSUInteger)index
{
count--;
memmove( &(t[index]), &(t[index+1]), (count-index) * sizeof(char));
if (capacity/2 >= count+100)
{
capacity = capacity/2;
t = (char *)realloc(t, capacity * sizeof(char));
}
}
- (void)replaceBooleanAtIndex:(NSUInteger)index withBoolean:(char)aBoolean
{
t[index] = aBoolean;
}
- (id)retain { retainCount++; return self;}
- (NSUInteger)retainCount { return retainCount;}
- (oneway void)release { if (--retainCount == 0) [self dealloc];}
- (NSArray *)subarrayWithRange:(NSRange)range
{
ArrayRepBoolean *resRep;
FSArray *r;
resRep = [[ArrayRepBoolean alloc] initWithBooleans:t+range.location count:range.length];
r = [FSArray arrayWithRep:resRep];
[resRep release];
return r;
}
- (enum ArrayRepType)repType {return BOOLEAN;}
@end