5 /*ANTLR Translator Generator
6 * Project led by Terence Parr at http://www.jGuru.com
7 * Software rights: http://www.antlr.org/license.html
13 // ANTLR C# Code Generator by Micheal Jordan
14 // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
15 // Anthony Oguntimehin
17 // With many thanks to Eric V. Smith from the ANTLR list.
20 /*A circular buffer object used by CharBuffer */
21 public class CharQueue
23 /*Physical circular buffer of tokens */
24 protected internal char[] buffer
;
25 /*buffer.length-1 for quick modulos */
26 private int sizeLessOne
;
27 /*physical index of front token */
29 /*number of tokens in the queue */
30 protected internal int nbrEntries
;
32 public CharQueue(int minSize
)
34 // Find first power of 2 >= to requested size
38 init(16); // pick some value for them
42 if (minSize
>= (Int32
.MaxValue
/ 2))
44 init(Int32
.MaxValue
); // wow that's big.
47 for (size
= 2; size
< minSize
; size
*= 2)
54 /*Add token to end of the queue
55 * @param tok The token to add
57 public void append(char tok
)
59 if (nbrEntries
== buffer
.Length
)
63 buffer
[(offset
+ nbrEntries
) & sizeLessOne
] = tok
;
67 /*Fetch a token from the queue by index
68 * @param idx The index of the token to fetch, where zero is the token at the front of the queue
70 public char elementAt(int idx
)
72 return buffer
[(offset
+ idx
) & sizeLessOne
];
75 /*Expand the token buffer by doubling its capacity */
78 char[] newBuffer
= new char[buffer
.Length
* 2];
79 // Copy the contents to the new buffer
80 // Note that this will store the first logical item in the
81 // first physical array element.
82 for (int i
= 0; i
< buffer
.Length
; i
++)
84 newBuffer
[i
] = elementAt(i
);
86 // Re-initialize with new contents, keep old nbrEntries
88 sizeLessOne
= buffer
.Length
- 1;
92 /*Initialize the queue.
93 * @param size The initial size of the queue
95 public virtual void init(int size
)
98 buffer
= new char[size
];
99 // Other initialization
100 sizeLessOne
= size
- 1;
105 /*Clear the queue. Leaving the previous buffer alone.
113 /*Remove char from front of queue */
114 public void removeFirst()
116 offset
= (offset
+ 1) & sizeLessOne
;