1 /* PriorityQueue.java -- Unbounded priority queue
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.io.Serializable;
44 * @author Tom Tromey (tromey@redhat.com)
45 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
48 public class PriorityQueue/*<E>*/ extends AbstractQueue/*<E>*/ implements Serializable
50 private static final int DEFAULT_CAPACITY = 11;
52 private static final long serialVersionUID = -7720805057305804111L;
54 /** Number of elements actually used in the storage array. */
58 * This is the storage for the underlying binomial heap.
59 * The idea is, each node is less than or equal to its children.
60 * A node at index N (0-based) has two direct children, at
61 * nodes 2N+1 and 2N+2.
63 Object/*E*/[] storage;
66 * The comparator we're using, or null for natural ordering.
68 Comparator/*<? super E>*/ comparator;
70 public PriorityQueue()
72 this(DEFAULT_CAPACITY, null);
75 public PriorityQueue(Collection/*<? extends E>*/ c)
77 this(Math.max(1, (int) (1.1 * c.size())), null);
79 // Special case where we can find the comparator to use.
80 /*if (c instanceof SortedSet)
82 SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
83 this.comparator = (Comparator<? super E>) ss.comparator();
84 // We can insert the elements directly, since they are sorted.
89 throw new NullPointerException();
93 else */if (c instanceof PriorityQueue)
95 PriorityQueue/*<? extends E>*/ pq = (PriorityQueue/*<? extends E>*/) c;
96 this.comparator = (Comparator/*<? super E>*/)pq.comparator();
97 // We can just copy the contents.
98 System.arraycopy(pq.storage, 0, storage, 0, pq.storage.length);
104 public PriorityQueue(int cap)
109 public PriorityQueue(int cap, Comparator/*<? super E>*/ comp)
112 this.storage = /*(E[])*/ new Object[cap];
113 this.comparator = comp;
116 public PriorityQueue(PriorityQueue/*<? extends E>*/ c)
118 this(Math.max(1, (int) (1.1 * c.size())),
119 (Comparator/*<? super E>*/)c.comparator());
120 // We can just copy the contents.
121 System.arraycopy(c.storage, 0, storage, 0, c.storage.length);
124 /*public PriorityQueue(SortedSet<? extends E> c)
126 this(Math.max(1, (int) (1.1 * c.size())),
127 (Comparator<? super E>)c.comparator());
128 // We can insert the elements directly, since they are sorted.
133 throw new NullPointerException();
140 //Arrays.fill(storage, null);
141 for(int i = 0; i<storage.length; i++) {
147 public Comparator/*<? super E>*/ comparator()
152 public Iterator/*<E>*/ iterator()
154 return (Iterator)(new PriorityQueueIterator/*<E>*/(this));
157 public boolean offer(Object/*E*/ o)
160 throw new NullPointerException();
162 int slot = findSlot(-1);
171 public Object/*E*/ peek()
173 return used == 0 ? null : storage[0];
176 public Object/*E*/ poll()
180 Object/*E*/ result = storage[0];
185 public boolean remove(Object o)
189 for (int i = 0; i < storage.length; ++i)
191 if (o.equals(storage[i]))
206 // It is more efficient to implement this locally -- less searching
208 public boolean addAll(Collection/*<? extends E>*/ c)
211 throw new IllegalArgumentException();
215 //for (Object/*E*/ val : c)
216 Iterator it = c.iterator();
219 Object val = it.next();
221 throw new NullPointerException();
222 newSlot = findSlot(newSlot);
223 storage[newSlot] = val;
231 int findSlot(int start)
234 if (used == storage.length)
241 for (slot = start + 1; slot < storage.length; ++slot)
243 if (storage[slot] == null)
246 // We'll always find a slot.
251 void remove(int index)
253 // Remove the element at INDEX. We do this by finding the least
254 // child and moving it into place, then iterating until we reach
255 // the bottom of the tree.
256 while (storage[index] != null)
258 int child = 2 * index + 1;
260 // See if we went off the end.
261 if (child >= storage.length)
263 storage[index] = null;
267 // Find which child we want to promote. If one is not null,
268 // we pick it. If both are null, it doesn't matter, we're
269 // about to leave. If neither is null, pick the lesser.
270 if (child + 1 >= storage.length || storage[child + 1] == null)
274 else if (storage[child] == null
275 || (Collections.compare(storage[child], storage[child + 1],
278 storage[index] = storage[child];
284 void bubbleUp(int index)
286 // The element at INDEX was inserted into a blank spot. Now move
287 // it up the tree to its natural resting place.
290 // This works regardless of whether we're at 2N+1 or 2N+2.
291 int parent = (index - 1) / 2;
292 if (Collections.compare(storage[parent], storage[index], comparator)
295 // Parent is the same or smaller than this element, so the
296 // invariant is preserved. Note that if the new element
297 // is smaller than the parent, then it is necessarily
298 // smaller than the parent's other child.
302 Object/*E*/ temp = storage[index];
303 storage[index] = storage[parent];
304 storage[parent] = temp;
312 Object/*E*/[] new_data = /*(E[])*/ new Object[2 * storage.length];
313 System.arraycopy(storage, 0, new_data, 0, storage.length);
318 public class PriorityQueueIterator implements Iterator {
323 public PriorityQueueIterator(PriorityQueue queue) {
327 public boolean hasNext()
332 public Object/*E*/ next()
334 while (this.queue.storage[++index] == null)
338 return this.queue.storage[index];
343 this.queue.remove(index);