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 private circular buffer object used by the token buffer */
24 /*Physical circular buffer of tokens */
25 private IToken
[] buffer
;
26 /*buffer.length-1 for quick modulos */
27 private int sizeLessOne
;
28 /*physical index of front token */
30 /*number of tokens in the queue */
31 protected internal int nbrEntries
;
33 public TokenQueue(int minSize
)
35 // Find first power of 2 >= to requested size
39 init(16); // pick some value for them
43 if (minSize
>= (int.MaxValue
/ 2))
45 init(int.MaxValue
); // wow that's big.
48 for (size
= 2; size
< minSize
; size
*= 2)
55 /*Add token to end of the queue
56 * @param tok The token to add
58 public void append(IToken tok
)
60 if (nbrEntries
== buffer
.Length
)
64 buffer
[(offset
+ nbrEntries
) & sizeLessOne
] = tok
;
68 /*Fetch a token from the queue by index
69 * @param idx The index of the token to fetch, where zero is the token at the front of the queue
71 public IToken
elementAt(int idx
)
73 return buffer
[(offset
+ idx
) & sizeLessOne
];
76 /*Expand the token buffer by doubling its capacity */
79 IToken
[] newBuffer
= new IToken
[buffer
.Length
* 2];
80 // Copy the contents to the new buffer
81 // Note that this will store the first logical item in the
82 // first physical array element.
83 for (int i
= 0; i
< buffer
.Length
; i
++)
85 newBuffer
[i
] = elementAt(i
);
87 // Re-initialize with new contents, keep old nbrEntries
89 sizeLessOne
= buffer
.Length
- 1;
93 /*Initialize the queue.
94 * @param size The initial size of the queue
96 private void init(int size
)
99 buffer
= new IToken
[size
];
100 // Other initialization
101 sizeLessOne
= size
- 1;
106 /*Clear the queue. Leaving the previous buffer alone.
114 /*Remove token from front of queue */
115 public void removeFirst()
117 offset
= (offset
+ 1) & sizeLessOne
;