Skip to content

Commit a506f06

Browse files
committed
Implemented suggestons from @jbgi for PriorityQueue
1 parent 3718313 commit a506f06

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

core/src/main/java/fj/data/PriorityQueue.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
import static fj.data.List.list;
1414

1515
/**
16-
* A priority queue implementation backed by a {@link fj.data.fingertrees.FingerTree}. The finger tree nodes are annotated with type K, are combined using a monoid of K and both the key and value are stored in the leaf. Priorities of the same value are returned FIFO (first in, first out).
16+
* A priority queue implementation backed by a
17+
* {@link fj.data.fingertrees.FingerTree}. The finger tree nodes are
18+
* annotated with type K, are combined using a monoid of K and both the
19+
* key and value are stored in the leaf. Priorities of the same value
20+
* are returned FIFO (first in, first out).
1721
*
1822
* Created by MarkPerry on 31 May 16.
1923
*/
20-
public class PriorityQueue<K, A> {
24+
public final class PriorityQueue<K, A> {
2125

2226
private final FingerTree<K, P2<K, A>> ftree;
2327
private final Equal<K> equal;
@@ -31,7 +35,7 @@ private PriorityQueue(Equal<K> e, FingerTree<K, P2<K, A>> ft) {
3135
* Creates a priority queue from a finger tree.
3236
*/
3337
public static <K, A> PriorityQueue<K, A> priorityQueue(Equal<K> e, FingerTree<K, P2<K, A>> ft) {
34-
return new PriorityQueue<K, A>(e, ft);
38+
return new PriorityQueue<>(e, ft);
3539
}
3640

3741
/**
@@ -41,23 +45,24 @@ public static <K, A> PriorityQueue<K, A> priorityQueue(Equal<K> e, FingerTree<K,
4145
* @param e A value to compare key equality.
4246
*/
4347
public static <K, A> PriorityQueue<K, A> empty(Monoid<K> m, Equal<K> e) {
44-
return priorityQueue(e, FingerTree.empty(m, (P2<K, A> p) -> p._1()));
48+
return priorityQueue(e, FingerTree.empty(m, P2.__1()));
4549
}
4650

4751
/**
4852
* An empty priority queue with integer priorities.
4953
*/
5054
public static <A> PriorityQueue<Integer, A> emptyInt() {
51-
return priorityQueue(Equal.intEqual, FingerTree.empty(Monoid.intMaxMonoid, (P2<Integer, A> p) -> p._1()));
55+
return empty(Monoid.intMaxMonoid, Equal.intEqual);
5256
}
5357

5458
/**
5559
* Maps the values in each node with function f.
5660
*/
5761
public <B> PriorityQueue<K, B> map(F<A, B> f) {
5862
return priorityQueue(equal,
59-
ftree.map(p2 -> p2.map2(a -> f.f(a)),
60-
FingerTree.measured(ftree.measured().monoid(), (P2<K, B> p2) -> p2._1()))
63+
ftree.map(P2.map2_(f),
64+
FingerTree.measured(ftree.measured().monoid(), P2.__1())
65+
)
6166
);
6267
}
6368

@@ -91,6 +96,9 @@ public Option<P2<K, A>> top() {
9196
return p._2().headOption();
9297
}
9398

99+
/**
100+
* Returns all the elements of the queue with the highest (same) priority.
101+
*/
94102
public List<P2<K, A>> topN() {
95103
Stream<P2<K, A>> s = toStream();
96104
if (s.isEmpty()) {
@@ -153,7 +161,7 @@ public PriorityQueue<K, A> dequeue() {
153161
/**
154162
* Returns a tuple of the node with the highest priority and the rest of the priority queue.
155163
*/
156-
public P2<Option<P2<K, A>>, PriorityQueue<K, A>> dequeueTop() {
164+
public P2<Option<P2<K, A>>, PriorityQueue<K, A>> topDequeue() {
157165
return P.p(top(), dequeue());
158166
}
159167

props-core/src/test/java/fj/data/properties/PriorityQueueProperties.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public Property sorted() {
9494
return property(arbPriorityQueueIntegerString, pq -> {
9595
List<P2<Integer, String>> expected = pq.toList().sort(Ord.p2Ord1(Ord.intOrd.reverse()));
9696
List<P2<Integer, String>> actual = pq.toList();
97-
assertThat(actual, equalTo(expected));
9897
return prop(actual.equals(expected));
9998
});
10099
}

0 commit comments

Comments
 (0)