1 ///
2 /// \file
3 /// Contains the C implementation of ANTLR3 bitsets as adapted from Terence Parr's
4 /// Java implementation.
5 ///
6
7 // [The "BSD licence"]
8 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
9 // http://www.temporal-wave.com
10 // http://www.linkedin.com/in/jimidle
11 //
12 // All rights reserved.
13 //
14 // Redistribution and use in source and binary forms, with or without
15 // modification, are permitted provided that the following conditions
16 // are met:
17 // 1. Redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 // 3. The name of the author may not be used to endorse or promote products
23 // derived from this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 #include <antlr3bitset.h>
37
38 // External interface
39 //
40
41 static pANTLR3_BITSET antlr3BitsetClone (pANTLR3_BITSET inSet);
42 static pANTLR3_BITSET antlr3BitsetOR (pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2);
43 static void antlr3BitsetORInPlace (pANTLR3_BITSET bitset, pANTLR3_BITSET bitset2);
44 static ANTLR3_UINT32 antlr3BitsetSize (pANTLR3_BITSET bitset);
45 static void antlr3BitsetAdd (pANTLR3_BITSET bitset, ANTLR3_INT32 bit);
46 static ANTLR3_BOOLEAN antlr3BitsetEquals (pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2);
47 static ANTLR3_BOOLEAN antlr3BitsetMember (pANTLR3_BITSET bitset, ANTLR3_UINT32 bit);
48 static ANTLR3_UINT32 antlr3BitsetNumBits (pANTLR3_BITSET bitset);
49 static void antlr3BitsetRemove (pANTLR3_BITSET bitset, ANTLR3_UINT32 bit);
50 static ANTLR3_BOOLEAN antlr3BitsetIsNil (pANTLR3_BITSET bitset);
51 static pANTLR3_INT32 antlr3BitsetToIntList (pANTLR3_BITSET bitset);
52
53 // Local functions
54 //
55 static void growToInclude (pANTLR3_BITSET bitset, ANTLR3_INT32 bit);
56 static void grow (pANTLR3_BITSET bitset, ANTLR3_INT32 newSize);
57 static ANTLR3_UINT64 bitMask (ANTLR3_UINT32 bitNumber);
58 static ANTLR3_UINT32 numWordsToHold (ANTLR3_UINT32 bit);
59 static ANTLR3_UINT32 wordNumber (ANTLR3_UINT32 bit);
60 static void antlr3BitsetFree (pANTLR3_BITSET bitset);
61
62 static void
antlr3BitsetFree(pANTLR3_BITSET bitset)63 antlr3BitsetFree(pANTLR3_BITSET bitset)
64 {
65 if (bitset->blist.bits != NULL)
66 {
67 ANTLR3_FREE(bitset->blist.bits);
68 bitset->blist.bits = NULL;
69 }
70 ANTLR3_FREE(bitset);
71
72 return;
73 }
74
75 ANTLR3_API pANTLR3_BITSET
antlr3BitsetNew(ANTLR3_UINT32 numBits)76 antlr3BitsetNew(ANTLR3_UINT32 numBits)
77 {
78 pANTLR3_BITSET bitset;
79
80 ANTLR3_UINT32 numelements;
81
82 // Allocate memory for the bitset structure itself
83 //
84 bitset = (pANTLR3_BITSET) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_BITSET));
85
86 if (bitset == NULL)
87 {
88 return NULL;
89 }
90
91 // Avoid memory thrashing at the up front expense of a few bytes
92 //
93 if (numBits < (8 * ANTLR3_BITSET_BITS))
94 {
95 numBits = 8 * ANTLR3_BITSET_BITS;
96 }
97
98 // No we need to allocate the memory for the number of bits asked for
99 // in multiples of ANTLR3_UINT64.
100 //
101 numelements = ((numBits -1) >> ANTLR3_BITSET_LOG_BITS) + 1;
102
103 bitset->blist.bits = (pANTLR3_BITWORD) ANTLR3_MALLOC((size_t)(numelements * sizeof(ANTLR3_BITWORD)));
104 if (bitset->blist.bits == NULL)
105 {
106 ANTLR3_FREE(bitset);
107 return NULL;
108 }
109 memset(bitset->blist.bits, 0, (size_t)(numelements * sizeof(ANTLR3_BITWORD)));
110 bitset->blist.length = numelements;
111
112 antlr3BitsetSetAPI(bitset);
113
114
115 // All seems good
116 //
117 return bitset;
118 }
119
120 ANTLR3_API void
antlr3BitsetSetAPI(pANTLR3_BITSET bitset)121 antlr3BitsetSetAPI(pANTLR3_BITSET bitset)
122 {
123 bitset->clone = antlr3BitsetClone;
124 bitset->bor = antlr3BitsetOR;
125 bitset->borInPlace = antlr3BitsetORInPlace;
126 bitset->size = antlr3BitsetSize;
127 bitset->add = antlr3BitsetAdd;
128 bitset->grow = grow;
129 bitset->equals = antlr3BitsetEquals;
130 bitset->isMember = antlr3BitsetMember;
131 bitset->numBits = antlr3BitsetNumBits;
132 bitset->remove = antlr3BitsetRemove;
133 bitset->isNilNode = antlr3BitsetIsNil;
134 bitset->toIntList = antlr3BitsetToIntList;
135
136 bitset->free = antlr3BitsetFree;
137 }
138
139 ANTLR3_API pANTLR3_BITSET
antlr3BitsetCopy(pANTLR3_BITSET_LIST blist)140 antlr3BitsetCopy(pANTLR3_BITSET_LIST blist)
141 {
142 pANTLR3_BITSET bitset;
143 int numElements;
144
145 // Allocate memory for the bitset structure itself
146 //
147 bitset = (pANTLR3_BITSET) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_BITSET));
148
149 if (bitset == NULL)
150 {
151 return NULL;
152 }
153
154 numElements = blist->length;
155
156 // Avoid memory thrashing at the expense of a few more bytes
157 //
158 if (numElements < 8)
159 {
160 numElements = 8;
161 }
162
163 // Install the length in ANTLR3_UINT64 units
164 //
165 bitset->blist.length = numElements;
166
167 bitset->blist.bits = (pANTLR3_BITWORD)ANTLR3_MALLOC((size_t)(numElements * sizeof(ANTLR3_BITWORD)));
168
169 if (bitset->blist.bits == NULL)
170 {
171 ANTLR3_FREE(bitset);
172 return NULL;
173 }
174
175 ANTLR3_MEMCPY(bitset->blist.bits, blist->bits, (ANTLR3_UINT64)(numElements * sizeof(ANTLR3_BITWORD)));
176
177 // All seems good
178 //
179 return bitset;
180 }
181
182 static pANTLR3_BITSET
antlr3BitsetClone(pANTLR3_BITSET inSet)183 antlr3BitsetClone(pANTLR3_BITSET inSet)
184 {
185 pANTLR3_BITSET bitset;
186
187 // Allocate memory for the bitset structure itself
188 //
189 bitset = antlr3BitsetNew(ANTLR3_BITSET_BITS * inSet->blist.length);
190
191 if (bitset == NULL)
192 {
193 return NULL;
194 }
195
196 // Install the actual bits in the source set
197 //
198 ANTLR3_MEMCPY(bitset->blist.bits, inSet->blist.bits, (ANTLR3_UINT64)(inSet->blist.length * sizeof(ANTLR3_BITWORD)));
199
200 // All seems good
201 //
202 return bitset;
203 }
204
205
206 ANTLR3_API pANTLR3_BITSET
antlr3BitsetList(pANTLR3_HASH_TABLE list)207 antlr3BitsetList(pANTLR3_HASH_TABLE list)
208 {
209 pANTLR3_BITSET bitSet;
210 pANTLR3_HASH_ENUM en;
211 pANTLR3_HASH_KEY key;
212 ANTLR3_UINT64 bit;
213
214 // We have no idea what exactly is in the list
215 // so create a default bitset and then just add stuff
216 // as we enumerate.
217 //
218 bitSet = antlr3BitsetNew(0);
219
220 en = antlr3EnumNew(list);
221
222 while (en->next(en, &key, (void **)(&bit)) == ANTLR3_SUCCESS)
223 {
224 bitSet->add(bitSet, (ANTLR3_UINT32)bit);
225 }
226 en->free(en);
227
228 return NULL;
229 }
230
231 ///
232 /// \brief
233 /// Creates a new bitset with at least one 64 bit bset of bits, but as
234 /// many 64 bit sets as are required.
235 ///
236 /// \param[in] bset
237 /// A variable number of bits to add to the set, ending in -1 (impossible bit).
238 ///
239 /// \returns
240 /// A new bit set with all of the specified bitmaps in it and the API
241 /// initialized.
242 ///
243 /// Call as:
244 /// - pANTLR3_BITSET = antlrBitsetLoad(bset, bset11, ..., -1);
245 /// - pANTLR3_BITSET = antlrBitsetOf(-1); Create empty bitset
246 ///
247 /// \remarks
248 /// Stdargs function - must supply -1 as last paremeter, which is NOT
249 /// added to the set.
250 ///
251 ///
252 ANTLR3_API pANTLR3_BITSET
antlr3BitsetLoad(pANTLR3_BITSET_LIST inBits)253 antlr3BitsetLoad(pANTLR3_BITSET_LIST inBits)
254 {
255 pANTLR3_BITSET bitset;
256 ANTLR3_UINT32 count;
257
258 // Allocate memory for the bitset structure itself
259 // the input parameter is the bit number (0 based)
260 // to include in the bitset, so we need at at least
261 // bit + 1 bits. If any arguments indicate a
262 // a bit higher than the default number of bits (0 means default size)
263 // then Add() will take care
264 // of it.
265 //
266 bitset = antlr3BitsetNew(0);
267
268 if (bitset == NULL)
269 {
270 return NULL;
271 }
272
273 if (inBits != NULL)
274 {
275 // Now we can add the element bits into the set
276 //
277 count=0;
278 while (count < inBits->length)
279 {
280 if (bitset->blist.length <= count)
281 {
282 bitset->grow(bitset, count+1);
283 }
284
285 bitset->blist.bits[count] = *((inBits->bits)+count);
286 count++;
287 }
288 }
289
290 // return the new bitset
291 //
292 return bitset;
293 }
294
295 ///
296 /// \brief
297 /// Creates a new bitset with at least one element, but as
298 /// many elements are required.
299 ///
300 /// \param[in] bit
301 /// A variable number of bits to add to the set, ending in -1 (impossible bit).
302 ///
303 /// \returns
304 /// A new bit set with all of the specified elements added into it.
305 ///
306 /// Call as:
307 /// - pANTLR3_BITSET = antlrBitsetOf(n, n1, n2, -1);
308 /// - pANTLR3_BITSET = antlrBitsetOf(-1); Create empty bitset
309 ///
310 /// \remarks
311 /// Stdargs function - must supply -1 as last paremeter, which is NOT
312 /// added to the set.
313 ///
314 ///
315 ANTLR3_API pANTLR3_BITSET
antlr3BitsetOf(ANTLR3_INT32 bit,...)316 antlr3BitsetOf(ANTLR3_INT32 bit, ...)
317 {
318 pANTLR3_BITSET bitset;
319
320 va_list ap;
321
322 // Allocate memory for the bitset structure itself
323 // the input parameter is the bit number (0 based)
324 // to include in the bitset, so we need at at least
325 // bit + 1 bits. If any arguments indicate a
326 // a bit higher than the default number of bits (0 menas default size)
327 // then Add() will take care
328 // of it.
329 //
330 bitset = antlr3BitsetNew(0);
331
332 if (bitset == NULL)
333 {
334 return NULL;
335 }
336
337 // Now we can add the element bits into the set
338 //
339 va_start(ap, bit);
340 while (bit != -1)
341 {
342 antlr3BitsetAdd(bitset, bit);
343 bit = va_arg(ap, ANTLR3_UINT32);
344 }
345 va_end(ap);
346
347 // return the new bitset
348 //
349 return bitset;
350 }
351
352 static pANTLR3_BITSET
antlr3BitsetOR(pANTLR3_BITSET bitset1,pANTLR3_BITSET bitset2)353 antlr3BitsetOR(pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2)
354 {
355 pANTLR3_BITSET bitset;
356
357 if (bitset1 == NULL)
358 {
359 return antlr3BitsetClone(bitset2);
360 }
361
362 if (bitset2 == NULL)
363 {
364 return antlr3BitsetClone(bitset1);
365 }
366
367 // Allocate memory for the newly ordered bitset structure itself.
368 //
369 bitset = antlr3BitsetClone(bitset1);
370
371 antlr3BitsetORInPlace(bitset, bitset2);
372
373 return bitset;
374
375 }
376
377 static void
antlr3BitsetAdd(pANTLR3_BITSET bitset,ANTLR3_INT32 bit)378 antlr3BitsetAdd(pANTLR3_BITSET bitset, ANTLR3_INT32 bit)
379 {
380 ANTLR3_UINT32 word;
381
382 word = wordNumber(bit);
383
384 if (word >= bitset->blist.length)
385 {
386 growToInclude(bitset, bit);
387 }
388
389 bitset->blist.bits[word] |= bitMask(bit);
390
391 }
392
393 static void
grow(pANTLR3_BITSET bitset,ANTLR3_INT32 newSize)394 grow(pANTLR3_BITSET bitset, ANTLR3_INT32 newSize)
395 {
396 pANTLR3_BITWORD newBits;
397
398 // Space for newly sized bitset - TODO: come back to this and use realloc?, it may
399 // be more efficient...
400 //
401 newBits = (pANTLR3_BITWORD) ANTLR3_CALLOC(1, (size_t)(newSize * sizeof(ANTLR3_BITWORD)));
402 if (bitset->blist.bits != NULL)
403 {
404 // Copy existing bits
405 //
406 ANTLR3_MEMCPY((void *)newBits, (const void *)bitset->blist.bits, (size_t)(bitset->blist.length * sizeof(ANTLR3_BITWORD)));
407
408 // Out with the old bits... de de de derrr
409 //
410 ANTLR3_FREE(bitset->blist.bits);
411 }
412
413 // In with the new bits... keerrrang.
414 //
415 bitset->blist.bits = newBits;
416 bitset->blist.length = newSize;
417 }
418
419 static void
growToInclude(pANTLR3_BITSET bitset,ANTLR3_INT32 bit)420 growToInclude(pANTLR3_BITSET bitset, ANTLR3_INT32 bit)
421 {
422 ANTLR3_UINT32 bl;
423 ANTLR3_UINT32 nw;
424
425 bl = (bitset->blist.length << 1);
426 nw = numWordsToHold(bit);
427
428 if (bl > nw)
429 {
430 bitset->grow(bitset, bl);
431 }
432 else
433 {
434 bitset->grow(bitset, nw);
435 }
436 }
437
438 static void
antlr3BitsetORInPlace(pANTLR3_BITSET bitset,pANTLR3_BITSET bitset2)439 antlr3BitsetORInPlace(pANTLR3_BITSET bitset, pANTLR3_BITSET bitset2)
440 {
441 ANTLR3_UINT32 minimum;
442 ANTLR3_UINT32 i;
443
444 if (bitset2 == NULL)
445 {
446 return;
447 }
448
449
450 // First make sure that the target bitset is big enough
451 // for the new bits to be ored in.
452 //
453 if (bitset->blist.length < bitset2->blist.length)
454 {
455 growToInclude(bitset, (bitset2->blist.length * sizeof(ANTLR3_BITWORD)));
456 }
457
458 // Or the miniimum number of bits after any resizing went on
459 //
460 if (bitset->blist.length < bitset2->blist.length)
461 {
462 minimum = bitset->blist.length;
463 }
464 else
465 {
466 minimum = bitset2->blist.length;
467 }
468
469 for (i = minimum; i > 0; i--)
470 {
471 bitset->blist.bits[i-1] |= bitset2->blist.bits[i-1];
472 }
473 }
474
475 static ANTLR3_UINT64
bitMask(ANTLR3_UINT32 bitNumber)476 bitMask(ANTLR3_UINT32 bitNumber)
477 {
478 return ((ANTLR3_UINT64)1) << (bitNumber & (ANTLR3_BITSET_MOD_MASK));
479 }
480
481 static ANTLR3_UINT32
antlr3BitsetSize(pANTLR3_BITSET bitset)482 antlr3BitsetSize(pANTLR3_BITSET bitset)
483 {
484 ANTLR3_UINT32 degree;
485 ANTLR3_INT32 i;
486 ANTLR3_INT8 bit;
487
488 // TODO: Come back to this, it may be faster to & with 0x01
489 // then shift right a copy of the 4 bits, than shift left a constant of 1.
490 // But then again, the optimizer might just work this out
491 // anyway.
492 //
493 degree = 0;
494 for (i = bitset->blist.length - 1; i>= 0; i--)
495 {
496 if (bitset->blist.bits[i] != 0)
497 {
498 for (bit = ANTLR3_BITSET_BITS - 1; bit >= 0; bit--)
499 {
500 if ((bitset->blist.bits[i] & (((ANTLR3_BITWORD)1) << bit)) != 0)
501 {
502 degree++;
503 }
504 }
505 }
506 }
507 return degree;
508 }
509
510 static ANTLR3_BOOLEAN
antlr3BitsetEquals(pANTLR3_BITSET bitset1,pANTLR3_BITSET bitset2)511 antlr3BitsetEquals(pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2)
512 {
513 ANTLR3_INT32 minimum;
514 ANTLR3_INT32 i;
515
516 if (bitset1 == NULL || bitset2 == NULL)
517 {
518 return ANTLR3_FALSE;
519 }
520
521 // Work out the minimum comparison set
522 //
523 if (bitset1->blist.length < bitset2->blist.length)
524 {
525 minimum = bitset1->blist.length;
526 }
527 else
528 {
529 minimum = bitset2->blist.length;
530 }
531
532 // Make sure explict in common bits are equal
533 //
534 for (i = minimum - 1; i >=0 ; i--)
535 {
536 if (bitset1->blist.bits[i] != bitset2->blist.bits[i])
537 {
538 return ANTLR3_FALSE;
539 }
540 }
541
542 // Now make sure the bits of the larger set are all turned
543 // off.
544 //
545 if (bitset1->blist.length > (ANTLR3_UINT32)minimum)
546 {
547 for (i = minimum ; (ANTLR3_UINT32)i < bitset1->blist.length; i++)
548 {
549 if (bitset1->blist.bits[i] != 0)
550 {
551 return ANTLR3_FALSE;
552 }
553 }
554 }
555 else if (bitset2->blist.length > (ANTLR3_UINT32)minimum)
556 {
557 for (i = minimum; (ANTLR3_UINT32)i < bitset2->blist.length; i++)
558 {
559 if (bitset2->blist.bits[i] != 0)
560 {
561 return ANTLR3_FALSE;
562 }
563 }
564 }
565
566 return ANTLR3_TRUE;
567 }
568
569 static ANTLR3_BOOLEAN
antlr3BitsetMember(pANTLR3_BITSET bitset,ANTLR3_UINT32 bit)570 antlr3BitsetMember(pANTLR3_BITSET bitset, ANTLR3_UINT32 bit)
571 {
572 ANTLR3_UINT32 wordNo;
573
574 wordNo = wordNumber(bit);
575
576 if (wordNo >= bitset->blist.length)
577 {
578 return ANTLR3_FALSE;
579 }
580
581 if ((bitset->blist.bits[wordNo] & bitMask(bit)) == 0)
582 {
583 return ANTLR3_FALSE;
584 }
585 else
586 {
587 return ANTLR3_TRUE;
588 }
589 }
590
591 static void
antlr3BitsetRemove(pANTLR3_BITSET bitset,ANTLR3_UINT32 bit)592 antlr3BitsetRemove(pANTLR3_BITSET bitset, ANTLR3_UINT32 bit)
593 {
594 ANTLR3_UINT32 wordNo;
595
596 wordNo = wordNumber(bit);
597
598 if (wordNo < bitset->blist.length)
599 {
600 bitset->blist.bits[wordNo] &= ~(bitMask(bit));
601 }
602 }
603 static ANTLR3_BOOLEAN
antlr3BitsetIsNil(pANTLR3_BITSET bitset)604 antlr3BitsetIsNil(pANTLR3_BITSET bitset)
605 {
606 ANTLR3_INT32 i;
607
608 for (i = bitset->blist.length -1; i>= 0; i--)
609 {
610 if (bitset->blist.bits[i] != 0)
611 {
612 return ANTLR3_FALSE;
613 }
614 }
615
616 return ANTLR3_TRUE;
617 }
618
619 static ANTLR3_UINT32
numWordsToHold(ANTLR3_UINT32 bit)620 numWordsToHold(ANTLR3_UINT32 bit)
621 {
622 return (bit >> ANTLR3_BITSET_LOG_BITS) + 1;
623 }
624
625 static ANTLR3_UINT32
wordNumber(ANTLR3_UINT32 bit)626 wordNumber(ANTLR3_UINT32 bit)
627 {
628 return bit >> ANTLR3_BITSET_LOG_BITS;
629 }
630
631 static ANTLR3_UINT32
antlr3BitsetNumBits(pANTLR3_BITSET bitset)632 antlr3BitsetNumBits(pANTLR3_BITSET bitset)
633 {
634 return bitset->blist.length << ANTLR3_BITSET_LOG_BITS;
635 }
636
637 /** Produce an integer list of all the bits that are turned on
638 * in this bitset. Used for error processing in the main as the bitset
639 * reresents a number of integer tokens which we use for follow sets
640 * and so on.
641 *
642 * The first entry is the number of elements following in the list.
643 */
644 static pANTLR3_INT32
antlr3BitsetToIntList(pANTLR3_BITSET bitset)645 antlr3BitsetToIntList (pANTLR3_BITSET bitset)
646 {
647 ANTLR3_UINT32 numInts; // How many integers we will need
648 ANTLR3_UINT32 numBits; // How many bits are in the set
649 ANTLR3_UINT32 i;
650 ANTLR3_UINT32 index;
651
652 pANTLR3_INT32 intList;
653
654 numInts = bitset->size(bitset) + 1;
655 numBits = bitset->numBits(bitset);
656
657 intList = (pANTLR3_INT32)ANTLR3_MALLOC(numInts * sizeof(ANTLR3_INT32));
658
659 if (intList == NULL)
660 {
661 return NULL; // Out of memory
662 }
663
664 intList[0] = numInts;
665
666 // Enumerate the bits that are turned on
667 //
668 for (i = 0, index = 1; i<numBits; i++)
669 {
670 if (bitset->isMember(bitset, i) == ANTLR3_TRUE)
671 {
672 intList[index++] = i;
673 }
674 }
675
676 // Result set
677 //
678 return intList;
679 }
680
681