aboutsummaryrefslogtreecommitdiff
path: root/runtime/CSharp3/Sources/Antlr3.Runtime/BitSet.cs
blob: b18611b614e0eb57b67751bbb0bc913c988e088e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * [The "BSD licence"]
 * Copyright (c) 2005-2008 Terence Parr
 * All rights reserved.
 *
 * Conversion to C#:
 * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

namespace Antlr.Runtime
{
    using System.Collections.Generic;

    using Array = System.Array;
    using CLSCompliant = System.CLSCompliantAttribute;
    using ICloneable = System.ICloneable;
    using Math = System.Math;
    using StringBuilder = System.Text.StringBuilder;

    /** <summary>
     *  A stripped-down version of org.antlr.misc.BitSet that is just
     *  good enough to handle runtime requirements such as FOLLOW sets
     *  for automatic error recovery.
     *  </summary>
     */
    [System.Serializable]
    public sealed class BitSet : ICloneable
    {
        private const int BITS = 64;    // number of bits / long
        private const int LOG_BITS = 6; // 2^6 == 64

        /** <summary>
         *  We will often need to do a mod operator (i mod nbits).  Its
         *  turns out that, for powers of two, this mod operation is
         *  same as (i & (nbits-1)).  Since mod is slow, we use a
         *  precomputed mod mask to do the mod instead.
         *  </summary>
         */
        private const int MOD_MASK = BITS - 1;

        /** <summary>The actual data bits</summary> */
        ulong[] _bits;

        /** <summary>Construct a bitset of size one word (64 bits)</summary> */
        public BitSet()
            : this( BITS )
        {
        }

        /** <summary>Construction from a static array of longs</summary> */
        [CLSCompliant( false )]
        public BitSet( ulong[] bits )
        {
            _bits = bits;
        }

        /** <summary>Construction from a list of integers</summary> */
        public BitSet( IEnumerable<int> items )
            : this()
        {
            foreach ( int i in items )
                Add( i );
        }

        /** <summary>Construct a bitset given the size</summary>
         *  <param name="nbits">The size of the bitset in bits</param>
         */
        public BitSet( int nbits )
        {
            _bits = new ulong[( ( nbits - 1 ) >> LOG_BITS ) + 1];
        }

        public static BitSet Of( int el )
        {
            BitSet s = new BitSet( el + 1 );
            s.Add( el );
            return s;
        }

        public static BitSet Of( int a, int b )
        {
            BitSet s = new BitSet( Math.Max( a, b ) + 1 );
            s.Add( a );
            s.Add( b );
            return s;
        }

        public static BitSet Of( int a, int b, int c )
        {
            BitSet s = new BitSet();
            s.Add( a );
            s.Add( b );
            s.Add( c );
            return s;
        }

        public static BitSet Of( int a, int b, int c, int d )
        {
            BitSet s = new BitSet();
            s.Add( a );
            s.Add( b );
            s.Add( c );
            s.Add( d );
            return s;
        }

        /** <summary>return this | a in a new set</summary> */
        public BitSet Or( BitSet a )
        {
            if ( a == null )
            {
                return this;
            }
            BitSet s = (BitSet)this.Clone();
            s.OrInPlace( a );
            return s;
        }

        /** <summary>or this element into this set (grow as necessary to accommodate)</summary> */
        public void Add( int el )
        {
            int n = WordNumber( el );
            if ( n >= _bits.Length )
            {
                GrowToInclude( el );
            }
            _bits[n] |= BitMask( el );
        }

        /** <summary>Grows the set to a larger number of bits.</summary>
         *  <param name="bit">element that must fit in set</param>
         */
        public void GrowToInclude( int bit )
        {
            int newSize = Math.Max( _bits.Length << 1, NumWordsToHold( bit ) );
            SetSize(newSize);
        }

        public void OrInPlace( BitSet a )
        {
            if ( a == null )
            {
                return;
            }
            // If this is smaller than a, grow this first
            if ( a._bits.Length > _bits.Length )
            {
                SetSize( a._bits.Length );
            }
            int min = Math.Min( _bits.Length, a._bits.Length );
            for ( int i = min - 1; i >= 0; i-- )
            {
                _bits[i] |= a._bits[i];
            }
        }

