use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkExactUnionNoOverlap.
@Test
public void checkExactUnionNoOverlap() {
// 512
final int lgK = 9;
final int k = 1 << lgK;
final int u = k;
final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
for (int i = 0; i < u / 2; i++) {
// 256
usk1.update(i);
}
for (int i = u / 2; i < u; i++) {
// 256 no overlap
usk2.update(i);
}
// exact, no overlap
assertEquals(u, usk1.getEstimate() + usk2.getEstimate(), 0.0);
final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
// update with heap UpdateSketch
union.union(usk1);
// update with heap UpdateSketch
union.union(usk2);
testAllCompactForms(union, u, 0.0);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkWrapExact.
// these parallel the checkHeapifyExact, etc.
@Test
public void checkWrapExact() {
// 512
final int lgK = 9;
final int k = 1 << lgK;
final int u = k;
final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
for (int i = 0; i < u / 2; i++) {
// 256
usk1.update(i);
}
for (int i = u / 2; i < u; i++) {
// 256 no overlap
usk2.update(i);
}
// exact, no overlap
assertEquals(u, usk1.getEstimate() + usk2.getEstimate(), 0.0);
final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
// update with heap UpdateSketch
union.union(usk1);
// update with heap UpdateSketch
union.union(usk2);
testAllCompactForms(union, u, 0.0);
final Union union2 = Sketches.wrapUnion(WritableMemory.writableWrap(union.toByteArray()));
testAllCompactForms(union2, u, 0.0);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkUnionMemToString.
@Test
public void checkUnionMemToString() {
final int k = 64;
// union memory
final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkExactUnionWithOverlap.
@Test
public void checkExactUnionWithOverlap() {
// 512
final int lgK = 9;
final int k = 1 << lgK;
final int u = k;
final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
for (int i = 0; i < u / 2; i++) {
// 256
usk1.update(i);
}
for (int i = 0; i < u; i++) {
// 512, 256 overlapped
usk2.update(i);
}
// exact, overlapped
assertEquals(u, usk1.getEstimate() + usk2.getEstimate() / 2, 0.0);
final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
// update with heap UpdateSketch
union.union(usk1);
// update with heap UpdateSketch
union.union(usk2);
testAllCompactForms(union, u, 0.0);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkSerVer2Handling.
@Test
public void checkSerVer2Handling() {
// 4096
final int lgK = 12;
final int k = 1 << lgK;
final int u1 = 2 * k;
// smaller exact sketch forces early stop
final int u2 = 1024;
final int totU = u1 + u2;
final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
for (int i = 0; i < u1; i++) {
// 2*k
usk1.update(i);
}
for (int i = u1; i < totU; i++) {
// 2*k + 1024 no overlap
usk2.update(i);
}
final Memory v2mem1 = convertSerVer3toSerVer2(usk1.compact(true, null), Util.DEFAULT_UPDATE_SEED);
final Memory v2mem2 = convertSerVer3toSerVer2(usk2.compact(true, null), Util.DEFAULT_UPDATE_SEED);
// union memory
final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
union.union(v2mem1);
union.union(v2mem2);
final CompactSketch cOut = union.getResult(true, null);
assertEquals(cOut.getEstimate(), totU, .05 * k);
}
Aggregations