source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr/CharQueue.cs @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 2.7 KB
Line 
1using System;
2
3namespace antlr
4{
5        /*ANTLR Translator Generator
6        * Project led by Terence Parr at http://www.jGuru.com
7        * Software rights: http://www.antlr.org/license.html
8        *
9        * $Id:$
10        */
11
12        //
13        // ANTLR C# Code Generator by Micheal Jordan
14        //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
15        //                            Anthony Oguntimehin
16        //
17        // With many thanks to Eric V. Smith from the ANTLR list.
18        //
19       
20        /*A circular buffer object used by CharBuffer */
21        public class CharQueue
22        {
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;
31               
32                public CharQueue(int minSize)
33                {
34                        // Find first power of 2 >= to requested size
35                        int size;
36                        if (minSize < 0)
37                        {
38                                init(16); // pick some value for them
39                                return ;
40                        }
41                        // check for overflow
42                        if (minSize >= (Int32.MaxValue / 2))
43                        {
44                                init(Int32.MaxValue); // wow that's big.
45                                return ;
46                        }
47                        for (size = 2; size < minSize; size *= 2) 
48                        { 
49                                ;
50                        }
51                        init(size);
52                }
53               
54                /*Add token to end of the queue
55                * @param tok The token to add
56                */
57                public void append(char tok)
58                {
59                        if (nbrEntries == buffer.Length)
60                        {
61                                expand();
62                        }
63                        buffer[(offset + nbrEntries) & sizeLessOne] = tok;
64                        nbrEntries++;
65                }
66               
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
69                */
70                public char elementAt(int idx)
71                {
72                        return buffer[(offset + idx) & sizeLessOne];
73                }
74               
75                /*Expand the token buffer by doubling its capacity */
76                private void  expand()
77                {
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++)
83                        {
84                                newBuffer[i] = elementAt(i);
85                        }
86                        // Re-initialize with new contents, keep old nbrEntries
87                        buffer = newBuffer;
88                        sizeLessOne = buffer.Length - 1;
89                        offset = 0;
90                }
91               
92                /*Initialize the queue.
93                * @param size The initial size of the queue
94                */
95                public virtual void  init(int size)
96                {
97                        // Allocate buffer
98                        buffer = new char[size];
99                        // Other initialization
100                        sizeLessOne = size - 1;
101                        offset = 0;
102                        nbrEntries = 0;
103                }
104               
105                /*Clear the queue. Leaving the previous buffer alone.
106                */
107                public void  reset()
108                {
109                        offset = 0;
110                        nbrEntries = 0;
111                }
112               
113                /*Remove char from front of queue */
114                public void  removeFirst()
115                {
116                        offset = (offset + 1) & sizeLessOne;
117                        nbrEntries--;
118                }
119        }
120}
Note: See TracBrowser for help on using the repository browser.