aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/ActionScript/project/src/org/antlr/runtime/BitSet.as
blob: 0dce7886a82462d6fd4297fa5daaccf729e072c2 (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
/*
 [The "BSD licence"]
 Copyright (c) 2005-2006 Terence Parr
 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.
*/
package org.antlr.runtime {

	/**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.
	 */
	public class BitSet {
	    protected static const BITS:uint = 32;    // number of bits / int
	    protected static const LOG_BITS:uint = 5; // 2^5 == 32
	
	    /* 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.
	     */
	    protected static const MOD_MASK:uint = BITS - 1;

	    /** The actual data bits */
	    protected var bits:Array;
	
	    /** Construction from a static array of longs */
	    public function BitSet(bits:Array = null) {
	        if (bits == null) {
	            this.bits = new Array();
	        }
	        else {
    	        this.bits = new Array();
    			for (var i:int = 0; i < bits.length; i++) {
    				this.bits[i] = bits[i];
    	    	}
	        }
	    }
	
		public static function of(... args):BitSet {
			var s:BitSet = new BitSet();
			for (var i:int = 0; i < args.length; i++) {
				s.add(args[i]);
			}
			return s;
		}
	
		/** return this | a in a new set */
		public function or(a:BitSet):BitSet {
			if ( a==null ) {
				return this;
			}
			var s:BitSet = this.clone();
			s.orInPlace(a);
			return s;
		}
	
		/** or this element into this set (grow as necessary to accommodate) */
		public function add(el:int):void {
			var n:int = wordNumber(el);
			if (n >= bits.length) {
				growToInclude(el);
			}
			bits[n] |= bitMask(el);
		}
	
		/**
		 * Grows the set to a larger number of bits.
		 * @param bit element that must fit in set
		 */
		public function growToInclude(bit:int):void {
			var newSize:int = Math.max(bits.length << 1, numWordsToHold(bit));
			bits.length = newSize;
		}
	
		public function orInPlace(a:BitSet):void {
			if ( a==null ) {
				return;
			}
			// If this is smaller than a, grow this first
			if (a.bits.length > bits.length) {
				this.bits.length = a.bits.length;
			}
			var min:int = Math.min(bits.length, a.bits.length);
			for (var i:int = min - 1; i >= 0; i--) {
				bits[i] |= a.bits[i];
			}
		}
	
		/**
		 * Sets the size of a set.
		 * @param nwords how many words the new set should be
		 */
		private function set size(nwords:int):void {
			bits.length = nwords;
		}
	
	    private static function bitMask(bitNumber:int):int {
	        var bitPosition:int = bitNumber & MOD_MASK; // bitNumber mod BITS
	        return 1 << bitPosition;
	    }
	
	    public function clone():BitSet {
	        var s:BitSet = new BitSet(bits);
			return s;
	    }
	
	    public function get size():int {
	        var deg:uint = 0;
	        for (var i:int = bits.length - 1; i >= 0; i--) {
	            var word:uint = bits[i];
	            if (word != 0) {
	                for (var bit:int = BITS - 1; bit >= 0; bit--) {
	                    if ((word & (1 << bit)) != 0) {
	                        deg++;
	                    }
	                }
	            }
	        }
	        return deg;
	    }
	
	    public function equals(other:Object):Boolean {
	        if ( other == null || !(other is BitSet) ) {
	            return false;
	        }
	
	        var otherSet:BitSet = BitSet(other);
	
	        var n:int = Math.min(this.bits.length, otherSet.bits.length);
	
	        // for any bits in common, compare
	        for (var i:int=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 (i = n+1; i<this.bits.length; i++) {
	                if (this.bits[i] != 0) {
	                    return false;
	                }
	            }
	        }
	        else if (otherSet.bits.length > n) {
	            for (i = n+1; i<otherSet.bits.length; i++) {
	                if (otherSet.bits[i] != 0) {
	                    return false;
	                }
	            }
	        }
	
	        return true;
	    }
	
	    public function member(el:int):Boolean {
			if ( el<0 ) {
				return false;
			}
	        var n:int = wordNumber(el);
	        if (n >= bits.length) return false;
	        return (bits[n] & bitMask(el)) != 0;
	    }
	
		// remove this element from this set
		public function remove(el:int):void {
			var n:int = wordNumber(el);
			if (n < bits.length) {
				bits[n] &= ~bitMask(el);
			}
		}
	
	    public function get isNil():Boolean {
	        for (var i:int = bits.length - 1; i >= 0; i--) {
	            if (bits[i] != 0) return false;
	        }
	        return true;
	    }
	
	    private final function numWordsToHold(el:int):int {
	        return (el >> LOG_BITS) + 1;
	    }
	
	    public function get numBits():int {
	        return bits.length << LOG_BITS; // num words * bits per word
	    }
	
	    /** return how much space is being used by the bits array not
	     *  how many actually have member bits on.
	     */
	    public function get lengthInLongWords():int {
	        return bits.length;
	    }
	
	    public function toArray():Array {
	        var elems:Array = new Array[this.bits.length];
	        var en:int = 0;
	        for (var i:int = 0; i < (bits.length << LOG_BITS); i++) {
	            if (member(i)) {
	                elems[en++] = i;
	            }
	        }
	        return elems;
	    }
	
	    public function toPackedArray():Array {
	        return bits;
	    }
	
		private static function wordNumber(bit:uint):uint {
			return bit >> LOG_BITS; // bit / BITS
		}
	
		public function toString():String {
			return toStringFromTokens(null);
		}
	
		public function toStringFromTokens(tokenNames:Array):String {
			var buf:String = "";
			const separator:String = ",";
			var havePrintedAnElement:Boolean = false;
			buf = buf + '{';
	
			for (var i:int = 0; i < (bits.length << LOG_BITS); i++) {
				if (member(i)) {
					if (i > 0 && havePrintedAnElement ) {
						buf += separator;
					}
					if ( tokenNames!=null ) {
						buf += tokenNames[i];
					}
					else {
						buf += i;
					}
					havePrintedAnElement = true;
				}
			}
			buf += '}';
			return buf;
		}
	
	}
}