-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathANTLRBitSet.m
553 lines (479 loc) · 13.8 KB
/
ANTLRBitSet.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
/* ANTLRInputBuffer.m
Copyright (C) 1998, 1999 MDlink online service center, Helge Hess and Manuel Guesdon
All rights reserved.
Authors: Helge Hess <[email protected]>
Manuel Guesdon <[email protected]>
This file is part of the ANTLR Objective-C Library.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
We disclaim all warranties with regard to this software, including all
implied warranties of merchantability and fitness, in no event shall
we be liable for any special, indirect or consequential damages or any
damages whatsoever resulting from loss of use, data or profits, whether in
an action of contract, negligence or other tortious action, arising out of
or in connection with the use or performance of this software.
---------------------------------------------------------------------------
This code is based on ANTLR toolkit (http://www.antlr.org)
from MageLang Institute:
"We encourage users to develop software with ANTLR. However,
we do ask that credit is given to us for developing
ANTLR. By "credit", we mean that if you use ANTLR or
incorporate any source code into one of your programs
(commercial product, research project, or otherwise) that
you acknowledge this fact somewhere in the documentation,
research report, etc... If you like ANTLR and have
developed a nice tool with the output, please mention that
you developed it using ANTLR."
*/
#include "ANTLRCommon.h"
#include "ANTLRBitSet.h"
#define ANTLRStorageSize sizeof(ANTLRBitSetStorage)
#define ANTLRBitsPerEntry (ANTLRStorageSize * 8)
#define ANTLRByteSize (universe / 8)
#define ANTLRTestBit(_x) (((storage[ _x / ANTLRBitsPerEntry ] & \
(1 << (_x % ANTLRBitsPerEntry))) == 0) ? NO : YES)
//====================================================================
@interface ANTLRConcreteBitSetEnumerator : NSEnumerator
{
@public
unsigned int universe;
unsigned int count;
ANTLRBitSetStorage *storage;
unsigned int position;
unsigned int found;
};
-(id)nextObject;
@end
//====================================================================
@implementation ANTLRBitSet
//--------------------------------------------------------------------
+(id)bitSet
{
return AUTORELEASE([self new]);
}
//--------------------------------------------------------------------
+(id)bitSetWithCapacity:(unsigned)_capacity
{
return AUTORELEASE([[self alloc] initWithCapacity:_capacity]);
};
//--------------------------------------------------------------------
+(id)bitSetWithBitSet:(ANTLRBitSet*)_set
{
return AUTORELEASE([[self alloc] initWithBitSet:_set]);
}
//--------------------------------------------------------------------
+(id)bitSetWithULongBits:(unsigned long*)_bits
length:(unsigned)_length
{
id bitSet=[self bitSetWithCapacity:_length*sizeof(long)];
int i;
for(i=0;i<_length;i++)
{
int j=0;
for(j=0;j<sizeof(long)*8;j++)
{
if ((unsigned long)(_bits[i]) & (1UL << j))
[bitSet addMember:(i*sizeof(long)*8+j)];
};
};
return bitSet;
};
//--------------------------------------------------------------------
-(id)initWithCapacity:(unsigned)_capacity
{
LOGObjectFnStart();
if ((self = [super init]))
{
self->universe = (_capacity / ANTLRBitsPerEntry + 1) * ANTLRBitsPerEntry;
storage = ANTLR_ALLOC_ATOMIC(ANTLRByteSize);
memset(storage, 0, ANTLRByteSize);
count = 0;
};
LOGObjectFnStop();
return self;
};
//--------------------------------------------------------------------
-(id)initWithBitSet:(ANTLRBitSet*)_set
{
LOGObjectFnStart();
if ((self = [self initWithCapacity:ANTLRBitsPerEntry]))
{
NSEnumerator *enumerator = [_set objectEnumerator];
id obj = nil;
while ((obj = [enumerator nextObject]))
[self addMember:[obj unsignedIntValue]];
enumerator = nil;
};
LOGObjectFnStop();
return self;
};
//--------------------------------------------------------------------
- (id)init
{
return [self initWithCapacity:ANTLRBitsPerEntry];
};
//--------------------------------------------------------------------
-(id)initWithNullTerminatedArray:(unsigned int*)_array
{
LOGObjectFnStart();
if ((self = [self initWithCapacity:ANTLRBitsPerEntry]))
{
while (*_array)
{
[self addMember:*_array];
_array++;
};
};
LOGObjectFnStop();
return self;
}
//--------------------------------------------------------------------
#if !ANTLR_GC
- (void)dealloc
{
ANTLR_FREE(storage);
[super dealloc];
}
#endif
//--------------------------------------------------------------------
// storage
-(void)_expandToInclude:(unsigned int)_element
{
unsigned int nu = (_element / ANTLRBitsPerEntry + 1) * ANTLRBitsPerEntry;
LOGObjectFnStart();
if (nu > self->universe)
{
void *old = storage;
storage = (ANTLRBitSetStorage *)ANTLR_ALLOC_ATOMIC(nu / 8);
memset(storage, 0, nu / 8);
if (old)
{
memcpy(storage, old, ANTLRByteSize);
ANTLR_FREE(old);
};
self->universe = nu;
};
LOGObjectFnStop();
};
// accessors
//--------------------------------------------------------------------
-(unsigned int)capacity
{
return self->universe;
};
//--------------------------------------------------------------------
-(unsigned int)count
{
return count;
};
// membership
//--------------------------------------------------------------------
-(BOOL)isMember:(unsigned int)_element
{
BOOL isMember=NO;
LOGObjectFnStart();
isMember=(_element >= self->universe) ? NO : ANTLRTestBit(_element);
LOGObjectFnStop();
return isMember;
};
//--------------------------------------------------------------------
-(void)addMember:(unsigned int)_element
{
register unsigned int subIdxPattern = 1 << (_element % ANTLRBitsPerEntry);
// LOGObjectFnStart();
if (_element >= self->universe)
[self _expandToInclude:_element];
if ((storage[ _element / ANTLRBitsPerEntry ] & subIdxPattern) == 0)
{
storage[ _element / ANTLRBitsPerEntry ] |= subIdxPattern;
count++;
};
// LOGObjectFnStop();
};
//--------------------------------------------------------------------
-(void)addMembersInRange:(NSRange)_range
{
register unsigned int from = _range.location;
register unsigned int to = from + _range.length - 1;
LOGObjectFnStart();
if (to >= self->universe)
[self _expandToInclude:to];
for (; from <= to; from++)
{
register unsigned int subIdxPattern = 1 << (from % ANTLRBitsPerEntry);
if ((storage[ from / ANTLRBitsPerEntry ] & subIdxPattern) == 0)
{
storage[ from / ANTLRBitsPerEntry ] |= subIdxPattern;
count++;
};
};
LOGObjectFnStop();
};
//--------------------------------------------------------------------
-(void)addMembersFromBitSet:(ANTLRBitSet*)_set
{
int i;
LOGObjectFnStart();
if ([_set capacity] > self->universe)
[self _expandToInclude:[_set capacity]];
for (i = 0; i < [_set capacity]; i++)
{
if ([_set isMember:i])
{
register unsigned int subIdxPattern = 1 << (i % ANTLRBitsPerEntry);
if ((storage[ i / ANTLRBitsPerEntry ] & subIdxPattern) == 0)
{
storage[ i / ANTLRBitsPerEntry ] |= subIdxPattern;
count++;
};
};
};
LOGObjectFnStop();
};
//--------------------------------------------------------------------
-(void)removeMember:(unsigned int)_element
{
register unsigned int subIdxPattern = 1 << (_element % ANTLRBitsPerEntry);
LOGObjectFnStart();
if (_element < self->universe)
{
if ((storage[ _element / ANTLRBitsPerEntry ] & subIdxPattern) != 0)
{
storage[ _element / ANTLRBitsPerEntry ] -= subIdxPattern;
count--;
};
};
LOGObjectFnStop();
};
//--------------------------------------------------------------------
-(void)removeMembersInRange:(NSRange)_range
{
register unsigned int from = _range.location;
register unsigned int to = from + _range.length - 1;
if (from < self->universe)
{
if (to >= self->universe)
to = self->universe - 1;
for (; from <= to; from++)
{
register unsigned int subIdxPattern = 1 << (from % ANTLRBitsPerEntry);
if ((storage[ from / ANTLRBitsPerEntry ] & subIdxPattern) != 0)
{
storage[ from / ANTLRBitsPerEntry ] -= subIdxPattern;
count--;
};
};
};
LOGObjectFnStop();
};
//--------------------------------------------------------------------
-(void)removeAllMembers
{
LOGObjectFnStart();
memset(storage, 0, ANTLRByteSize);
count = 0;
LOGObjectFnStop();
};
//--------------------------------------------------------------------
-(unsigned int)firstMember
{
register unsigned int element;
unsigned int firstMember=NSNotFound;
LOGObjectFnStart();
for (element = 0;firstMember==NSNotFound && element < self->universe; element++)
{
if (ANTLRTestBit(element))
firstMember=element;
};
LOGObjectFnStop();
return firstMember;
};
//--------------------------------------------------------------------
-(unsigned int)lastMember
{
register unsigned int element;
unsigned int lastMember=NSNotFound;
LOGObjectFnStart();
for (element = (self->universe - 1);lastMember==NSNotFound && element >= 0; element--)
{
if (ANTLRTestBit(element))
lastMember=element;
};
LOGObjectFnStop();
return lastMember;
};
// equality
//--------------------------------------------------------------------
-(BOOL)isEqual:(id)_object
{
BOOL equal=YES;
LOGObjectFnStart();
if (self == _object)
equal=YES;
else if ([self class] != [_object class])
equal=NO;
else
equal=[self isEqualToSet:_object];
LOGObjectFnStop();
return equal;
};
//--------------------------------------------------------------------
-(BOOL)isEqualToSet:(ANTLRBitSet *)_set
{
BOOL equal=YES;
LOGObjectFnStart();
if (self == _set)
equal=YES;
else if (count != [_set count])
equal=NO;
else
{
register unsigned int element;
for (element = 0;equal && element < self->universe; element++)
{
if (ANTLRTestBit(element))
{
if (![_set isMember:element])
equal=NO;
};
};
};
LOGObjectFnStop();
return equal;
};
// enumerator
//--------------------------------------------------------------------
-(NSEnumerator *)objectEnumerator
{
if (self->count == 0)
return nil;
else
{
ANTLRConcreteBitSetEnumerator *en = [[ANTLRConcreteBitSetEnumerator alloc] init];
en->universe = self->universe;
en->count = self->count;
en->storage = self->storage;
return AUTORELEASE(en);
};
};
// NSCopying
//--------------------------------------------------------------------
-(id)copy
{
return [self copyWithZone:[self zone]];
};
//--------------------------------------------------------------------
-(id)copyWithZone:(NSZone *)_zone
{
return [[ANTLRBitSet alloc] initWithBitSet:self];
};
//--------------------------------------------------------------------
// NSCoding
-(void)encodeWithCoder:(NSCoder *)_coder
{
unsigned int element;
register unsigned int found;
LOGObjectFnStart();
[_coder encodeValueOfObjCType:@encode(unsigned int) at:&count];
for (element = 0, found = 0; (element < self->universe) && (found < count); element++)
{
if (ANTLRTestBit(element))
{
[_coder encodeValueOfObjCType:@encode(unsigned int) at:&element];
found++;
};
};
LOGObjectFnStop();
};
//--------------------------------------------------------------------
-(id)initWithCoder:(NSCoder*)_coder
{
LOGObjectFnStart();
if ((self = [super init]))
{
unsigned int nc;
register unsigned int cnt;
self->universe = ANTLRBitsPerEntry;
storage = ANTLR_ALLOC_ATOMIC(ANTLRByteSize);
memset(storage, 0, ANTLRByteSize);
[_coder decodeValueOfObjCType:@encode(unsigned int) at:&nc];
for (cnt = 0; cnt < nc; cnt++)
{
unsigned int member;
[_coder decodeValueOfObjCType:@encode(unsigned int) at:&member];
[self addMember:member];
};
};
LOGObjectFnStop();
return self;
};
//--------------------------------------------------------------------
// description
-(NSString *)description
{
return [NSString stringWithFormat:
@"<ANTLRBitSet[0x%08X]: capacity=%u count=%u content=%@>",
(unsigned)self,
self->universe,
self->count,
[[self toArray] description]];
};
//--------------------------------------------------------------------
-(NSArray *)toArray
{
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:count + 1];
register unsigned int element, found;
LOGObjectFnStart();
for (element = 0, found = 0;
(element < self->universe) && (found < self->count); element++)
{
if (ANTLRTestBit(element))
{
[result addObject:[NSNumber numberWithUnsignedInt:element]];
found++;
};
};
LOGObjectFnStop();
return AUTORELEASE([AUTORELEASE(result) copy]);
};
@end
//====================================================================
@implementation ANTLRConcreteBitSetEnumerator
//--------------------------------------------------------------------
-(id)nextObject
{
if (self->found == self->count)
return nil;
else if (self->position >= self->universe)
return nil;
else
{
while (!ANTLRTestBit(self->position))
self->position++;
self->found++;
self->position++;
return [NSNumber numberWithUnsignedInt:(self->position - 1)];
};
};
@end
//--------------------------------------------------------------------
NSString *stringValueForBitset(unsigned int _set, char _setC, char _unsetC, short _wide)
{
char buf[_wide + 1];
register short pos;
for (pos = 0; pos < _wide; pos++)
{
register unsigned int v = (1 << pos);
buf[(int)pos] = ((v & _set) == v) ? _setC : _unsetC;
};
buf[_wide] = '\0';
return [NSString stringWithCString:buf];
};
//--------------------------------------------------------------------
void __link_ANTLRExtensions_ANTLRBitSet()
{
__link_ANTLRExtensions_ANTLRBitSet();
};