xref: /aosp_15_r20/external/antlr/runtime/C/include/antlr3bitset.h (revision 16467b971bd3e2009fad32dd79016f2c7e421deb)
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_H
9*16467b97STreehugger Robot #define	_ANTLR3_BITSET_H
10*16467b97STreehugger Robot 
11*16467b97STreehugger Robot // [The "BSD licence"]
12*16467b97STreehugger Robot // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
13*16467b97STreehugger Robot // http://www.temporal-wave.com
14*16467b97STreehugger Robot // http://www.linkedin.com/in/jimidle
15*16467b97STreehugger Robot //
16*16467b97STreehugger Robot // All rights reserved.
17*16467b97STreehugger Robot //
18*16467b97STreehugger Robot // Redistribution and use in source and binary forms, with or without
19*16467b97STreehugger Robot // modification, are permitted provided that the following conditions
20*16467b97STreehugger Robot // are met:
21*16467b97STreehugger Robot // 1. Redistributions of source code must retain the above copyright
22*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer.
23*16467b97STreehugger Robot // 2. Redistributions in binary form must reproduce the above copyright
24*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer in the
25*16467b97STreehugger Robot //    documentation and/or other materials provided with the distribution.
26*16467b97STreehugger Robot // 3. The name of the author may not be used to endorse or promote products
27*16467b97STreehugger Robot //    derived from this software without specific prior written permission.
28*16467b97STreehugger Robot //
29*16467b97STreehugger Robot // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30*16467b97STreehugger Robot // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31*16467b97STreehugger Robot // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32*16467b97STreehugger Robot // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33*16467b97STreehugger Robot // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34*16467b97STreehugger Robot // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35*16467b97STreehugger Robot // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36*16467b97STreehugger Robot // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37*16467b97STreehugger Robot // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38*16467b97STreehugger Robot // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39*16467b97STreehugger Robot 
40*16467b97STreehugger Robot #include    <antlr3defs.h>
41*16467b97STreehugger Robot #include    <antlr3collections.h>
42*16467b97STreehugger Robot 
43*16467b97STreehugger Robot /** How many bits in the elements
44*16467b97STreehugger Robot  */
45*16467b97STreehugger Robot #define	ANTLR3_BITSET_BITS	64
46*16467b97STreehugger Robot 
47*16467b97STreehugger Robot /** How many bits in a nible of bits
48*16467b97STreehugger Robot  */
49*16467b97STreehugger Robot #define	ANTLR3_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 #define	ANTLR3_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 #define	ANTLR3_BITSET_MOD_MASK	ANTLR3_BITSET_BITS - 1
64*16467b97STreehugger Robot 
65*16467b97STreehugger Robot #ifdef __cplusplus
66*16467b97STreehugger Robot extern "C" {
67*16467b97STreehugger Robot #endif
68*16467b97STreehugger Robot 
69*16467b97STreehugger Robot typedef struct ANTLR3_BITSET_LIST_struct
70*16467b97STreehugger Robot {
71*16467b97STreehugger Robot 	/// Pointer to the allocated array of bits for this bit set, which
72*16467b97STreehugger Robot     /// is an array of 64 bit elements (of the architecture). If we find a
73*16467b97STreehugger Robot     /// machine/C compiler that does not know anything about 64 bit values
74*16467b97STreehugger Robot     ///	then it should be easy enough to produce a 32 bit (or less) version
75*16467b97STreehugger Robot     /// of the bitset code. Note that the pointer here may be static if laid down
76*16467b97STreehugger Robot 	/// by the code generation, and it must be copied if it is to be manipulated
77*16467b97STreehugger Robot 	/// to perform followset calculations.
78*16467b97STreehugger Robot     ///
79*16467b97STreehugger Robot     pANTLR3_BITWORD   bits;
80*16467b97STreehugger Robot 
81*16467b97STreehugger Robot     /// Length of the current bit set in ANTLR3_UINT64 units.
82*16467b97STreehugger Robot     ///
83*16467b97STreehugger Robot     ANTLR3_UINT32    length;
84*16467b97STreehugger Robot }
85*16467b97STreehugger Robot 	ANTLR3_BITSET_LIST;
86*16467b97STreehugger Robot 
87*16467b97STreehugger Robot typedef	struct ANTLR3_BITSET_struct
88*16467b97STreehugger Robot {
89*16467b97STreehugger Robot 	/// The actual bits themselves
90*16467b97STreehugger Robot 	///
91*16467b97STreehugger Robot 	ANTLR3_BITSET_LIST				blist;
92*16467b97STreehugger Robot 
93*16467b97STreehugger Robot     pANTLR3_BITSET					(*clone)	    (struct ANTLR3_BITSET_struct  * inSet);
94*16467b97STreehugger Robot     pANTLR3_BITSET					(*bor)			(struct ANTLR3_BITSET_struct  * bitset1, struct ANTLR3_BITSET_struct * bitset2);
95*16467b97STreehugger Robot     void							(*borInPlace)   (struct ANTLR3_BITSET_struct  * bitset,  struct ANTLR3_BITSET_struct * bitset2);
96*16467b97STreehugger Robot     ANTLR3_UINT32					(*size)			(struct ANTLR3_BITSET_struct  * bitset);
97*16467b97STreehugger Robot     void							(*add)			(struct ANTLR3_BITSET_struct  * bitset, ANTLR3_INT32 bit);
98*16467b97STreehugger Robot     void							(*grow)			(struct ANTLR3_BITSET_struct  * bitset, ANTLR3_INT32 newSize);
99*16467b97STreehugger Robot     ANTLR3_BOOLEAN					(*equals)	    (struct ANTLR3_BITSET_struct  * bitset1, struct ANTLR3_BITSET_struct * bitset2);
100*16467b97STreehugger Robot     ANTLR3_BOOLEAN					(*isMember)	    (struct ANTLR3_BITSET_struct  * bitset, ANTLR3_UINT32 bit);
101*16467b97STreehugger Robot     ANTLR3_UINT32					(*numBits)	    (struct ANTLR3_BITSET_struct  * bitset);
102*16467b97STreehugger Robot     void							(*remove)	    (struct ANTLR3_BITSET_struct  * bitset, ANTLR3_UINT32 bit);
103*16467b97STreehugger Robot     ANTLR3_BOOLEAN					(*isNilNode)	    (struct ANTLR3_BITSET_struct  * bitset);
104*16467b97STreehugger Robot     pANTLR3_INT32					(*toIntList)    (struct ANTLR3_BITSET_struct  * bitset);
105*16467b97STreehugger Robot 
106*16467b97STreehugger Robot     void							(*free)			(struct ANTLR3_BITSET_struct  * bitset);
107*16467b97STreehugger Robot 
108*16467b97STreehugger Robot 
109*16467b97STreehugger Robot }
110*16467b97STreehugger Robot     ANTLR3_BITSET;
111*16467b97STreehugger Robot 
112*16467b97STreehugger Robot #ifdef __cplusplus
113*16467b97STreehugger Robot }
114*16467b97STreehugger Robot #endif
115*16467b97STreehugger Robot 
116*16467b97STreehugger Robot 
117*16467b97STreehugger Robot 
118*16467b97STreehugger Robot #endif
119*16467b97STreehugger Robot 
120