KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > CharQueue


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/CharQueue.java#8 $
8  */

9
10 /** A circular buffer object used by CharBuffer */
11 public class CharQueue {
12     /** Physical circular buffer of tokens */
13     protected char[] 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 CharQueue(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         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(char 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 char 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         char[] newBuffer = new char[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     public void init(int size) {
75         // Allocate buffer
76
buffer = new char[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 char from front of queue */
91     public final void removeFirst() {
92         offset = (offset + 1) & sizeLessOne;
93         nbrEntries--;
94     }
95 }
96
Popular Tags