3 /// Contains the C implementation of ANTLR3 bitsets as adapted from Terence Parr's
4 /// Java implementation.
8 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
9 // http://www.temporal-wave.com
10 // http://www.linkedin.com/in/jimidle
12 // All rights reserved.
14 // Redistribution and use in source and binary forms, with or without
15 // modification, are permitted provided that the following conditions
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.
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.
36 #include <antlr3bitset.h>
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);
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);
63 antlr3BitsetFree(pANTLR3_BITSET bitset)
65 if (bitset->blist.bits != NULL)
67 ANTLR3_FREE(bitset->blist.bits);
68 bitset->blist.bits = NULL;
75 ANTLR3_API pANTLR3_BITSET
76 antlr3BitsetNew(ANTLR3_UINT32 numBits)
78 pANTLR3_BITSET bitset;
80 ANTLR3_UINT32 numelements;
82 // Allocate memory for the bitset structure itself
84 bitset = (pANTLR3_BITSET) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_BITSET));
91 // Avoid memory thrashing at the up front expense of a few bytes
93 if (numBits < (8 * ANTLR3_BITSET_BITS))
95 numBits = 8 * ANTLR3_BITSET_BITS;
98 // No we need to allocate the memory for the number of bits asked for
99 // in multiples of ANTLR3_UINT64.
101 numelements = ((numBits -1) >> ANTLR3_BITSET_LOG_BITS) + 1;
103 bitset->blist.bits = (pANTLR3_BITWORD) ANTLR3_MALLOC((size_t)(numelements * sizeof(ANTLR3_BITWORD)));
104 memset(bitset->blist.bits, 0, (size_t)(numelements * sizeof(ANTLR3_BITWORD)));
105 bitset->blist.length = numelements;
107 if (bitset->blist.bits == NULL)
113 antlr3BitsetSetAPI(bitset);
122 antlr3BitsetSetAPI(pANTLR3_BITSET bitset)
124 bitset->clone = antlr3BitsetClone;
125 bitset->bor = antlr3BitsetOR;
126 bitset->borInPlace = antlr3BitsetORInPlace;
127 bitset->size = antlr3BitsetSize;
128 bitset->add = antlr3BitsetAdd;
130 bitset->equals = antlr3BitsetEquals;
131 bitset->isMember = antlr3BitsetMember;
132 bitset->numBits = antlr3BitsetNumBits;
133 bitset->remove = antlr3BitsetRemove;
134 bitset->isNilNode = antlr3BitsetIsNil;
135 bitset->toIntList = antlr3BitsetToIntList;
137 bitset->free = antlr3BitsetFree;
140 ANTLR3_API pANTLR3_BITSET
141 antlr3BitsetCopy(pANTLR3_BITSET_LIST blist)
143 pANTLR3_BITSET bitset;
146 // Allocate memory for the bitset structure itself
148 bitset = (pANTLR3_BITSET) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_BITSET));
155 numElements = blist->length;
157 // Avoid memory thrashing at the expense of a few more bytes
164 // Install the length in ANTLR3_UINT64 units
166 bitset->blist.length = numElements;
168 bitset->blist.bits = (pANTLR3_BITWORD)ANTLR3_MALLOC((size_t)(numElements * sizeof(ANTLR3_BITWORD)));
170 if (bitset->blist.bits == NULL)
176 ANTLR3_MEMCPY(bitset->blist.bits, blist->bits, (ANTLR3_UINT64)(numElements * sizeof(ANTLR3_BITWORD)));
183 static pANTLR3_BITSET
184 antlr3BitsetClone(pANTLR3_BITSET inSet)
186 pANTLR3_BITSET bitset;
188 // Allocate memory for the bitset structure itself
190 bitset = antlr3BitsetNew(ANTLR3_BITSET_BITS * inSet->blist.length);
197 // Install the actual bits in the source set
199 ANTLR3_MEMCPY(bitset->blist.bits, inSet->blist.bits, (ANTLR3_UINT64)(inSet->blist.length * sizeof(ANTLR3_BITWORD)));
207 ANTLR3_API pANTLR3_BITSET
208 antlr3BitsetList(pANTLR3_HASH_TABLE list)
210 pANTLR3_BITSET bitSet;
211 pANTLR3_HASH_ENUM en;
212 pANTLR3_HASH_KEY key;
215 // We have no idea what exactly is in the list
216 // so create a default bitset and then just add stuff
219 bitSet = antlr3BitsetNew(0);
221 en = antlr3EnumNew(list);
223 while (en->next(en, &key, (void **)(&bit)) == ANTLR3_SUCCESS)
225 bitSet->add(bitSet, (ANTLR3_UINT32)bit);
234 /// Creates a new bitset with at least one 64 bit bset of bits, but as
235 /// many 64 bit sets as are required.
238 /// A variable number of bits to add to the set, ending in -1 (impossible bit).
241 /// A new bit set with all of the specified bitmaps in it and the API
245 /// - pANTLR3_BITSET = antlrBitsetLoad(bset, bset11, ..., -1);
246 /// - pANTLR3_BITSET = antlrBitsetOf(-1); Create empty bitset
249 /// Stdargs function - must supply -1 as last paremeter, which is NOT
250 /// added to the set.
253 ANTLR3_API pANTLR3_BITSET
254 antlr3BitsetLoad(pANTLR3_BITSET_LIST inBits)
256 pANTLR3_BITSET bitset;
259 // Allocate memory for the bitset structure itself
260 // the input parameter is the bit number (0 based)
261 // to include in the bitset, so we need at at least
262 // bit + 1 bits. If any arguments indicate a
263 // a bit higher than the default number of bits (0 means default size)
264 // then Add() will take care
267 bitset = antlr3BitsetNew(0);
276 // Now we can add the element bits into the set
279 while (count < inBits->length)
281 if (bitset->blist.length <= count)
283 bitset->grow(bitset, count+1);
286 bitset->blist.bits[count] = *((inBits->bits)+count);
291 // return the new bitset
298 /// Creates a new bitset with at least one element, but as
299 /// many elements are required.
302 /// A variable number of bits to add to the set, ending in -1 (impossible bit).
305 /// A new bit set with all of the specified elements added into it.
308 /// - pANTLR3_BITSET = antlrBitsetOf(n, n1, n2, -1);
309 /// - pANTLR3_BITSET = antlrBitsetOf(-1); Create empty bitset
312 /// Stdargs function - must supply -1 as last paremeter, which is NOT
313 /// added to the set.
316 ANTLR3_API pANTLR3_BITSET
317 antlr3BitsetOf(ANTLR3_INT32 bit, ...)
319 pANTLR3_BITSET bitset;
323 // Allocate memory for the bitset structure itself
324 // the input parameter is the bit number (0 based)
325 // to include in the bitset, so we need at at least
326 // bit + 1 bits. If any arguments indicate a
327 // a bit higher than the default number of bits (0 menas default size)
328 // then Add() will take care
331 bitset = antlr3BitsetNew(0);
338 // Now we can add the element bits into the set
343 antlr3BitsetAdd(bitset, bit);
344 bit = va_arg(ap, ANTLR3_UINT32);
348 // return the new bitset
353 static pANTLR3_BITSET
354 antlr3BitsetOR(pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2)
356 pANTLR3_BITSET bitset;
360 return antlr3BitsetClone(bitset2);
365 return antlr3BitsetClone(bitset1);
368 // Allocate memory for the newly ordered bitset structure itself.
370 bitset = antlr3BitsetClone(bitset1);
372 antlr3BitsetORInPlace(bitset, bitset2);
379 antlr3BitsetAdd(pANTLR3_BITSET bitset, ANTLR3_INT32 bit)
383 word = wordNumber(bit);
385 if (word >= bitset->blist.length)
387 growToInclude(bitset, bit);
390 bitset->blist.bits[word] |= bitMask(bit);
395 grow(pANTLR3_BITSET bitset, ANTLR3_INT32 newSize)
397 pANTLR3_BITWORD newBits;
399 // Space for newly sized bitset - TODO: come back to this and use realloc?, it may
400 // be more efficient...
402 newBits = (pANTLR3_BITWORD) ANTLR3_CALLOC(1, (size_t)(newSize * sizeof(ANTLR3_BITWORD)));
403 if (bitset->blist.bits != NULL)
405 // Copy existing bits
407 ANTLR3_MEMCPY((void *)newBits, (const void *)bitset->blist.bits, (size_t)(bitset->blist.length * sizeof(ANTLR3_BITWORD)));
409 // Out with the old bits... de de de derrr
411 ANTLR3_FREE(bitset->blist.bits);
414 // In with the new bits... keerrrang.
416 bitset->blist.bits = newBits;
417 bitset->blist.length = newSize;
421 growToInclude(pANTLR3_BITSET bitset, ANTLR3_INT32 bit)
426 bl = (bitset->blist.length << 1);
427 nw = numWordsToHold(bit);
431 bitset->grow(bitset, bl);
435 bitset->grow(bitset, nw);
440 antlr3BitsetORInPlace(pANTLR3_BITSET bitset, pANTLR3_BITSET bitset2)
442 ANTLR3_UINT32 minimum;
451 // First make sure that the target bitset is big enough
452 // for the new bits to be ored in.
454 if (bitset->blist.length < bitset2->blist.length)
456 growToInclude(bitset, (bitset2->blist.length * sizeof(ANTLR3_BITWORD)));
459 // Or the miniimum number of bits after any resizing went on
461 if (bitset->blist.length < bitset2->blist.length)
463 minimum = bitset->blist.length;
467 minimum = bitset2->blist.length;
470 for (i = minimum; i > 0; i--)
472 bitset->blist.bits[i-1] |= bitset2->blist.bits[i-1];
477 bitMask(ANTLR3_UINT32 bitNumber)
479 return ((ANTLR3_UINT64)1) << (bitNumber & (ANTLR3_BITSET_MOD_MASK));
483 antlr3BitsetSize(pANTLR3_BITSET bitset)
485 ANTLR3_UINT32 degree;
489 // TODO: Come back to this, it may be faster to & with 0x01
490 // then shift right a copy of the 4 bits, than shift left a constant of 1.
491 // But then again, the optimizer might just work this out
495 for (i = bitset->blist.length - 1; i>= 0; i--)
497 if (bitset->blist.bits[i] != 0)
499 for (bit = ANTLR3_BITSET_BITS - 1; bit >= 0; bit--)
501 if ((bitset->blist.bits[i] & (((ANTLR3_BITWORD)1) << bit)) != 0)
511 static ANTLR3_BOOLEAN
512 antlr3BitsetEquals(pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2)
514 ANTLR3_INT32 minimum;
517 if (bitset1 == NULL || bitset2 == NULL)
522 // Work out the minimum comparison set
524 if (bitset1->blist.length < bitset2->blist.length)
526 minimum = bitset1->blist.length;
530 minimum = bitset2->blist.length;
533 // Make sure explict in common bits are equal
535 for (i = minimum - 1; i >=0 ; i--)
537 if (bitset1->blist.bits[i] != bitset2->blist.bits[i])
543 // Now make sure the bits of the larger set are all turned
546 if (bitset1->blist.length > (ANTLR3_UINT32)minimum)
548 for (i = minimum ; (ANTLR3_UINT32)i < bitset1->blist.length; i++)
550 if (bitset1->blist.bits[i] != 0)
556 else if (bitset2->blist.length > (ANTLR3_UINT32)minimum)
558 for (i = minimum; (ANTLR3_UINT32)i < bitset2->blist.length; i++)
560 if (bitset2->blist.bits[i] != 0)
570 static ANTLR3_BOOLEAN
571 antlr3BitsetMember(pANTLR3_BITSET bitset, ANTLR3_UINT32 bit)
573 ANTLR3_UINT32 wordNo;
575 wordNo = wordNumber(bit);
577 if (wordNo >= bitset->blist.length)
582 if ((bitset->blist.bits[wordNo] & bitMask(bit)) == 0)
593 antlr3BitsetRemove(pANTLR3_BITSET bitset, ANTLR3_UINT32 bit)
595 ANTLR3_UINT32 wordNo;
597 wordNo = wordNumber(bit);
599 if (wordNo < bitset->blist.length)
601 bitset->blist.bits[wordNo] &= ~(bitMask(bit));
604 static ANTLR3_BOOLEAN
605 antlr3BitsetIsNil(pANTLR3_BITSET bitset)
609 for (i = bitset->blist.length -1; i>= 0; i--)
611 if (bitset->blist.bits[i] != 0)
621 numWordsToHold(ANTLR3_UINT32 bit)
623 return (bit >> ANTLR3_BITSET_LOG_BITS) + 1;
627 wordNumber(ANTLR3_UINT32 bit)
629 return bit >> ANTLR3_BITSET_LOG_BITS;
633 antlr3BitsetNumBits(pANTLR3_BITSET bitset)
635 return bitset->blist.length << ANTLR3_BITSET_LOG_BITS;
638 /** Produce an integer list of all the bits that are turned on
639 * in this bitset. Used for error processing in the main as the bitset
640 * reresents a number of integer tokens which we use for follow sets
643 * The first entry is the number of elements following in the list.
646 antlr3BitsetToIntList (pANTLR3_BITSET bitset)
648 ANTLR3_UINT32 numInts; // How many integers we will need
649 ANTLR3_UINT32 numBits; // How many bits are in the set
653 pANTLR3_INT32 intList;
655 numInts = bitset->size(bitset) + 1;
656 numBits = bitset->numBits(bitset);
658 intList = (pANTLR3_INT32)ANTLR3_MALLOC(numInts * sizeof(ANTLR3_INT32));
662 return NULL; // Out of memory
665 intList[0] = numInts;
667 // Enumerate the bits that are turned on
669 for (i = 0, index = 1; i<numBits; i++)
671 if (bitset->isMember(bitset, i) == ANTLR3_TRUE)
673 intList[index++] = i;