Skip to content

Commit 0042ad4

Browse files
WIP
1 parent 3712bf2 commit 0042ad4

28 files changed

+646
-117
lines changed

src/Tests/Test_CountMinCounter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import org.junit.Assert;
66
import org.junit.Test;
77

8-
import YASL.CExactCounter;
98
import YASL.IItemsCounter;
109
import YASL.Counters.CCMCounter;
1110
import YASL.Counters.CCountMinParams;
11+
import YASL.Counters.CExactCounter;
1212
import YASL.Hashing.ChgBasic;
1313

1414
public class Test_CountMinCounter {

src/Tests/Test_CountingStrings.java

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/Tests/Test_Estimator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import org.junit.Test;
55

66
import YASL.CEstimator;
7-
import YASL.CExactCounter;
87
import YASL.IEstimator;
98
import YASL.Collectors.CExactCollector;
9+
import YASL.Counters.CExactCounter;
1010

1111
public class Test_Estimator {
1212
@Test

src/Tests/Test_KTopCollector.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package Tests;
22

3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
36
import org.junit.Assert;
47
import org.junit.Test;
58

69
import YASL.IEstimationCollector;
710
import YASL.Collectors.KTopCollector;
11+
import YASL.Collectors.StoredKTop;
12+
import YASL.Streams.CtisInteger;
13+
import YASL.Streams.CtosInteger;
814

915
public class Test_KTopCollector {
1016

@@ -34,4 +40,26 @@ public void sameCounts() {
3440
collector.collect().toString() //
3541
);
3642
}
43+
44+
@Test
45+
public void loading() throws Exception {
46+
final IEstimationCollector<Integer> collectorA = new KTopCollector<>(3);
47+
collectorA.put(3, 4);
48+
collectorA.put(2, 3);
49+
50+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
51+
collectorA.store(new CtosInteger(stream));
52+
53+
final IEstimationCollector<Integer> collectorB = (new StoredKTop<Integer>( //
54+
new CtisInteger( //
55+
new ByteArrayInputStream(stream.toByteArray()) //
56+
), //
57+
3 //
58+
)).load();
59+
60+
Assert.assertEquals( //
61+
collectorA.collect().toString(), //
62+
collectorB.collect().toString() //
63+
);
64+
}
3765
}

src/Tests/Test_csTable.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 java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.DataInputStream;
6+
import java.io.DataOutputStream;
7+
import java.io.IOException;
8+
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
import YASL.Counters.Tables.CcsIntTable;
13+
import YASL.Counters.Tables.CcsStoredTable;
14+
import YASL.Counters.Tables.CcsTable;
15+
16+
public class Test_csTable {
17+
18+
@Test
19+
public void loadIntTable() throws IOException {
20+
CcsTable tableA = new CcsIntTable(2, 1);
21+
tableA.put(0, 0, 5);
22+
tableA.put(1, 0, 7);
23+
24+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
25+
tableA.store(new DataOutputStream(stream));
26+
27+
CcsTable tableB = (new CcsStoredTable( //
28+
new DataInputStream( //
29+
new ByteArrayInputStream(stream.toByteArray()) //
30+
), //
31+
2, 1 //
32+
)).asInt();
33+
34+
Assert.assertEquals(tableA.read(0, 0), tableB.read(0, 0));
35+
Assert.assertEquals(tableA.read(1, 0), tableB.read(1, 0));
36+
}
37+
}

src/YASL/CEstimator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package YASL;
22

3+
import java.io.IOException;
4+
5+
import YASL.Streams.TypedOutputStream;
6+
37
public class CEstimator<T> implements IEstimator<T> {
48
private final IItemsCounter<T> _counter;
59
private final IEstimationCollector<T> _collector;
@@ -22,4 +26,10 @@ public void add(T item, long count) {
2226
public CEstimatedItems<T> estimate() {
2327
return _collector.collect();
2428
}
29+
30+
@Override
31+
public void store(TypedOutputStream<T> stream) throws IOException {
32+
_counter.store(stream);
33+
_collector.store(stream);
34+
}
2535
}

src/YASL/Collectors/CExactCollector.java

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

3+
import java.io.IOException;
34
import java.util.HashMap;
45
import java.util.Map;
56
import java.util.stream.Collectors;
67

78
import YASL.CEstimatedItems;
89
import YASL.CEstimationFor;
910
import YASL.IEstimationCollector;
11+
import YASL.Streams.TypedOutputStream;
1012

1113
public class CExactCollector<T> implements IEstimationCollector<T> {
1214

@@ -26,4 +28,9 @@ public CEstimatedItems<T> collect() {
2628
.collect(Collectors.toList()) //
2729
);
2830
}
31+
32+
@Override
33+
public void store(TypedOutputStream<T> stream) throws IOException {
34+
throw new UnsupportedOperationException();
35+
}
2936
}

src/YASL/Collectors/KTopCollector.java

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

3+
import java.io.IOException;
34
import java.util.ArrayList;
45
import java.util.Collections;
56
import java.util.HashMap;
@@ -10,17 +11,26 @@
1011
import YASL.CEstimatedItems;
1112
import YASL.CEstimationFor;
1213
import YASL.IEstimationCollector;
14+
import YASL.Streams.TypedOutputStream;
1315

1416
public class KTopCollector<T> implements IEstimationCollector<T> {
1517
private final int _K;
1618
private long _min = 0L;
1719
private final List<CEstimationFor<T>> _items;
1820
private final Map<T, CEstimationFor<T>> _byValue;
1921

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;
30+
}
31+
2032
public KTopCollector(int K) {
21-
this._K = K;
22-
_items = new ArrayList<>();
23-
_byValue = new HashMap<>();
33+
this(K, new ArrayList<>(), new HashMap<>());
2434
}
2535

2636
@Override
@@ -58,4 +68,15 @@ public CEstimatedItems<T> collect() {
5868
_items.stream().collect(Collectors.toList()) //
5969
);
6070
}
71+
72+
@Override
73+
public void store(TypedOutputStream<T> stream) throws IOException {
74+
fitK();
75+
76+
stream.writeInt(_items.size());
77+
for (CEstimationFor<T> item : _items) {
78+
stream.writeType(item.Item);
79+
stream.writeLong(item.Count);
80+
}
81+
}
6182
}

src/YASL/Collectors/StoredKTop.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package YASL.Collectors;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import YASL.CEstimationFor;
10+
import YASL.IEstimationCollector;
11+
import YASL.Streams.TypedInputStream;
12+
13+
public class StoredKTop<T> {
14+
private final TypedInputStream<T> _stream;
15+
private final int _size;
16+
17+
public StoredKTop(TypedInputStream<T> _stream, int _size) {
18+
this._stream = _stream;
19+
this._size = _size;
20+
}
21+
22+
public IEstimationCollector<T> load() throws IOException {
23+
final int cnt = _stream.readInt();
24+
final List<CEstimationFor<T>> items = new ArrayList<>(cnt);
25+
final Map<T, CEstimationFor<T>> byValue = new HashMap<>();
26+
27+
for (int i = 0; i < cnt; i++) {
28+
final T item = _stream.readType();
29+
final long itemCount = _stream.readLong();
30+
final CEstimationFor<T> est= new CEstimationFor<>(item, itemCount);
31+
32+
items.add(est);
33+
byValue.put(item, est);
34+
}
35+
return new KTopCollector<>(_size, items, byValue);
36+
}
37+
}

0 commit comments

Comments
 (0)