KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > TokenQueue


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 /** A private circular buffer object used by the token buffer */
10 class TokenQueue {
11     /** Physical circular buffer of tokens */
12     private Token[] buffer;
13     /** buffer.length-1 for quick modulos */
14     private int sizeLessOne;
15     /** physical index of front token */
16     private int offset;
17     /** number of tokens in the queue */
18     protected int nbrEntries;
19
20     public TokenQueue(int minSize) {
21         // Find first power of 2 >= to requested size
22
int size;
23         if ( minSize<0 ) {
24             init(16); // pick some value for them
25
return;
26         }
27         // check for overflow
28
if ( minSize>=(Integer.MAX_VALUE/2) ) {
29             init(Integer.MAX_VALUE); // wow that's big.
30
return;
31         }
32         for (size = 2; size < minSize; size *= 2) {
33             ;
34         }
35         init(size);
36     }
37
38     /** Add token to end of the queue
39      * @param tok The token to add
40      */

41     public final void append(Token tok) {
42         if (nbrEntries == buffer.length) {
43             expand();
44         }
45         buffer[(offset + nbrEntries) & sizeLessOne] = tok;
46         nbrEntries++;
47     }
48
49     /** Fetch a token from the queue by index
50      * @param idx The index of the token to fetch, where zero is the token at the front of the queue
51      */

52     public final Token elementAt(int idx) {
53         return buffer[(offset + idx) & sizeLessOne];
54     }
55
56     /** Expand the token buffer by doubling its capacity */
57     private final void expand() {
58         Token[] newBuffer = new Token[buffer.length * 2];
59         // Copy the contents to the new buffer
60
// Note that this will store the first logical item in the
61
// first physical array element.
62
for (int i = 0; i < buffer.length; i++) {
63             newBuffer[i] = elementAt(i);
64         }
65         // Re-initialize with new contents, keep old nbrEntries
66
buffer = newBuffer;
67         sizeLessOne = buffer.length - 1;
68         offset = 0;
69     }
70
71     /** Initialize the queue.
72      * @param size The initial size of the queue
73      */

74     private final void init(int size) {
75         // Allocate buffer
76
buffer = new Token[size];
77         // Other initialization
78
sizeLessOne = size - 1;
79         offset = 0;
80         nbrEntries = 0;
81     }
82
83     /** Clear the queue. Leaving the previous buffer alone.
84      */

85     public final void reset() {
86         offset = 0;
87         nbrEntries = 0;
88     }
89
90     /** Remove token from front of queue */
91     public final void removeFirst() {
92         offset = (offset + 1) & sizeLessOne;
93         nbrEntries--;
94     }
95 }
96
Popular Tags