Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / Rook / Castle.Rook.Compiler / Parser / antlr / CharQueue.cs
blob60ee0abb10b19aaa5ae61acfa3d1f9b00635680d
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 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 */
28 private int offset;
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
35 int size;
36 if (minSize < 0)
38 init(16); // pick some value for them
39 return ;
41 // check for overflow
42 if (minSize >= (Int32.MaxValue / 2))
44 init(Int32.MaxValue); // wow that's big.
45 return ;
47 for (size = 2; size < minSize; size *= 2)
51 init(size);
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)
61 expand();
63 buffer[(offset + nbrEntries) & sizeLessOne] = tok;
64 nbrEntries++;
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 */
76 private void expand()
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
87 buffer = newBuffer;
88 sizeLessOne = buffer.Length - 1;
89 offset = 0;
92 /*Initialize the queue.
93 * @param size The initial size of the queue
95 public virtual void init(int size)
97 // Allocate buffer
98 buffer = new char[size];
99 // Other initialization
100 sizeLessOne = size - 1;
101 offset = 0;
102 nbrEntries = 0;
105 /*Clear the queue. Leaving the previous buffer alone.
107 public void reset()
109 offset = 0;
110 nbrEntries = 0;
113 /*Remove char from front of queue */
114 public void removeFirst()
116 offset = (offset + 1) & sizeLessOne;
117 nbrEntries--;