PriorityQueue.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* PriorityQueue.java -- Unbounded priority queue
  2. Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. import java.io.Serializable;
  33. /**
  34. * @author Tom Tromey (tromey@redhat.com)
  35. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  36. * @since 1.5
  37. */
  38. public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
  39. {
  40. private static final int DEFAULT_CAPACITY = 11;
  41. private static final long serialVersionUID = -7720805057305804111L;
  42. /** Number of elements actually used in the storage array. */
  43. int used;
  44. /**
  45. * This is the storage for the underlying binomial heap.
  46. * The idea is, each node is less than or equal to its children.
  47. * A node at index N (0-based) has two direct children, at
  48. * nodes 2N+1 and 2N+2.
  49. */
  50. E[] storage;
  51. /**
  52. * The comparator we're using, or null for natural ordering.
  53. */
  54. Comparator<? super E> comparator;
  55. public PriorityQueue()
  56. {
  57. this(DEFAULT_CAPACITY, null);
  58. }
  59. public PriorityQueue(Collection<? extends E> c)
  60. {
  61. this(Math.max(1, (int) (1.1 * c.size())), null);
  62. // Special case where we can find the comparator to use.
  63. if (c instanceof SortedSet)
  64. {
  65. SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
  66. this.comparator = (Comparator<? super E>) ss.comparator();
  67. // We can insert the elements directly, since they are sorted.
  68. int i = 0;
  69. for (E val : ss)
  70. {
  71. if (val == null)
  72. throw new NullPointerException();
  73. storage[i++] = val;
  74. }
  75. }
  76. else if (c instanceof PriorityQueue)
  77. {
  78. PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;
  79. this.comparator = (Comparator<? super E>)pq.comparator();
  80. // We can just copy the contents.
  81. System.arraycopy(pq.storage, 0, storage, 0, pq.storage.length);
  82. }
  83. addAll(c);
  84. }
  85. public PriorityQueue(int cap)
  86. {
  87. this(cap, null);
  88. }
  89. public PriorityQueue(int cap, Comparator<? super E> comp)
  90. {
  91. if (cap < 1)
  92. throw new IllegalArgumentException();
  93. this.used = 0;
  94. this.storage = (E[]) new Object[cap];
  95. this.comparator = comp;
  96. }
  97. public PriorityQueue(PriorityQueue<? extends E> c)
  98. {
  99. this(Math.max(1, (int) (1.1 * c.size())),
  100. (Comparator<? super E>)c.comparator());
  101. // We can just copy the contents.
  102. System.arraycopy(c.storage, 0, storage, 0, c.storage.length);
  103. }
  104. public PriorityQueue(SortedSet<? extends E> c)
  105. {
  106. this(Math.max(1, (int) (1.1 * c.size())),
  107. (Comparator<? super E>)c.comparator());
  108. // We can insert the elements directly, since they are sorted.
  109. int i = 0;
  110. for (E val : c)
  111. {
  112. if (val == null)
  113. throw new NullPointerException();
  114. storage[i++] = val;
  115. }
  116. }
  117. public void clear()
  118. {
  119. Arrays.fill(storage, null);
  120. used = 0;
  121. }
  122. public Comparator<? super E> comparator()
  123. {
  124. return comparator;
  125. }
  126. public Iterator<E> iterator()
  127. {
  128. return new Iterator<E>()
  129. {
  130. int index = -1;
  131. int count = 0;
  132. public boolean hasNext()
  133. {
  134. return count < used;
  135. }
  136. public E next()
  137. {
  138. while (storage[++index] == null)
  139. ;
  140. ++count;
  141. return storage[index];
  142. }
  143. public void remove()
  144. {
  145. PriorityQueue.this.remove(index);
  146. index--;
  147. }
  148. };
  149. }
  150. public boolean offer(E o)
  151. {
  152. if (o == null)
  153. throw new NullPointerException();
  154. int slot = findSlot(-1);
  155. storage[slot] = o;
  156. ++used;
  157. bubbleUp(slot);
  158. return true;
  159. }
  160. public E peek()
  161. {
  162. return used == 0 ? null : storage[0];
  163. }
  164. public E poll()
  165. {
  166. if (used == 0)
  167. return null;
  168. E result = storage[0];
  169. remove(0);
  170. return result;
  171. }
  172. public boolean remove(Object o)
  173. {
  174. if (o != null)
  175. {
  176. for (int i = 0; i < storage.length; ++i)
  177. {
  178. if (o.equals(storage[i]))
  179. {
  180. remove(i);
  181. return true;
  182. }
  183. }
  184. }
  185. return false;
  186. }
  187. public int size()
  188. {
  189. return used;
  190. }
  191. // It is more efficient to implement this locally -- less searching
  192. // for free slots.
  193. public boolean addAll(Collection<? extends E> c)
  194. {
  195. if (c == this)
  196. throw new IllegalArgumentException();
  197. int newSlot = -1;
  198. int save = used;
  199. for (E val : c)
  200. {
  201. if (val == null)
  202. throw new NullPointerException();
  203. newSlot = findSlot(newSlot);
  204. storage[newSlot] = val;
  205. ++used;
  206. bubbleUp(newSlot);
  207. }
  208. return save != used;
  209. }
  210. int findSlot(int start)
  211. {
  212. int slot;
  213. if (used == storage.length)
  214. {
  215. resize();
  216. slot = used;
  217. }
  218. else
  219. {
  220. for (slot = start + 1; slot < storage.length; ++slot)
  221. {
  222. if (storage[slot] == null)
  223. break;
  224. }
  225. // We'll always find a slot.
  226. }
  227. return slot;
  228. }
  229. void remove(int index)
  230. {
  231. // Remove the element at INDEX. We do this by finding the least
  232. // child and moving it into place, then iterating until we reach
  233. // the bottom of the tree.
  234. while (storage[index] != null)
  235. {
  236. int child = 2 * index + 1;
  237. // See if we went off the end.
  238. if (child >= storage.length)
  239. {
  240. storage[index] = null;
  241. break;
  242. }
  243. // Find which child we want to promote. If one is not null,
  244. // we pick it. If both are null, it doesn't matter, we're
  245. // about to leave. If neither is null, pick the lesser.
  246. if (child + 1 >= storage.length || storage[child + 1] == null)
  247. {
  248. // Nothing.
  249. }
  250. else if (storage[child] == null
  251. || (Collections.compare(storage[child], storage[child + 1],
  252. comparator) > 0))
  253. ++child;
  254. storage[index] = storage[child];
  255. index = child;
  256. }
  257. --used;
  258. }
  259. void bubbleUp(int index)
  260. {
  261. // The element at INDEX was inserted into a blank spot. Now move
  262. // it up the tree to its natural resting place.
  263. while (index > 0)
  264. {
  265. // This works regardless of whether we're at 2N+1 or 2N+2.
  266. int parent = (index - 1) / 2;
  267. if (Collections.compare(storage[parent], storage[index], comparator)
  268. <= 0)
  269. {
  270. // Parent is the same or smaller than this element, so the
  271. // invariant is preserved. Note that if the new element
  272. // is smaller than the parent, then it is necessarily
  273. // smaller than the parent's other child.
  274. break;
  275. }
  276. E temp = storage[index];
  277. storage[index] = storage[parent];
  278. storage[parent] = temp;
  279. index = parent;
  280. }
  281. }
  282. void resize()
  283. {
  284. E[] new_data = (E[]) new Object[2 * storage.length];
  285. System.arraycopy(storage, 0, new_data, 0, storage.length);
  286. storage = new_data;
  287. }
  288. }