KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > TokenQueue


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/TokenQueue.java#7 $
8  */

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

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

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

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

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