1 package antlr; 2 3 9 10 11 public class CharQueue { 12 13 protected char[] buffer; 14 15 private int sizeLessOne; 16 17 private int offset; 18 19 protected int nbrEntries; 20 21 public CharQueue(int minSize) { 22 int size; 24 if ( minSize<0 ) { 25 init(16); return; 27 } 28 if ( minSize>=(Integer.MAX_VALUE/2) ) { 30 init(Integer.MAX_VALUE); return; 32 } 33 for (size = 2; size < minSize; size *= 2) { 34 } 35 init(size); 36 } 37 38 41 public final void append(char tok) { 42 if (nbrEntries == buffer.length) { 43 expand(); 44 } 45 buffer[(offset + nbrEntries) & sizeLessOne] = tok; 46 nbrEntries++; 47 } 48 49 52 public final char elementAt(int idx) { 53 return buffer[(offset + idx) & sizeLessOne]; 54 } 55 56 57 private final void expand() { 58 char[] newBuffer = new char[buffer.length * 2]; 59 for (int i = 0; i < buffer.length; i++) { 63 newBuffer[i] = elementAt(i); 64 } 65 buffer = newBuffer; 67 sizeLessOne = buffer.length - 1; 68 offset = 0; 69 } 70 71 74 public void init(int size) { 75 buffer = new char[size]; 77 sizeLessOne = size - 1; 79 offset = 0; 80 nbrEntries = 0; 81 } 82 83 85 public final void reset() { 86 offset = 0; 87 nbrEntries = 0; 88 } 89 90 91 public final void removeFirst() { 92 offset = (offset + 1) & sizeLessOne; 93 nbrEntries--; 94 } 95 } 96 | Popular Tags |