-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorityQueue.java
356 lines (290 loc) · 10.1 KB
/
priorityQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import java.util.*;
public class PriorityQueue<E>extends AbstractQueue<E>
implements java.io.Serializable {
private static final long serialVersionUID = -7720805057305804111L;
private static final int DEFAULT_INITIAL_CAPACITY = 11;
private transient Object[] queue;
private int size = 0;
private final Comparator<? super E> comparator;
private transient int modCount = 0;
public PriorityQueue() {
this(DEFAULT_INITIAL_CAPACITY, null);
}
public PriorityQueue(int initialCapacity) {
this(initialCapacity, null);
}
public PriorityQueue(int initialCapacity,
Comparator<? super E> comparator) {
if (initialCapacity < 1)
throw new IllegalArgumentException();
this.queue = new Object[initialCapacity + 1];
this.comparator = comparator;
}
private void initializeArray(Collection<? extends E> c) {
int sz = c.size();
int initialCapacity = (int)Math.min((sz * 110L) / 100,
Integer.MAX_VALUE - 1);
if (initialCapacity < 1)
initialCapacity = 1;
this.queue = new Object[initialCapacity + 1];
}
private void fillFromSorted(Collection<? extends E> c) {
for (Iterator<? extends E> i = c.iterator(); i.hasNext(); )
queue[++size] = i.next();
}
private void fillFromUnsorted(Collection<? extends E> c) {
for (Iterator<? extends E> i = c.iterator(); i.hasNext(); )
queue[++size] = i.next();
heapify();
}
public PriorityQueue(Collection<? extends E> c) {
initializeArray(c);
if (c instanceof SortedSet) {
SortedSet<? extends E> s = (SortedSet<? extends E>)c;
comparator = (Comparator<? super E>)s.comparator();
fillFromSorted(s);
} else if (c instanceof PriorityQueue) {
PriorityQueue<? extends E> s = (PriorityQueue<? extends E>) c;
comparator = (Comparator<? super E>)s.comparator();
fillFromSorted(s);
} else {
comparator = null;
fillFromUnsorted(c);
}
}
public PriorityQueue(PriorityQueue<? extends E> c) {
initializeArray(c);
comparator = (Comparator<? super E>)c.comparator();
fillFromSorted(c);
}
public PriorityQueue(SortedSet<? extends E> c) {
initializeArray(c);
comparator = (Comparator<? super E>)c.comparator();
fillFromSorted(c);
}
private void grow(int index) {
int newlen = queue.length;
if (index < newlen) // don't need to grow
return;
if (index == Integer.MAX_VALUE)
throw new OutOfMemoryError();
while (newlen <= index) {
if (newlen >= Integer.MAX_VALUE / 2) // avoid overflow
newlen = Integer.MAX_VALUE;
else
newlen <<= 2;
}
Object[] newQueue = new Object[newlen];
System.arraycopy(queue, 0, newQueue, 0, queue.length);
queue = newQueue;
}
public boolean offer(E o) {
if (o == null)
throw new NullPointerException();
modCount++;
++size;
// Grow backing store if necessary
if (size >= queue.length)
grow(size);
queue[size] = o;
fixUp(size);
return true;
}
public E peek() {
if (size == 0)
return null;
return (E) queue[1];
}
// Collection Methods - the first two override to update docs
public boolean add(E o) {
return offer(o);
}
public boolean remove(Object o) {
if (o == null)
return false;
if (comparator == null) {
for (int i = 1; i <= size; i++) {
if (((Comparable<E>)queue[i]).compareTo((E)o) == 0) {
removeAt(i);
return true;
}
}
} else {
for (int i = 1; i <= size; i++) {
if (comparator.compare((E)queue[i], (E)o) == 0) {
removeAt(i);
return true;
}
}
}
return false;
}
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
private int cursor = 1;
private int lastRet = 0;
private int expectedModCount = modCount;
private ArrayList<E> forgetMeNot = null;
private Object lastRetElt = null;
public boolean hasNext() {
return cursor <= size || forgetMeNot != null;
}
public E next() {
checkForComodification();
E result;
if (cursor <= size) {
result = (E) queue[cursor];
lastRet = cursor++;
}
else if (forgetMeNot == null)
throw new NoSuchElementException();
else {
int remaining = forgetMeNot.size();
result = forgetMeNot.remove(remaining - 1);
if (remaining == 1)
forgetMeNot = null;
lastRet = 0;
lastRetElt = result;
}
return result;
}
public void remove() {
checkForComodification();
if (lastRet != 0) {
E moved = PriorityQueue.this.removeAt(lastRet);
lastRet = 0;
if (moved == null) {
cursor--;
} else {
if (forgetMeNot == null)
forgetMeNot = new ArrayList<E>();
forgetMeNot.add(moved);
}
} else if (lastRetElt != null) {
PriorityQueue.this.remove(lastRetElt);
lastRetElt = null;
} else {
throw new IllegalStateException();
}
expectedModCount = modCount;
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
public int size() {
return size;
}
/**
* Removes all elements from the priority queue.
* The queue will be empty after this call returns.
*/
public void clear() {
modCount++;
// Null out element references to prevent memory leak
for (int i=1; i<=size; i++)
queue[i] = null;
size = 0;
}
public E poll() {
if (size == 0)
return null;
modCount++;
E result = (E) queue[1];
queue[1] = queue[size];
queue[size--] = null; // Drop extra ref to prevent memory leak
if (size > 1)
fixDown(1);
return result;
}
private E removeAt(int i) {
assert i > 0 && i <= size;
modCount++;
E moved = (E) queue[size];
queue[i] = moved;
queue[size--] = null; // Drop extra ref to prevent memory leak
if (i <= size) {
fixDown(i);
if (queue[i] == moved) {
fixUp(i);
if (queue[i] != moved)
return moved;
}
}
return null;
}
private void fixUp(int k) {
if (comparator == null) {
while (k > 1) {
int j = k >> 1;
if (((Comparable<E>)queue[j]).compareTo((E)queue[k]) <= 0)
break;
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
k = j;
}
} else {
while (k > 1) {
int j = k >>> 1;
if (comparator.compare((E)queue[j], (E)queue[k]) <= 0)
break;
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
k = j;
}
}
}
private void fixDown(int k) {
int j;
if (comparator == null) {
while ((j = k << 1) <= size && (j > 0)) {
if (j<size &&
((Comparable<E>)queue[j]).compareTo((E)queue[j+1]) > 0)
j++; // j indexes smallest kid
if (((Comparable<E>)queue[k]).compareTo((E)queue[j]) <= 0)
break;
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
k = j;
}
} else {
while ((j = k << 1) <= size && (j > 0)) {
if (j<size &&
comparator.compare((E)queue[j], (E)queue[j+1]) > 0)
j++; // j indexes smallest kid
if (comparator.compare((E)queue[k], (E)queue[j]) <= 0)
break;
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
k = j;
}
}
}
private void heapify() {
for (int i = size/2; i >= 1; i--)
fixDown(i);
}
public Comparator<? super E> comparator() {
return comparator;
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
s.defaultWriteObject();
// Write out array length
s.writeInt(queue.length);
// Write out all elements in the proper order.
for (int i=1; i<=size; i++)
s.writeObject(queue[i]);
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in array length and allocate array
int arrayLength = s.readInt();
queue = new Object[arrayLength];
// Read in all elements in the proper order.
for (int i=1; i<=size; i++)
queue[i] = (E) s.readObject();
}
}