1 package antlr; 2 3 9 10 11 class TokenQueue { 12 13 private Token[] buffer; 14 15 private int sizeLessOne; 16 17 private int offset; 18 19 protected int nbrEntries; 20 21 public TokenQueue(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 } 36 init(size); 37 } 38 39 42 public final void append(Token tok) { 43 if (nbrEntries == buffer.length) { 44 expand(); 45 } 46 buffer[(offset + nbrEntries) & sizeLessOne] = tok; 47 nbrEntries++; 48 } 49 50 53 public final Token elementAt(int idx) { 54 return buffer[(offset + idx) & sizeLessOne]; 55 } 56 57 58 private final void expand() { 59 Token[] newBuffer = new Token[buffer.length * 2]; 60 for (int i = 0; i < buffer.length; i++) { 64 newBuffer[i] = elementAt(i); 65 } 66 buffer = newBuffer; 68 sizeLessOne = buffer.length - 1; 69 offset = 0; 70 } 71 72 75 private final void init(int size) { 76 buffer = new Token[size]; 78 sizeLessOne = size - 1; 80 offset = 0; 81 nbrEntries = 0; 82 } 83 84 86 public final void reset() { 87 offset = 0; 88 nbrEntries = 0; 89 } 90 91 92 public final void removeFirst() { 93 offset = (offset + 1) & sizeLessOne; 94 nbrEntries--; 95 } 96 } 97 | Popular Tags |