Home | History | Annotate | Download | only in misc
      1 /*
      2  [The "BSD license"]
      3  Copyright (c) 2005-2009 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 	protected int range = -1; // how deep have we gone?
     50 
     51     public void reset() { clear(); }
     52     public void clear() { p = 0; data.clear(); }
     53 
     54     /** Get and remove first element in queue */
     55     public T remove() {
     56         T o = elementAt(0);
     57         p++;
     58         // have we hit end of buffer?
     59         if ( p == data.size() ) {
     60             // if so, it's an opportunity to start filling at index 0 again
     61             clear(); // size goes to 0, but retains memory
     62         }
     63         return o;
     64     }
     65 
     66     public void add(T o) { data.add(o); }
     67 
     68     public int size() { return data.size() - p; }
     69 
     70 	public int range() { return range; }
     71 
     72     public T head() { return elementAt(0); }
     73 
     74     /** Return element i elements ahead of current element.  i==0 gets
     75      *  current element.  This is not an absolute index into the data list
     76      *  since p defines the start of the real list.
     77      */
     78     public T elementAt(int i) {
     79 		int absIndex = p + i;
     80 		if ( absIndex >= data.size() ) {
     81             throw new NoSuchElementException("queue index "+ absIndex +" > last index "+(data.size()-1));
     82         }
     83         if ( absIndex < 0 ) {
     84             throw new NoSuchElementException("queue index "+ absIndex +" < 0");
     85         }
     86 		if ( absIndex>range ) range = absIndex;
     87         return data.get(absIndex);
     88     }
     89 
     90     /** Return string of current buffer contents; non-destructive */
     91     public String toString() {
     92         StringBuffer buf = new StringBuffer();
     93         int n = size();
     94         for (int i=0; i<n; i++) {
     95             buf.append(elementAt(i));
     96             if ( (i+1)<n ) buf.append(" ");
     97         }
     98         return buf.toString();
     99     }
    100 }