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