1 package persistence.antlr; 2 3 8 9 10 class TokenQueue { 11 12 private Token[] buffer; 13 14 private int sizeLessOne; 15 16 private int offset; 17 18 protected int nbrEntries; 19 20 public TokenQueue(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 } 35 init(size); 36 } 37 38 41 public final void append(Token tok) { 42 if (nbrEntries == buffer.length) { 43 expand(); 44 } 45 buffer[(offset + nbrEntries) & sizeLessOne] = tok; 46 nbrEntries++; 47 } 48 49 52 public final Token elementAt(int idx) { 53 return buffer[(offset + idx) & sizeLessOne]; 54 } 55 56 57 private final void expand() { 58 Token[] newBuffer = new Token[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 private final void init(int size) { 75 buffer = new Token[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 |