antlr3bitset.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * \file
  3. * Defines the basic structures of an ANTLR3 bitset. this is a C version of the
  4. * cut down Bitset class provided with the java version of antlr 3.
  5. *
  6. *
  7. */
  8. #ifndef _ANTLR3_BITSET_HPP
  9. #define _ANTLR3_BITSET_HPP
  10. // [The "BSD licence"]
  11. // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB
  12. //
  13. // All rights reserved.
  14. //
  15. // Redistribution and use in source and binary forms, with or without
  16. // modification, are permitted provided that the following conditions
  17. // are met:
  18. // 1. Redistributions of source code must retain the above copyright
  19. // notice, this list of conditions and the following disclaimer.
  20. // 2. Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the distribution.
  23. // 3. The name of the author may not be used to endorse or promote products
  24. // derived from this software without specific prior written permission.
  25. //
  26. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  27. // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  28. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  29. // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  31. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  35. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. namespace antlr3 {
  37. /** How many bits in the elements
  38. */
  39. static const ANTLR_UINT32 ANTLR_BITSET_BITS = 64;
  40. /** How many bits in a nible of bits
  41. */
  42. static const ANTLR_UINT32 ANTLR_BITSET_NIBBLE = 4;
  43. /** log2 of ANTLR3_BITSET_BITS 2^ANTLR3_BITSET_LOG_BITS = ANTLR3_BITSET_BITS
  44. */
  45. static const ANTLR_UINT32 ANTLR_BITSET_LOG_BITS = 6;
  46. /** We will often need to do a mod operator (i mod nbits).
  47. * For powers of two, this mod operation is the
  48. * same as:
  49. * - (i & (nbits-1)).
  50. *
  51. * Since mod is relatively slow, we use an easily
  52. * precomputed mod mask to do the mod instead.
  53. */
  54. static const ANTLR_UINT32 ANTLR_BITSET_MOD_MASK = ANTLR_BITSET_BITS - 1;
  55. template <class ImplTraits>
  56. class BitsetList : public ImplTraits::AllocPolicyType
  57. {
  58. public:
  59. typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
  60. typedef typename ImplTraits::BitsetType BitsetType;
  61. private:
  62. /// Pointer to the allocated array of bits for this bit set, which
  63. /// is an array of 64 bit elements (of the architecture). If we find a
  64. /// machine/C compiler that does not know anything about 64 bit values
  65. /// then it should be easy enough to produce a 32 bit (or less) version
  66. /// of the bitset code. Note that the pointer here may be static if laid down
  67. /// by the code generation, and it must be copied if it is to be manipulated
  68. /// to perform followset calculations.
  69. ///
  70. ANTLR_BITWORD* m_bits;
  71. /// Length of the current bit set in ANTLR3_UINT64 units.
  72. ///
  73. ANTLR_UINT32 m_length;
  74. public:
  75. BitsetList();
  76. BitsetList( ANTLR_BITWORD* bits, ANTLR_UINT32 length );
  77. ANTLR_BITWORD* get_bits() const;
  78. ANTLR_UINT32 get_length() const;
  79. void set_bits( ANTLR_BITWORD* bits );
  80. void set_length( ANTLR_UINT32 length );
  81. ///
  82. /// \brief
  83. /// Creates a new bitset with at least one 64 bit bset of bits, but as
  84. /// many 64 bit sets as are required.
  85. ///
  86. /// \param[in] bset
  87. /// A variable number of bits to add to the set, ending in -1 (impossible bit).
  88. ///
  89. /// \returns
  90. /// A new bit set with all of the specified bitmaps in it and the API
  91. /// initialized.
  92. ///
  93. /// Call as:
  94. /// - pANTLR3_BITSET = antlrBitsetLoad(bset, bset11, ..., -1);
  95. /// - pANTLR3_BITSET = antlrBitsetOf(-1); Create empty bitset
  96. ///
  97. /// \remarks
  98. /// Stdargs function - must supply -1 as last paremeter, which is NOT
  99. /// added to the set.
  100. ///
  101. ///
  102. BitsetType* bitsetLoad();
  103. BitsetType* bitsetCopy();
  104. };
  105. template <class ImplTraits>
  106. class Bitset : public ImplTraits::AllocPolicyType
  107. {
  108. public:
  109. typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
  110. typedef typename AllocPolicyType::template ListType<ANTLR_UINT32> IntListType;
  111. typedef typename ImplTraits::BitsetListType BitsetListType;
  112. private:
  113. /// The actual bits themselves
  114. ///
  115. BitsetListType m_blist;
  116. public:
  117. Bitset( ANTLR_UINT32 nbits=0 );
  118. Bitset( const Bitset& bitset );
  119. Bitset* clone() const;
  120. Bitset* bor(Bitset* bitset2);
  121. BitsetListType& get_blist();
  122. void borInPlace(Bitset* bitset2);
  123. ANTLR_UINT32 size() const;
  124. void add(ANTLR_INT32 bit);
  125. void grow(ANTLR_INT32 newSize);
  126. bool equals(Bitset* bitset2) const;
  127. bool isMember(ANTLR_UINT32 bit) const;
  128. ANTLR_UINT32 numBits() const;
  129. void remove(ANTLR_UINT32 bit);
  130. bool isNilNode() const;
  131. /** Produce an integer list of all the bits that are turned on
  132. * in this bitset. Used for error processing in the main as the bitset
  133. * reresents a number of integer tokens which we use for follow sets
  134. * and so on.
  135. *
  136. * The first entry is the number of elements following in the list.
  137. */
  138. ANTLR_INT32* toIntList() const;
  139. ///
  140. /// \brief
  141. /// Creates a new bitset with at least one element, but as
  142. /// many elements are required.
  143. ///
  144. /// \param[in] bit
  145. /// A variable number of bits to add to the set, ending in -1 (impossible bit).
  146. ///
  147. /// \returns
  148. /// A new bit set with all of the specified elements added into it.
  149. ///
  150. /// Call as:
  151. /// - pANTLR3_BITSET = antlrBitsetOf(n, n1, n2, -1);
  152. /// - pANTLR3_BITSET = antlrBitsetOf(-1); Create empty bitset
  153. ///
  154. /// \remarks
  155. /// Stdargs function - must supply -1 as last paremeter, which is NOT
  156. /// added to the set.
  157. ///
  158. ///
  159. //C++ doesn't like variable length arguments. so use function overloading
  160. static Bitset* BitsetOf(ANTLR_INT32 bit);
  161. static Bitset* BitsetOf(ANTLR_INT32 bit1, ANTLR_INT32 bit2);
  162. ///
  163. /// \brief
  164. /// Creates a new bitset with at least one 64 bit bset of bits, but as
  165. /// many 64 bit sets as are required.
  166. ///
  167. /// \param[in] bset
  168. /// A variable number of bits to add to the set, ending in -1 (impossible bit).
  169. ///
  170. /// \returns
  171. /// A new bit set with all of the specified bitmaps in it and the API
  172. /// initialized.
  173. ///
  174. /// Call as:
  175. /// - pANTLR3_BITSET = antlrBitsetLoad(bset, bset11, ..., -1);
  176. /// - pANTLR3_BITSET = antlrBitsetOf(-1); Create empty bitset
  177. ///
  178. /// \remarks
  179. /// Stdargs function - must supply -1 as last paremeter, which is NOT
  180. /// added to the set.
  181. ///
  182. ///antlr3BitsetList
  183. static Bitset* BitsetFromList(const IntListType& list);
  184. ~Bitset();
  185. private:
  186. void growToInclude(ANTLR_INT32 bit);
  187. static ANTLR_UINT64 BitMask(ANTLR_UINT32 bitNumber);
  188. static ANTLR_UINT32 NumWordsToHold(ANTLR_UINT32 bit);
  189. static ANTLR_UINT32 WordNumber(ANTLR_UINT32 bit);
  190. void bitsetORInPlace(Bitset* bitset2);
  191. };
  192. }
  193. #include "antlr3bitset.inl"
  194. #endif