• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2[The "BSD licence"]
3Copyright (c) 2005-2008 Terence Parr
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions
8are met:
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
112. Redistributions in binary form must reproduce the above copyright
12notice, this list of conditions and the following disclaimer in the
13documentation and/or other materials provided with the distribution.
143. The name of the author may not be used to endorse or promote products
15derived from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28package org.antlr.runtime.misc;
29
30import java.util.List;
31import java.util.ArrayList;
32import 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 */
44public 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}