1 17 18 package com.finalist.jag.template.parser; 19 20 21 28 public class CharQueue { 29 30 31 protected char[] buffer; 32 33 35 36 protected int sizeLessOne; 37 38 40 41 protected int offset; 42 43 45 46 protected int nbrEntries; 47 48 49 56 public CharQueue(int minSize) { 57 int size; 59 for (size = 2; size < minSize; size *= 2) ; 60 init(size); 61 } 62 63 64 68 public final void append(char tok) { 69 if (nbrEntries == buffer.length) { 70 expand(); 71 } 72 buffer[(offset + nbrEntries) & sizeLessOne] = tok; 73 nbrEntries++; 74 } 75 76 77 83 public final char elementAt(int idx) { 84 return buffer[(offset + idx) & sizeLessOne]; 85 } 86 87 88 89 private final void expand() { 90 char[] newBuffer = new char[buffer.length * 2]; 91 92 for (int i = 0; i < buffer.length; i++) { 96 newBuffer[i] = elementAt(i); 97 } 98 99 buffer = newBuffer; 101 sizeLessOne = buffer.length - 1; 102 offset = 0; 103 } 104 105 106 110 private final void init(int size) { 111 buffer = new char[size]; 112 sizeLessOne = size - 1; 113 offset = 0; 114 nbrEntries = 0; 115 } 116 117 118 119 public final void removeFirst() { 120 offset = (offset + 1) & sizeLessOne; 121 nbrEntries--; 122 } 123 } | Popular Tags |