-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPQ.java
More file actions
46 lines (36 loc) · 1018 Bytes
/
PQ.java
File metadata and controls
46 lines (36 loc) · 1018 Bytes
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
package cs2030.util;
import java.util.PriorityQueue;
import java.util.Comparator;
public class PQ<T> {
private static final int initialCap = 1;
private final PriorityQueue<T> pq;
public PQ(Comparator<? super T> cmp) {
this.pq = new PriorityQueue<T>(initialCap, cmp);
}
public PQ(PriorityQueue<T> newpq) {
this.pq = newpq;
}
public PQ(PQ<T> newpq) {
this.pq = newpq.getInnerPQ();
}
public PriorityQueue<T> getInnerPQ() {
return this.pq;
}
public PQ<T> add(T elem) {
PriorityQueue<T> newpq = new PriorityQueue<T>(pq);
newpq.add(elem);
return new PQ<T>(newpq);
}
public boolean isEmpty() {
return pq.size() == 0;
}
public Pair<T, PQ<T>> poll() {
PQ<T> newpq = new PQ<T>(new PriorityQueue<T>(pq));
T elem = newpq.pq.poll();
return Pair.<T, PQ<T>>of(elem, newpq);
}
@Override
public String toString() {
return pq.toString();
}
}