|
1 | 1 | package YASL.Collectors;
|
2 | 2 |
|
3 | 3 | import java.io.IOException;
|
4 |
| -import java.util.ArrayList; |
5 |
| -import java.util.Collections; |
6 |
| -import java.util.HashMap; |
7 | 4 | import java.util.List;
|
8 |
| -import java.util.Map; |
9 | 5 | import java.util.stream.Collectors;
|
10 | 6 |
|
11 | 7 | import YASL.CEstimatedItems;
|
12 | 8 | import YASL.CEstimationFor;
|
13 | 9 | import YASL.IEstimationCollector;
|
| 10 | +import YASL.Collectors.Heap.CFixedMinHeap; |
| 11 | +import YASL.Collectors.Heap.CprSimple; |
14 | 12 | import YASL.Streams.TypedOutputStream;
|
15 | 13 |
|
16 | 14 | public class KTopCollector<T> implements IEstimationCollector<T> {
|
17 |
| - private final int _K; |
18 |
| - private long _min = 0L; |
19 |
| - private final List<CEstimationFor<T>> _items; |
20 |
| - private final Map<T, CEstimationFor<T>> _byValue; |
| 15 | + private static class CTopItem<T> extends CprSimple<T> { |
| 16 | + public CTopItem(T value, long priority) { |
| 17 | + super(value, (int) priority); |
| 18 | + } |
21 | 19 |
|
22 |
| - public KTopCollector( // |
23 |
| - int _K, // |
24 |
| - List<CEstimationFor<T>> _items, // |
25 |
| - Map<T, CEstimationFor<T>> _byValue // |
26 |
| - ) { |
27 |
| - this._K = _K; |
28 |
| - this._items = _items; |
29 |
| - this._byValue = _byValue; |
| 20 | + public void update(long cnt) { |
| 21 | + _priority = (int) cnt; |
| 22 | + } |
30 | 23 | }
|
31 | 24 |
|
32 |
| - public KTopCollector(int K) { |
33 |
| - this(K, new ArrayList<>(), new HashMap<>()); |
| 25 | + private final CFixedMinHeap<CTopItem<T>, T> _heap; |
| 26 | + |
| 27 | + public KTopCollector(int size) { |
| 28 | + this._heap = new CFixedMinHeap<>(size); |
34 | 29 | }
|
35 | 30 |
|
36 | 31 | @Override
|
37 | 32 | public void put(T item, long cnt) {
|
38 |
| - if (cnt < _min) |
39 |
| - return; |
40 |
| - |
41 |
| - CEstimationFor<T> est = _byValue.get(item); |
42 |
| - if (null != est) { |
43 |
| - _items.remove(Collections.binarySearch(_items, est)); |
| 33 | + final CTopItem<T> ratted = _heap.get(item); |
| 34 | + if (null == ratted) { |
| 35 | + _heap.add(new CTopItem<T>(item, cnt)); |
| 36 | + } else { |
| 37 | + ratted.update(cnt); |
| 38 | + _heap.update(item); |
44 | 39 | }
|
45 |
| - |
46 |
| - est = new CEstimationFor<T>(item, cnt); |
47 |
| - _byValue.put(item, est); |
48 |
| - int ind = Collections.binarySearch(_items, est); |
49 |
| - _items.add(-ind - 1, est); |
50 |
| - |
51 |
| - if ((_K * 1.5) < _items.size()) |
52 |
| - fitK(); |
53 | 40 | }
|
54 | 41 |
|
55 |
| - private void fitK() { |
56 |
| - while (_K < _items.size()) { |
57 |
| - _byValue.remove(_items.remove(_items.size() - 1).Item); |
58 |
| - } |
59 |
| - |
60 |
| - if (_items.size() == _K) |
61 |
| - _min = _items.get(_K - 1).Count; |
| 42 | + private List<CEstimationFor<T>> items() { |
| 43 | + return _heap.items() // |
| 44 | + .stream() // |
| 45 | + .map(x -> new CEstimationFor<>(x.value, x.Priority())) // |
| 46 | + .collect(Collectors.toList()); |
62 | 47 | }
|
63 | 48 |
|
64 | 49 | @Override
|
65 | 50 | public CEstimatedItems<T> collect() {
|
66 |
| - fitK(); |
67 |
| - return new CEstimatedItems<>( // |
68 |
| - _items.stream().collect(Collectors.toList()) // |
69 |
| - ); |
| 51 | + return new CEstimatedItems<>(items()); |
70 | 52 | }
|
71 | 53 |
|
72 | 54 | @Override
|
73 | 55 | public void store(TypedOutputStream<T> stream) throws IOException {
|
74 |
| - fitK(); |
75 |
| - |
76 |
| - stream.writeInt(_items.size()); |
77 |
| - for (CEstimationFor<T> item : _items) { |
| 56 | + List<CEstimationFor<T>> items = items(); |
| 57 | + stream.writeInt(items.size()); |
| 58 | + for (CEstimationFor<T> item : items) { |
78 | 59 | stream.writeType(item.Item);
|
79 | 60 | stream.writeLong(item.Count);
|
80 | 61 | }
|
|
0 commit comments