Search in sources :

Example 1 with IntByteCursor

use of com.carrotsearch.hppc.cursors.IntByteCursor in project lucene-solr by apache.

the class HLL method homogeneousUnion.

/**
     * Computes the union of two HLLs of the same type, and stores the
     * result in this instance.
     *
     * @param other the other {@link HLL} instance to union into this one. This
     *        cannot be <code>null</code>.
     */
private void homogeneousUnion(final HLL other) {
    switch(type) {
        case EMPTY:
            // union of empty and empty is empty
            return;
        case EXPLICIT:
            for (LongCursor c : other.explicitStorage) {
                addRaw(c.value);
            }
            // NOTE:  #addRaw() will handle promotion, if necessary
            return;
        case SPARSE:
            for (IntByteCursor c : other.sparseProbabilisticStorage) {
                final int registerIndex = c.key;
                final byte registerValue = c.value;
                final byte currentRegisterValue = sparseProbabilisticStorage.get(registerIndex);
                if (registerValue > currentRegisterValue) {
                    sparseProbabilisticStorage.put(registerIndex, registerValue);
                }
            }
            // promotion, if necessary
            if (sparseProbabilisticStorage.size() > sparseThreshold) {
                initializeStorage(HLLType.FULL);
                for (IntByteCursor c : sparseProbabilisticStorage) {
                    final int registerIndex = c.key;
                    final byte registerValue = c.value;
                    probabilisticStorage.setMaxRegister(registerIndex, registerValue);
                }
                sparseProbabilisticStorage = null;
            }
            return;
        case FULL:
            for (int i = 0; i < m; i++) {
                final long registerValue = other.probabilisticStorage.getRegister(i);
                probabilisticStorage.setMaxRegister(i, registerValue);
            }
            return;
        default:
            throw new RuntimeException("Unsupported HLL type " + type);
    }
}
Also used : LongCursor(com.carrotsearch.hppc.cursors.LongCursor) IntByteCursor(com.carrotsearch.hppc.cursors.IntByteCursor)

Example 2 with IntByteCursor

use of com.carrotsearch.hppc.cursors.IntByteCursor in project lucene-solr by apache.

the class SparseHLLTest method randomValuesTest.

/**
     * Smoke tests the multisets by adding random values.
     */
@Test
public void randomValuesTest() {
    final int log2m = 11;
    final int regwidth = 5;
    final int sparseThreshold = 256;
    for (int run = 0; run < 100; run++) {
        final HLL hll = new HLL(log2m, regwidth, 128, /*explicitThreshold, arbitrary, unused*/
        sparseThreshold, HLLType.SPARSE);
        final IntByteHashMap map = new IntByteHashMap();
        for (int i = 0; i < sparseThreshold; i++) {
            final long rawValue = RandomizedTest.randomLong();
            final short registerIndex = ProbabilisticTestUtil.getRegisterIndex(rawValue, log2m);
            final byte registerValue = ProbabilisticTestUtil.getRegisterValue(rawValue, log2m);
            if (map.get(registerIndex) < registerValue) {
                map.put(registerIndex, registerValue);
            }
            hll.addRaw(rawValue);
        }
        for (IntByteCursor c : map) {
            final byte expectedRegisterValue = map.get(c.key);
            assertRegisterPresent(hll, c.key, expectedRegisterValue);
        }
    }
}
Also used : IntByteHashMap(com.carrotsearch.hppc.IntByteHashMap) IntByteCursor(com.carrotsearch.hppc.cursors.IntByteCursor) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 3 with IntByteCursor

use of com.carrotsearch.hppc.cursors.IntByteCursor in project lucene-solr by apache.

the class SparseHLLTest method assertElementsEqual.

/**
     * Asserts that all registers in the two {@link HLL} instances are identical.
     */
private static void assertElementsEqual(final HLL hllA, final HLL hllB) {
    final IntByteHashMap sparseProbabilisticStorageA = hllA.sparseProbabilisticStorage;
    final IntByteHashMap sparseProbabilisticStorageB = hllB.sparseProbabilisticStorage;
    assertEquals(sparseProbabilisticStorageA.size(), sparseProbabilisticStorageB.size());
    for (IntByteCursor c : sparseProbabilisticStorageA) {
        assertEquals(sparseProbabilisticStorageA.get(c.key), sparseProbabilisticStorageB.get(c.key));
    }
}
Also used : IntByteHashMap(com.carrotsearch.hppc.IntByteHashMap) IntByteCursor(com.carrotsearch.hppc.cursors.IntByteCursor)

Aggregations

IntByteCursor (com.carrotsearch.hppc.cursors.IntByteCursor)3 IntByteHashMap (com.carrotsearch.hppc.IntByteHashMap)2 LongCursor (com.carrotsearch.hppc.cursors.LongCursor)1 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)1 Test (org.junit.Test)1