        /** <summary>Sets the size of a set.</summary>
         *  <param name="nwords">how many words the new set should be</param>
         */
        private void SetSize( int nwords )
        {
            Array.Resize(ref _bits, nwords);
        }

        private static ulong BitMask( int bitNumber )
        {
            int bitPosition = bitNumber & MOD_MASK; // bitNumber mod BITS
            return 1UL << bitPosition;
        }

        public object Clone()
        {
            return new BitSet( (ulong[])_bits.Clone() );
        }

        public int Size()
        {
            int deg = 0;
            for ( int i = _bits.Length - 1; i >= 0; i-- )
            {
                ulong word = _bits[i];
                if ( word != 0L )
                {
                    for ( int bit = BITS - 1; bit >= 0; bit-- )
                    {
                        if ( ( word & ( 1UL << bit ) ) != 0 )
                        {
                            deg++;
                        }
                    }
                }
            }
            return deg;
        }

        public override int GetHashCode()
        {
            throw new System.NotImplementedException();
        }

        public override bool Equals( object other )
        {
            if ( other == null || !( other is BitSet ) )
            {
                return false;
            }

            BitSet otherSet = (BitSet)other;

            int n = Math.Min( this._bits.Length, otherSet._bits.Length );

            // for any bits in common, compare
            for ( int i = 0; i < n; i++ )
            {
                if ( this._bits[i] != otherSet._bits[i] )
                {
                    return false;
                }
            }

            // make sure any extra bits are off

            if ( this._bits.Length > n )
            {
                for ( int i = n + 1; i < this._bits.Length; i++ )
                {
                    if ( this._bits[i] != 0 )
                    {
                        return false;
                    }
                }
            }
            else if ( otherSet._bits.Length > n )
            {
                for ( int i = n + 1; i < otherSet._bits.Length; i++ )
                {
                    if ( otherSet._bits[i] != 0 )
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        public bool Member( int el )
        {
            if ( el < 0 )
            {
                return false;
            }
            int n = WordNumber( el );
            if ( n >= _bits.Length )
                return false;
            return ( _bits[n] & BitMask( el ) ) != 0;
        }

        // remove this element from this set
        public void Remove( int el )
        {
            int n = WordNumber( el );
            if ( n < _bits.Length )
            {
                _bits[n] &= ~BitMask( el );
            }
        }

        public bool IsNil()
        {
            for ( int i = _bits.Length - 1; i >= 0; i-- )
            {
                if ( _bits[i] != 0 )
                    return false;
            }
            return true;
        }

        private static int NumWordsToHold( int el )
        {
            return ( el >> LOG_BITS ) + 1;
        }

        public int NumBits()
        {
            return _bits.Length << LOG_BITS; // num words * bits per word
        }

        /** <summary>return how much space is being used by the bits array not how many actually have member bits on.</summary> */
        public int LengthInLongWords()
        {
            return _bits.Length;
        }

        /**Is this contained within a? */
        /*
        public boolean subset(BitSet a) {
            if (a == null || !(a instanceof BitSet)) return false;
            return this.and(a).equals(this);
        }
        */

        public int[] ToArray()
        {
            int[] elems = new int[Size()];
            int en = 0;
            for ( int i = 0; i < ( _bits.Length << LOG_BITS ); i++ )
            {
                if ( Member( i ) )
                {
                    elems[en++] = i;
                }
            }
            return elems;
        }

        private static int WordNumber( int bit )
        {
            return bit >> LOG_BITS; // bit / BITS
        }

        public override string ToString()
        {
            return ToString( null );
        }

        public string ToString( string[] tokenNames )
        {
            StringBuilder buf = new StringBuilder();
            string separator = ",";
            bool havePrintedAnElement = false;
            buf.Append( '{' );

            for ( int i = 0; i < ( _bits.Length << LOG_BITS ); i++ )
            {
                if ( Member( i ) )
                {
                    if ( i > 0 && havePrintedAnElement )
                    {
                        buf.Append( separator );
                    }
                    if ( tokenNames != null )
                    {
                        buf.Append( tokenNames[i] );
                    }
                    else
                    {
                        buf.Append( i );
                    }
                    havePrintedAnElement = true;
                }
            }
            buf.Append( '}' );
            return buf.ToString();
        }
    }
}