KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > StateSetEnumerator


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
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.1, $Revision: 2.3 $, $Date: 2004/11/06 23:03:32 $
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 int current;
37   private long [] bits;
38   
39   /**
40    * creates a new StateSetEnumerator that is not yet associated
41    * with a StateSet. hasMoreElements() and nextElement() will
42    * throw NullPointerException when used before reset()
43    */

44   public StateSetEnumerator() {
45   }
46
47   public StateSetEnumerator(StateSet states) {
48     reset(states);
49   }
50
51   public void reset(StateSet states) {
52     bits = states.bits;
53     index = 0;
54     offset = 0;
55     mask = 1;
56     current = 0;
57
58     while (index < bits.length && bits[index] == 0)
59       index++;
60
61     if (index >= bits.length) return;
62         
63     while (offset <= StateSet.MASK && ((bits[index] & mask) == 0)) {
64       mask<<= 1;
65       offset++;
66     }
67   }
68
69   private void advance() {
70     
71     if (DEBUG) Out.dump("Advancing, at start, index = "+index+", offset = "+offset); //$NON-NLS-1$ //$NON-NLS-2$
72

73     // cache fields in local variable for faster access
74
int _index = this.index;
75     int _offset = this.offset;
76     long _mask = this.mask;
77     long [] _bits = this.bits;
78
79     long bi = _bits[_index];
80
81     do {
82       _offset++;
83       _mask<<= 1;
84     } while (_offset <= StateSet.MASK && ((bi & _mask) == 0));
85
86     if (_offset > StateSet.MASK) {
87       int length = _bits.length;
88
89       do
90         _index++;
91       while (_index < length && _bits[_index] == 0);
92         
93       if (_index >= length) {
94         this.index = length; // indicates "no more elements"
95
return;
96       }
97
98       _offset = 0;
99       _mask = 1;
100       bi = _bits[_index];
101
102       // terminates, because bi != 0
103
while ((bi & _mask) == 0) {
104         _mask<<= 1;
105         _offset++;
106       }
107     }
108
109     // write back cached values
110
this.index = _index;
111     this.mask = _mask;
112     this.offset = _offset;
113   }
114
115   public boolean hasMoreElements() {
116     if (DEBUG) Out.dump("hasMoreElements, index = "+index+", offset = "+offset); //$NON-NLS-1$ //$NON-NLS-2$
117
return index < bits.length;
118   }
119
120   public int nextElement() {
121     if (DEBUG) Out.dump("nextElement, index = "+index+", offset = "+offset); //$NON-NLS-1$ //$NON-NLS-2$
122
int x = (index << StateSet.BITS) + offset;
123     advance();
124     return x;
125   }
126
127 }
128
Popular Tags