1 /* 2 [The "BSD licence"] 3 Copyright (c) 2005-2008 Terence Parr 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 3. The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 package org.antlr.runtime.misc; 29 30 import java.util.List; 31 import java.util.ArrayList; 32 import java.util.NoSuchElementException; 33 34 /** A queue that can dequeue and get(i) in O(1) and grow arbitrarily large. 35 * A linked list is fast at dequeue but slow at get(i). An array is 36 * the reverse. This is O(1) for both operations. 37 * 38 * List grows until you dequeue last element at end of buffer. Then 39 * it resets to start filling at 0 again. If adds/removes are balanced, the 40 * buffer will not grow too large. 41 * 42 * No iterator stuff as that's not how we'll use it. 43 */ 44 public class FastQueue<T> { 45 /** dynamically-sized buffer of elements */ 46 protected List<T> data = new ArrayList<T>(); 47 /** index of next element to fill */ 48 protected int p = 0; 49 50 public void reset() { p = 0; data.clear(); } 51 52 /** Get and remove first element in queue */ 53 public T remove() { 54 T o = get(0); 55 p++; 56 // have we hit end of buffer? 57 if ( p == data.size() ) { 58 // if so, it's an opportunity to start filling at index 0 again 59 clear(); // size goes to 0, but retains memory 60 } 61 return o; 62 } 63 64 public void add(T o) { data.add(o); } 65 66 public int size() { return data.size() - p; } 67 68 public T head() { return get(0); } 69 70 /** Return element i elements ahead of current element. i==0 gets 71 * current element. This is not an absolute index into the data list 72 * since p defines the start of the real list. 73 */ 74 public T get(int i) { 75 if ( p+i >= data.size() ) { 76 throw new NoSuchElementException("queue index "+(p+i)+" > size "+data.size()); 77 } 78 return data.get(p+i); 79 } 80 81 public void clear() { p = 0; data.clear(); } 82 83 /** Return string of current buffer contents; non-destructive */ 84 public String toString() { 85 StringBuffer buf = new StringBuffer(); 86 int n = size(); 87 for (int i=0; i<n; i++) { 88 buf.append(get(i)); 89 if ( (i+1)<n ) buf.append(" "); 90 } 91 return buf.toString(); 92 } 93 }