Skip to content

Commit 9534dd1

Browse files
Improved KTopCollector performance
1 parent 76db68f commit 9534dd1

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

src/YASL/CEstimationFor.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ public class CEstimationFor<T> implements Comparable<CEstimationFor<T>> {
44
public final T Item;
55
public final long Count;
66

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

1212
@Override
@@ -17,18 +17,16 @@ public String toString() {
1717
@SuppressWarnings("unchecked")
1818
@Override
1919
public int compareTo(CEstimationFor<T> o) {
20-
final double delta = o.Count - Count;
21-
if (0 < delta)
22-
return 1;
23-
if (delta < 0)
24-
return -1;
25-
return -((Comparable<T>)Item).compareTo(o.Item);
20+
final long delta = o.Count - Count;
21+
if (0 != delta)
22+
return (int) delta;
23+
return -((Comparable<T>) Item).compareTo(o.Item);
2624
}
2725

2826
@SuppressWarnings("unchecked")
2927
@Override
3028
public boolean equals(Object obj) {
31-
return ((CEstimationFor<T>)obj).Item.equals(Item);
29+
return ((CEstimationFor<T>) obj).Item.equals(Item);
3230
}
3331

3432
}
Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,61 @@
11
package YASL.Collectors;
22

3-
import java.util.Comparator;
4-
import java.util.PriorityQueue;
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
58
import java.util.stream.Collectors;
69

710
import YASL.CEstimatedItems;
811
import YASL.CEstimationFor;
912
import YASL.IEstimationCollector;
1013

1114
public class KTopCollector<T> implements IEstimationCollector<T> {
12-
private final long _K;
13-
private final PriorityQueue<CEstimationFor<T>> _queue;
14-
private long _min = 0L;
15+
private final int _K;
16+
private long _min = 0L;
17+
private final List<CEstimationFor<T>> _items;
18+
private final Map<T, CEstimationFor<T>> _byValue;
1519

16-
public KTopCollector(long K) {
20+
public KTopCollector(int K) {
1721
this._K = K;
18-
_queue = new PriorityQueue<>(new Comparator<CEstimationFor<T>>() {
19-
20-
@Override
21-
public int compare(CEstimationFor<T> o1, CEstimationFor<T> o2) {
22-
return o2.compareTo(o1);
23-
}
24-
});
22+
_items = new ArrayList<>();
23+
_byValue = new HashMap<>();
2524
}
2625

2726
@Override
2827
public void put(T item, long cnt) {
2928
if (cnt < _min)
3029
return;
3130

32-
CEstimationFor<T> est = new CEstimationFor<>(item, cnt);
33-
if (_queue.contains(est)) {
34-
_queue.remove(est);
31+
CEstimationFor<T> est = _byValue.get(item);
32+
if (null != est) {
33+
_items.remove(Collections.binarySearch(_items, est));
3534
}
36-
_queue.offer(est);
3735

38-
while (_K < _queue.size())
39-
_queue.poll();
36+
est = new CEstimationFor<T>(item, cnt);
37+
_byValue.put(item, est);
38+
int ind = Collections.binarySearch(_items, est);
39+
_items.add(-(ind + 1), est);
40+
41+
if ((_K * 1.1) < _items.size())
42+
fitK();
43+
}
44+
45+
private void fitK() {
46+
while (_K < _items.size()) {
47+
_byValue.remove(_items.remove(_K).Item);
48+
}
4049

41-
if (_queue.size() == _K)
42-
_min = _queue.peek().Count;
50+
if (_items.size() == _K)
51+
_min = _items.get(_K - 1).Count;
4352
}
4453

4554
@Override
4655
public CEstimatedItems<T> collect() {
56+
fitK();
4757
return new CEstimatedItems<>( //
48-
_queue.stream() //
49-
.map(e -> new CEstimationFor<>(e.Item, e.Count)) //
50-
.sorted() //
51-
.collect(Collectors.toList()) //
58+
_items.stream().collect(Collectors.toList()) //
5259
);
5360
}
5461
}

0 commit comments

Comments
 (0)