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