/** * Heapify the given Memory as an ArrayOfDoublesUnion * @param mem the given Memory * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion heapify(final Memory mem) { return heapify(mem, DEFAULT_UPDATE_SEED); }
/** * Heapify the given Memory and seed as an ArrayOfDoublesUnion * @param mem the given Memory * @param seed the given seed * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion heapifyUnion(final Memory mem, final long seed) { return ArrayOfDoublesUnion.heapify(mem, seed); }
@Test public void heapDeserializeV0_9_1() throws Exception { byte[] bytes = TestUtil.readBytesFromFile(getClass().getClassLoader().getResource("ArrayOfDoublesUnion_v0.9.1.bin").getFile()); ArrayOfDoublesUnion union2 = ArrayOfDoublesUnion.heapify(Memory.wrap(bytes)); ArrayOfDoublesCompactSketch result = union2.getResult(); Assert.assertEquals(result.getEstimate(), 12288.0, 12288 * 0.01); union2.reset(); result = union2.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertFalse(result.isEstimationMode()); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getTheta(), 1.0); double[][] values = result.getValues(); for (int i = 0; i < values.length; i++) { Assert.assertEquals(values[i][0], 2.0); } }
/** * Heapify the given Memory and seed as an ArrayOfDoublesUnion * @param mem the given Memory * @param seed the given seed * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion heapifyUnion(final Memory mem, final long seed) { return ArrayOfDoublesUnion.heapify(mem, seed); }
/** * Heapify the given Memory as an ArrayOfDoublesUnion * @param mem the given Memory * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion heapify(final Memory mem) { return heapify(mem, DEFAULT_UPDATE_SEED); }
union1.update(sketch2); ArrayOfDoublesUnion union2 = ArrayOfDoublesUnion.heapify(Memory.wrap(union1.toByteArray())); ArrayOfDoublesCompactSketch result = union2.getResult(); Assert.assertEquals(result.getEstimate(), 12288.0, 12288 * 0.01);
@Test public void directToHeap() { ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().build(); sketch1.update(1, new double[] {1.0}); sketch1.update(1, new double[] {1.0}); sketch1.update(1, new double[] {1.0}); sketch1.update(2, new double[] {1.0}); ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder().build(); sketch2.update(2, new double[] {1.0}); sketch2.update(2, new double[] {1.0}); sketch2.update(3, new double[] {1.0}); sketch2.update(3, new double[] {1.0}); sketch2.update(3, new double[] {1.0}); ArrayOfDoublesUnion directUnion = new ArrayOfDoublesSetOperationBuilder().buildUnion(WritableMemory.wrap(new byte[1000000])); directUnion.update(sketch1); ArrayOfDoublesUnion heapUnion = ArrayOfDoublesUnion.heapify(Memory.wrap(directUnion.toByteArray())); heapUnion.update(sketch2); ArrayOfDoublesCompactSketch result = heapUnion.getResult(); Assert.assertFalse(result.isEmpty()); Assert.assertEquals(result.getEstimate(), 3.0); double[][] values = result.getValues(); Assert.assertEquals(values.length, 3); Assert.assertEquals(values[0][0], 3.0); Assert.assertEquals(values[1][0], 3.0); Assert.assertEquals(values[2][0], 3.0); }
@Test public void heapSerializeDeserializeWithSeed() { long seed = 1; int key = 0; ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().setSeed(seed).build(); for (int i = 0; i < 8192; i++) { sketch1.update(key++, new double[] {1.0}); } key -= 4096; // overlap half of the entries ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder().setSeed(seed).build(); for (int i = 0; i < 8192; i++) { sketch2.update(key++, new double[] {1.0}); } ArrayOfDoublesUnion union1 = new ArrayOfDoublesSetOperationBuilder().setSeed(seed).buildUnion(); union1.update(sketch1); union1.update(sketch2); ArrayOfDoublesUnion union2 = ArrayOfDoublesUnion.heapify(Memory.wrap(union1.toByteArray()), seed); ArrayOfDoublesCompactSketch result = union2.getResult(); Assert.assertEquals(result.getEstimate(), 12288.0, 12288 * 0.01); }