Skip to content

Commit ac5d870

Browse files
Simple KTop
1 parent 10ef4fe commit ac5d870

File tree

4 files changed

+102
-8
lines changed

4 files changed

+102
-8
lines changed

src/Tests/Test_Estimator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void simplest() {
2222
}
2323

2424
Assert.assertEquals( //
25-
"4 -> 4.0, 3 -> 3.0, 2 -> 2.0, 1 -> 1.0", //
25+
"4 -> 4, 3 -> 3, 2 -> 2, 1 -> 1", //
2626
est.estimate().toString() //
2727
);
2828
}

src/Tests/Test_KTopCollector.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Tests;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import YASL.IEstimationCollector;
7+
import YASL.KTopCollector;
8+
9+
public class Test_KTopCollector {
10+
11+
@Test
12+
public void simplest() {
13+
IEstimationCollector<Integer> collector = new KTopCollector<>(2);
14+
for (int i = 1; i <= 4; i++) {
15+
collector.put(i, i);
16+
}
17+
18+
Assert.assertEquals( //
19+
"4 -> 4, 3 -> 3", //
20+
collector.collect().toString() //
21+
);
22+
}
23+
24+
@Test
25+
public void sameCounts() {
26+
IEstimationCollector<Integer> collector = new KTopCollector<>(3);
27+
collector.put(2, 3);
28+
collector.put(3, 4);
29+
collector.put(4, 4);
30+
collector.put(5, 5);
31+
32+
Assert.assertEquals( //
33+
"5 -> 5, 4 -> 4, 3 -> 4", //
34+
collector.collect().toString() //
35+
);
36+
}
37+
}

src/YASL/CEstimationFor.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
package YASL;
22

33
public class CEstimationFor<T> implements Comparable<CEstimationFor<T>> {
4-
public final T Item;
5-
public final double Frequence;
4+
public final T Item;
5+
public final long Count;
66

7-
public CEstimationFor(T item, double frequence) {
7+
public CEstimationFor(T item, long frequence) {
88
Item = item;
9-
Frequence = frequence;
9+
Count = frequence;
1010
}
1111

1212
@Override
1313
public String toString() {
14-
return Item.toString() + " -> " + Frequence;
14+
return Item.toString() + " -> " + Count;
1515
}
1616

17+
@SuppressWarnings("unchecked")
1718
@Override
1819
public int compareTo(CEstimationFor<T> o) {
19-
final double delta = o.Frequence - Frequence;
20+
final double delta = o.Count - Count;
2021
if (0 < delta)
2122
return 1;
2223
if (delta < 0)
2324
return -1;
24-
return 0;
25+
return -((Comparable)Item).compareTo(o.Item);
26+
}
27+
28+
@SuppressWarnings("unchecked")
29+
@Override
30+
public boolean equals(Object obj) {
31+
return ((CEstimationFor<T>)obj).Item.equals(Item);
2532
}
2633

2734
}

src/YASL/KTopCollector.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package YASL;
2+
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
import java.util.stream.Collectors;
6+
7+
public class KTopCollector<T> implements IEstimationCollector<T> {
8+
private final long _K;
9+
private final PriorityQueue<CEstimationFor<T>> _queue;
10+
private long _min = 0L;
11+
12+
public KTopCollector(long K) {
13+
this._K = K;
14+
_queue = new PriorityQueue<>(new Comparator<CEstimationFor<T>>() {
15+
16+
@Override
17+
public int compare(CEstimationFor<T> o1, CEstimationFor<T> o2) {
18+
return o2.compareTo(o1);
19+
}
20+
});
21+
}
22+
23+
@Override
24+
public void put(T item, long cnt) {
25+
if (cnt < _min)
26+
return;
27+
28+
CEstimationFor<T> est = new CEstimationFor<>(item, cnt);
29+
if (_queue.contains(est)) {
30+
_queue.remove(est);
31+
}
32+
_queue.offer(est);
33+
34+
while (_K < _queue.size())
35+
_queue.poll();
36+
37+
if (_queue.size() == _K)
38+
_min = _queue.peek().Count;
39+
}
40+
41+
@Override
42+
public CEstimatedItems<T> collect() {
43+
return new CEstimatedItems<>( //
44+
_queue.stream() //
45+
.map(e -> new CEstimationFor<>(e.Item, e.Count)) //
46+
.sorted() //
47+
.collect(Collectors.toList()) //
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)