Search in sources :

Example 1 with BitmapDataProvider

use of org.roaringbitmap.BitmapDataProvider in project RoaringBitmap by RoaringBitmap.

the class Roaring64NavigableMap method flip.

/**
 * Add the value if it is not already present, otherwise remove it.
 *
 * @param x long value
 */
public void flip(final long x) {
    int high = RoaringIntPacking.high(x);
    BitmapDataProvider lowBitmap = highToBitmap.get(high);
    if (lowBitmap == null) {
        // The value is not added: add it without any flip specific code
        addLong(x);
    } else {
        int low = RoaringIntPacking.low(x);
        // TODO Is it relevant to calling .flip with a cast?
        if (lowBitmap instanceof RoaringBitmap) {
            ((RoaringBitmap) lowBitmap).flip(low);
        } else if (lowBitmap instanceof MutableRoaringBitmap) {
            ((MutableRoaringBitmap) lowBitmap).flip(low);
        } else {
            // Fallback to a manual flip
            if (lowBitmap.contains(low)) {
                lowBitmap.remove(low);
            } else {
                lowBitmap.add(low);
            }
        }
    }
    invalidateAboveHigh(high);
}
Also used : MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap) BitmapDataProvider(org.roaringbitmap.BitmapDataProvider) RoaringBitmap(org.roaringbitmap.RoaringBitmap) MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap)

Example 2 with BitmapDataProvider

use of org.roaringbitmap.BitmapDataProvider in project RoaringBitmap by RoaringBitmap.

the class Roaring64NavigableMap method rankLong.

@Override
public long rankLong(long id) {
    int high = RoaringIntPacking.high(id);
    int low = RoaringIntPacking.low(id);
    if (!doCacheCardinalities) {
        return rankLongNoCache(high, low);
    }
    int indexOk = ensureCumulatives(high);
    int highPosition = binarySearch(sortedHighs, 0, indexOk, high);
    if (highPosition >= 0) {
        // There is a bucket holding this item
        final long previousBucketCardinality;
        if (highPosition == 0) {
            previousBucketCardinality = 0;
        } else {
            previousBucketCardinality = sortedCumulatedCardinality[highPosition - 1];
        }
        BitmapDataProvider lowBitmap = highToBitmap.get(sortedHighs[highPosition]);
        // Rank is previous cardinality plus rank in current bitmap
        return previousBucketCardinality + lowBitmap.rankLong(low);
    } else {
        // There is no bucket holding this item: insertionPoint is previous bitmap
        int insertionPoint = -highPosition - 1;
        if (insertionPoint == 0) {
            // this key is before all inserted keys
            return 0;
        } else {
            // The rank is the cardinality of this previous bitmap
            return sortedCumulatedCardinality[insertionPoint - 1];
        }
    }
}
Also used : BitmapDataProvider(org.roaringbitmap.BitmapDataProvider)

Example 3 with BitmapDataProvider

use of org.roaringbitmap.BitmapDataProvider in project RoaringBitmap by RoaringBitmap.

the class Roaring64NavigableMap method contains.

@Override
public boolean contains(long x) {
    int high = RoaringIntPacking.high(x);
    BitmapDataProvider lowBitmap = highToBitmap.get(high);
    if (lowBitmap == null) {
        return false;
    }
    int low = RoaringIntPacking.low(x);
    return lowBitmap.contains(low);
}
Also used : BitmapDataProvider(org.roaringbitmap.BitmapDataProvider)

Example 4 with BitmapDataProvider

use of org.roaringbitmap.BitmapDataProvider in project RoaringBitmap by RoaringBitmap.

the class Roaring64NavigableMap method add.

/**
 * Add to the current bitmap all longs in [rangeStart,rangeEnd).
 *
 * @param rangeStart inclusive beginning of range
 * @param rangeEnd exclusive ending of range
 */
public void add(final long rangeStart, final long rangeEnd) {
    int startHigh = high(rangeStart);
    int startLow = low(rangeStart);
    int endHigh = high(rangeEnd);
    int endLow = low(rangeEnd);
    for (int high = startHigh; high <= endHigh; high++) {
        final int currentStartLow;
        if (startHigh == high) {
            // The whole range starts in this bucket
            currentStartLow = startLow;
        } else {
            // Add the bucket from the beginning
            currentStartLow = 0;
        }
        long startLowAsLong = Util.toUnsignedLong(currentStartLow);
        final long endLowAsLong;
        if (endHigh == high) {
            // The whole range ends in this bucket
            endLowAsLong = Util.toUnsignedLong(endLow);
        } else {
            // Add the bucket until the end: we have a +1 as, in RoaringBitmap.add(long,long), the end
            // is excluded
            endLowAsLong = Util.toUnsignedLong(-1) + 1;
        }
        if (endLowAsLong > startLowAsLong) {
            // Initialize the bitmap only if there is access data to write
            BitmapDataProvider bitmap = highToBitmap.get(high);
            if (bitmap == null) {
                bitmap = new MutableRoaringBitmap();
                pushBitmapForHigh(high, bitmap);
            }
            if (bitmap instanceof RoaringBitmap) {
                ((RoaringBitmap) bitmap).add(startLowAsLong, endLowAsLong);
            } else if (bitmap instanceof MutableRoaringBitmap) {
                ((MutableRoaringBitmap) bitmap).add(startLowAsLong, endLowAsLong);
            } else {
                throw new UnsupportedOperationException("TODO. Not for " + bitmap.getClass());
            }
        }
    }
    invalidateAboveHigh(startHigh);
}
Also used : MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap) BitmapDataProvider(org.roaringbitmap.BitmapDataProvider) RoaringBitmap(org.roaringbitmap.RoaringBitmap) MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap)

Example 5 with BitmapDataProvider

use of org.roaringbitmap.BitmapDataProvider in project RoaringBitmap by RoaringBitmap.

the class Roaring64NavigableMap method removeLong.

@Override
public void removeLong(long x) {
    int high = high(x);
    BitmapDataProvider bitmap = highToBitmap.get(high);
    if (bitmap != null) {
        int low = low(x);
        bitmap.remove(low);
        // Invalidate only if actually modified
        invalidateAboveHigh(high);
    }
}
Also used : BitmapDataProvider(org.roaringbitmap.BitmapDataProvider)

Aggregations

BitmapDataProvider (org.roaringbitmap.BitmapDataProvider)13 RoaringBitmap (org.roaringbitmap.RoaringBitmap)6 MutableRoaringBitmap (org.roaringbitmap.buffer.MutableRoaringBitmap)6 Entry (java.util.Map.Entry)2 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1