1:
37:
38:
39: package ;
40:
41: import ;
42:
43:
48: public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
49: {
50: private static final int DEFAULT_CAPACITY = 11;
51:
52: private static final long serialVersionUID = -7720805057305804111L;
53:
54:
55: int used;
56:
57:
63: E[] storage;
64:
65:
68: Comparator<? super E> comparator;
69:
70: public PriorityQueue()
71: {
72: this(DEFAULT_CAPACITY, null);
73: }
74:
75: public PriorityQueue(Collection<? extends E> c)
76: {
77: this(Math.max(1, (int) (1.1 * c.size())), null);
78:
79:
80: if (c instanceof SortedSet)
81: {
82: SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
83: this.comparator = (Comparator<? super E>) ss.comparator();
84:
85: int i = 0;
86: for (E val : ss)
87: {
88: if (val == null)
89: throw new NullPointerException();
90: storage[i++] = val;
91: }
92: }
93: else if (c instanceof PriorityQueue)
94: {
95: PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;
96: this.comparator = (Comparator<? super E>)pq.comparator();
97:
98: System.arraycopy(pq.storage, 0, storage, 0, pq.storage.length);
99: }
100:
101: addAll(c);
102: }
103:
104: public PriorityQueue(int cap)
105: {
106: this(cap, null);
107: }
108:
109: public PriorityQueue(int cap, Comparator<? super E> comp)
110: {
111: this.used = 0;
112: this.storage = (E[]) new Object[cap];
113: this.comparator = comp;
114: }
115:
116: public PriorityQueue(PriorityQueue<? extends E> c)
117: {
118: this(Math.max(1, (int) (1.1 * c.size())),
119: (Comparator<? super E>)c.comparator());
120:
121: System.arraycopy(c.storage, 0, storage, 0, c.storage.length);
122: }
123:
124: public PriorityQueue(SortedSet<? extends E> c)
125: {
126: this(Math.max(1, (int) (1.1 * c.size())),
127: (Comparator<? super E>)c.comparator());
128:
129: int i = 0;
130: for (E val : c)
131: {
132: if (val == null)
133: throw new NullPointerException();
134: storage[i++] = val;
135: }
136: }
137:
138: public void clear()
139: {
140: Arrays.fill(storage, null);
141: used = 0;
142: }
143:
144: public Comparator<? super E> comparator()
145: {
146: return comparator;
147: }
148:
149: public Iterator<E> iterator()
150: {
151: return new Iterator<E>()
152: {
153: int index = -1;
154: int count = 0;
155:
156: public boolean hasNext()
157: {
158: return count < used;
159: }
160:
161: public E next()
162: {
163: while (storage[++index] == null)
164: ;
165:
166: ++count;
167: return storage[index];
168: }
169:
170: public void remove()
171: {
172: PriorityQueue.this.remove(index);
173: }
174: };
175: }
176:
177: public boolean offer(E o)
178: {
179: if (o == null)
180: throw new NullPointerException();
181:
182: int slot = findSlot(-1);
183:
184: storage[slot] = o;
185: ++used;
186: bubbleUp(slot);
187:
188: return true;
189: }
190:
191: public E peek()
192: {
193: return used == 0 ? null : storage[0];
194: }
195:
196: public E poll()
197: {
198: if (used == 0)
199: return null;
200: E result = storage[0];
201: remove(0);
202: return result;
203: }
204:
205: public boolean remove(Object o)
206: {
207: if (o != null)
208: {
209: for (int i = 0; i < storage.length; ++i)
210: {
211: if (o.equals(storage[i]))
212: {
213: remove(i);
214: return true;
215: }
216: }
217: }
218: return false;
219: }
220:
221: public int size()
222: {
223: return used;
224: }
225:
226:
227:
228: public boolean addAll(Collection<? extends E> c)
229: {
230: if (c == this)
231: throw new IllegalArgumentException();
232:
233: int newSlot = -1;
234: int save = used;
235: for (E val : c)
236: {
237: if (val == null)
238: throw new NullPointerException();
239: newSlot = findSlot(newSlot);
240: storage[newSlot] = val;
241: ++used;
242: bubbleUp(newSlot);
243: }
244:
245: return save != used;
246: }
247:
248: int findSlot(int start)
249: {
250: int slot;
251: if (used == storage.length)
252: {
253: resize();
254: slot = used;
255: }
256: else
257: {
258: for (slot = start + 1; slot < storage.length; ++slot)
259: {
260: if (storage[slot] == null)
261: break;
262: }
263:
264: }
265: return slot;
266: }
267:
268: void remove(int index)
269: {
270:
271:
272:
273: while (storage[index] != null)
274: {
275: int child = 2 * index + 1;
276:
277:
278: if (child >= storage.length)
279: {
280: storage[index] = null;
281: break;
282: }
283:
284:
285:
286:
287: if (child + 1 >= storage.length || storage[child + 1] == null)
288: {
289:
290: }
291: else if (storage[child] == null
292: || (Collections.compare(storage[child], storage[child + 1],
293: comparator) > 0))
294: ++child;
295: storage[index] = storage[child];
296: index = child;
297: }
298: --used;
299: }
300:
301: void bubbleUp(int index)
302: {
303:
304:
305: while (index > 0)
306: {
307:
308: int parent = (index - 1) / 2;
309: if (Collections.compare(storage[parent], storage[index], comparator)
310: <= 0)
311: {
312:
313:
314:
315:
316: break;
317: }
318:
319: E temp = storage[index];
320: storage[index] = storage[parent];
321: storage[parent] = temp;
322:
323: index = parent;
324: }
325: }
326:
327: void resize()
328: {
329: E[] new_data = (E[]) new Object[2 * storage.length];
330: System.arraycopy(storage, 0, new_data, 0, storage.length);
331: storage = new_data;
332: }
333: }