Example usage for org.apache.lucene.util LongBitSet cardinality

List of usage examples for org.apache.lucene.util LongBitSet cardinality

Introduction

In this page you can find the example usage for org.apache.lucene.util LongBitSet cardinality.

Prototype

public long cardinality() 

Source Link

Document

Returns number of set bits.

Usage

From source file:org.apache.solr.legacy.TestLegacyNumericUtils.java

License:Apache License

/** Note: The neededBounds Iterable must be unsigned (easier understanding what's happening) */
private void assertLongRangeSplit(final long lower, final long upper, int precisionStep,
        final boolean useBitSet, final Iterable<Long> expectedBounds, final Iterable<Integer> expectedShifts) {
    // Cannot use FixedBitSet since the range could be long:
    final LongBitSet bits = useBitSet ? new LongBitSet(upper - lower + 1) : null;
    final Iterator<Long> neededBounds = (expectedBounds == null) ? null : expectedBounds.iterator();
    final Iterator<Integer> neededShifts = (expectedShifts == null) ? null : expectedShifts.iterator();

    LegacyNumericUtils.splitLongRange(new LegacyNumericUtils.LongRangeBuilder() {
        @Override/* w  ww. j  a  v  a2 s . c  o  m*/
        public void addRange(long min, long max, int shift) {
            assertTrue("min, max should be inside bounds",
                    min >= lower && min <= upper && max >= lower && max <= upper);
            if (useBitSet)
                for (long l = min; l <= max; l++) {
                    assertFalse("ranges should not overlap", bits.getAndSet(l - lower));
                    // extra exit condition to prevent overflow on MAX_VALUE
                    if (l == max)
                        break;
                }
            if (neededBounds == null || neededShifts == null)
                return;
            // make unsigned longs for easier display and understanding
            min ^= 0x8000000000000000L;
            max ^= 0x8000000000000000L;
            //System.out.println("0x"+Long.toHexString(min>>>shift)+"L,0x"+Long.toHexString(max>>>shift)+"L)/*shift="+shift+"*/,");
            assertEquals("shift", neededShifts.next().intValue(), shift);
            assertEquals("inner min bound", neededBounds.next().longValue(), min >>> shift);
            assertEquals("inner max bound", neededBounds.next().longValue(), max >>> shift);
        }
    }, precisionStep, lower, upper);

    if (useBitSet) {
        // after flipping all bits in the range, the cardinality should be zero
        bits.flip(0, upper - lower + 1);
        assertEquals("The sub-range concenated should match the whole range", 0, bits.cardinality());
    }
}