1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
2 | * JFlex 1.4.3 *
|
---|
3 | * Copyright (C) 1998-2009 Gerwin Klein <[email protected]> *
|
---|
4 | * All rights reserved. *
|
---|
5 | * *
|
---|
6 | * This program is free software; you can redistribute it and/or modify *
|
---|
7 | * it under the terms of the GNU General Public License. See the file *
|
---|
8 | * COPYRIGHT for more information. *
|
---|
9 | * *
|
---|
10 | * This program is distributed in the hope that it will be useful, *
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
---|
13 | * GNU General Public License for more details. *
|
---|
14 | * *
|
---|
15 | * You should have received a copy of the GNU General Public License along *
|
---|
16 | * with this program; if not, write to the Free Software Foundation, Inc., *
|
---|
17 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
---|
18 | * *
|
---|
19 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
---|
20 | package JFlex;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Enumerates the states of a StateSet.
|
---|
24 | *
|
---|
25 | * @author Gerwin Klein
|
---|
26 | * @version JFlex 1.4.3, $Revision: 433 $, $Date: 2009-01-31 19:52:34 +1100 (Sat, 31 Jan 2009) $
|
---|
27 | */
|
---|
28 | final public class StateSetEnumerator {
|
---|
29 |
|
---|
30 | private final static boolean DEBUG = false;
|
---|
31 |
|
---|
32 | private int index;
|
---|
33 | private int offset;
|
---|
34 | private long mask;
|
---|
35 |
|
---|
36 | private long [] bits;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * creates a new StateSetEnumerator that is not yet associated
|
---|
40 | * with a StateSet. hasMoreElements() and nextElement() will
|
---|
41 | * throw NullPointerException when used before reset()
|
---|
42 | */
|
---|
43 | public StateSetEnumerator() {
|
---|
44 | }
|
---|
45 |
|
---|
46 | public StateSetEnumerator(StateSet states) {
|
---|
47 | reset(states);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void reset(StateSet states) {
|
---|
51 | bits = states.bits;
|
---|
52 | index = 0;
|
---|
53 | offset = 0;
|
---|
54 | mask = 1;
|
---|
55 | while (index < bits.length && bits[index] == 0)
|
---|
56 | index++;
|
---|
57 |
|
---|
58 | if (index >= bits.length) return;
|
---|
59 |
|
---|
60 | while (offset <= StateSet.MASK && ((bits[index] & mask) == 0)) {
|
---|
61 | mask<<= 1;
|
---|
62 | offset++;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | private void advance() {
|
---|
67 |
|
---|
68 | if (DEBUG) Out.dump("Advancing, at start, index = "+index+", offset = "+offset); //$NON-NLS-1$ //$NON-NLS-2$
|
---|
69 |
|
---|
70 | // cache fields in local variable for faster access
|
---|
71 | int _index = this.index;
|
---|
72 | int _offset = this.offset;
|
---|
73 | long _mask = this.mask;
|
---|
74 | long [] _bits = this.bits;
|
---|
75 |
|
---|
76 | long bi = _bits[_index];
|
---|
77 |
|
---|
78 | do {
|
---|
79 | _offset++;
|
---|
80 | _mask<<= 1;
|
---|
81 | } while (_offset <= StateSet.MASK && ((bi & _mask) == 0));
|
---|
82 |
|
---|
83 | if (_offset > StateSet.MASK) {
|
---|
84 | int length = _bits.length;
|
---|
85 |
|
---|
86 | do
|
---|
87 | _index++;
|
---|
88 | while (_index < length && _bits[_index] == 0);
|
---|
89 |
|
---|
90 | if (_index >= length) {
|
---|
91 | this.index = length; // indicates "no more elements"
|
---|
92 | return;
|
---|
93 | }
|
---|
94 |
|
---|
95 | _offset = 0;
|
---|
96 | _mask = 1;
|
---|
97 | bi = _bits[_index];
|
---|
98 |
|
---|
99 | // terminates, because bi != 0
|
---|
100 | while ((bi & _mask) == 0) {
|
---|
101 | _mask<<= 1;
|
---|
102 | _offset++;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | // write back cached values
|
---|
107 | this.index = _index;
|
---|
108 | this.mask = _mask;
|
---|
109 | this.offset = _offset;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public boolean hasMoreElements() {
|
---|
113 | if (DEBUG) Out.dump("hasMoreElements, index = "+index+", offset = "+offset); //$NON-NLS-1$ //$NON-NLS-2$
|
---|
114 | return index < bits.length;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public int nextElement() {
|
---|
118 | if (DEBUG) Out.dump("nextElement, index = "+index+", offset = "+offset); //$NON-NLS-1$ //$NON-NLS-2$
|
---|
119 | int x = (index << StateSet.BITS) + offset;
|
---|
120 | advance();
|
---|
121 | return x;
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|