Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / Rook / Castle.Rook.Compiler / Parser / antlr / TokenQueue.cs
blobcd045fc8cecfafc394bcb71d76efd9ccc53741c9
1 using System;
3 namespace antlr
5 /*ANTLR Translator Generator
6 * Project led by Terence Parr at http://www.jGuru.com
7 * Software rights: http://www.antlr.org/license.html
9 * $Id:$
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 */
22 class TokenQueue
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 */
29 private int offset;
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
36 int size;
37 if (minSize < 0)
39 init(16); // pick some value for them
40 return ;
42 // check for overflow
43 if (minSize >= (int.MaxValue / 2))
45 init(int.MaxValue); // wow that's big.
46 return ;
48 for (size = 2; size < minSize; size *= 2)
52 init(size);
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)
62 expand();
64 buffer[(offset + nbrEntries) & sizeLessOne] = tok;
65 nbrEntries++;
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 */
77 private void expand()
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
88 buffer = newBuffer;
89 sizeLessOne = buffer.Length - 1;
90 offset = 0;
93 /*Initialize the queue.
94 * @param size The initial size of the queue
96 private void init(int size)
98 // Allocate buffer
99 buffer = new IToken[size];
100 // Other initialization
101 sizeLessOne = size - 1;
102 offset = 0;
103 nbrEntries = 0;
106 /*Clear the queue. Leaving the previous buffer alone.
108 public void reset()
110 offset = 0;
111 nbrEntries = 0;
114 /*Remove token from front of queue */
115 public void removeFirst()
117 offset = (offset + 1) & sizeLessOne;
118 nbrEntries--;