Skip to content

Commit 0fb6919

Browse files
Replaced old impl. with FixedMinHeap (much faster)
1 parent a6f8ee7 commit 0fb6919

File tree

2 files changed

+31
-59
lines changed

2 files changed

+31
-59
lines changed

src/YASL/Collectors/KTopCollector.java

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,61 @@
11
package YASL.Collectors;
22

33
import java.io.IOException;
4-
import java.util.ArrayList;
5-
import java.util.Collections;
6-
import java.util.HashMap;
74
import java.util.List;
8-
import java.util.Map;
95
import java.util.stream.Collectors;
106

117
import YASL.CEstimatedItems;
128
import YASL.CEstimationFor;
139
import YASL.IEstimationCollector;
10+
import YASL.Collectors.Heap.CFixedMinHeap;
11+
import YASL.Collectors.Heap.CprSimple;
1412
import YASL.Streams.TypedOutputStream;
1513

1614
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+
}
2119

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+
}
3023
}
3124

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);
3429
}
3530

3631
@Override
3732
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);
4439
}
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();
5340
}
5441

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());
6247
}
6348

6449
@Override
6550
public CEstimatedItems<T> collect() {
66-
fitK();
67-
return new CEstimatedItems<>( //
68-
_items.stream().collect(Collectors.toList()) //
69-
);
51+
return new CEstimatedItems<>(items());
7052
}
7153

7254
@Override
7355
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) {
7859
stream.writeType(item.Item);
7960
stream.writeLong(item.Count);
8061
}

src/YASL/Collectors/StoredKTop.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package YASL.Collectors;
22

33
import java.io.IOException;
4-
import java.util.ArrayList;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
84

9-
import YASL.CEstimationFor;
105
import YASL.Streams.TypedInputStream;
116

127
public class StoredKTop<T> {
@@ -18,17 +13,13 @@ public StoredKTop(int _size) {
1813

1914
public KTopCollector<T> load(TypedInputStream<T> stream) throws IOException {
2015
final int cnt = stream.readInt();
21-
final List<CEstimationFor<T>> items = new ArrayList<>(cnt);
22-
final Map<T, CEstimationFor<T>> byValue = new HashMap<>();
16+
final KTopCollector<T> res = new KTopCollector<>(_size);
2317

2418
for (int i = 0; i < cnt; i++) {
2519
final T item = stream.readType();
2620
final long itemCount = stream.readLong();
27-
final CEstimationFor<T> est = new CEstimationFor<>(item, itemCount);
28-
29-
items.add(est);
30-
byValue.put(item, est);
21+
res.put(item, itemCount);
3122
}
32-
return new KTopCollector<>(_size, items, byValue);
23+
return res;
3324
}
3425
}

0 commit comments

Comments
 (0)