Class RspBitmap

All Implemented Interfaces:
OrderedLongSet
Direct Known Subclasses:
DisposableRspBitmap

public class RspBitmap extends RspArray<RspBitmap> implements OrderedLongSet
See header comment on RspArray for explanation on space partitioning.
  • Constructor Details

    • RspBitmap

      public RspBitmap()
    • RspBitmap

      public RspBitmap(long start, long end)
    • RspBitmap

      public RspBitmap(RspArray src, int startIdx, long startOffset, int endIdx, long endOffset)
    • RspBitmap

      public RspBitmap(RspArray src, int startIdx, int endIdx)
  • Method Details

    • makeEmpty

      public static RspBitmap makeEmpty()
    • makeSingleRange

      public static RspBitmap makeSingleRange(long start, long end)
    • makeFromBlockPieces

      public static RspBitmap makeFromBlockPieces(long[] blocks, int blockCount, int[] offsets, int[] pieces, long[] fullRuns, int fullRunCount)
      Make a bitmap from row key ranges already bucketed by block: a radix pass on the high bits has split every range into block-local pieces and grouped the pieces of each block together, and has collected the runs of blocks some range covers whole. The bitmap is built in one pass over the blocks in order, so the span array is laid out exactly once and no span is ever spliced or grown.

      Each partial block's pieces are reduced to runs: at most 64 pieces are sorted and coalesced in place, since clearing and scanning a block's bitmap would cost more than sorting that many ints; more are accumulated into a scratch bitmap of one block, whose runs are then read off. Either way pieces that abut or overlap coalesce whatever their source, and the block's container is built from the runs as whichever representation is smallest for its cardinality and run count: a run container, an array container, or a bitmap container; a single row becomes a singleton span, and a block that came out all ones joins the full block spans. Consecutive full blocks become one span.

      Parameters:
      blocks - Ascending block indices that received pieces; blocks[0, blockCount)
      blockCount - How many of blocks are used
      offsets - For blocks[k], its pieces are pieces[offsets[k], offsets[k + 1])
      pieces - Block-local pieces, each the low 16 bits of its first row in the high half of the int and of its last row in the low half
      fullRuns - Runs of block indices some range covers whole, as inclusive first, last pairs, ascending, with no two runs overlapping or touching; fullRuns[0, 2 * fullRunCount). A block in blocks that lies within a run is full whatever its pieces say.
      fullRunCount - How many runs fullRuns holds
    • makeSingle

      public static RspBitmap makeSingle(long v)
    • make

      protected final RspBitmap make(RspArray src, int startIdx, long startOffset, int endIdx, long endOffset)
      Specified by:
      make in class RspArray<RspBitmap>
    • make

      protected final RspBitmap make()
      Specified by:
      make in class RspArray<RspBitmap>
    • self

      protected RspBitmap self()
      Description copied from class: RefCountedCow
      Derived classes should implement self() by simply "return this" of the right type. This method exists only as an implementation artifact for a type safe implementation of the curiously recurring generic pattern.
      Specified by:
      self in class RefCountedCow<RspBitmap>
      Returns:
      this object, with the right, most derived type.
    • deepCopy

      public RspBitmap deepCopy()
      Description copied from class: RefCountedCow
      Get a deep copy of the current object, not shared with anybody. Note this is not thread safe.
      Specified by:
      deepCopy in class RefCountedCow<RspBitmap>
      Returns:
      A full, deep copy of this object with a reference count of 1 (not shared).
    • writeCheck

      public RspBitmap writeCheck()
    • first

      public long first()
    • last

      public long last()
    • addValuesUnsafe

      public RspBitmap addValuesUnsafe(LongChunk<OrderedRowKeys> values, int offset, int length)
    • addValuesUnsafeNoWriteCheck

      public void addValuesUnsafeNoWriteCheck(LongChunk<OrderedRowKeys> values, int offset, int length)
      Add length values from values, starting at offset, to this bitmap.

      Blocks we do not have yet are collected and inserted in one pass, rather than shifting our tail once per block. Spans marked for removal along the way are recorded by index, so the two are reconciled together at the end, and at any point in between where our arrays have to be settled.

    • add

      public RspBitmap add(long val)
    • addUnsafe

      public RspBitmap addUnsafe(long val)
    • addUnsafeNoWriteCheck

      public void addUnsafeNoWriteCheck(long val)
    • appendRangeUnsafeNoWriteCheck

      public void appendRangeUnsafeNoWriteCheck(long sHigh, long start, long end)
    • appendRange

      public RspBitmap appendRange(long start, long end)
    • appendRangeUnsafe

      public RspBitmap appendRangeUnsafe(long start, long end)
    • appendRangeUnsafeNoWriteCheck

      public void appendRangeUnsafeNoWriteCheck(long start, long end)
    • appendContainerUnsafeNoWriteCheck

      public void appendContainerUnsafeNoWriteCheck(long k, Container c)
    • appendFullBlockSpanUnsafeNoWriteCheck

      public void appendFullBlockSpanUnsafeNoWriteCheck(long k, long slen)
    • append

      public RspBitmap append(long v)
    • appendUnsafe

      public RspBitmap appendUnsafe(long v)
    • appendUnsafeNoWriteCheck

      public void appendUnsafeNoWriteCheck(long v)
    • containerForLowValueAndRange

      public static Container containerForLowValueAndRange(int val, int start, int end)
    • addRangeExclusiveEnd

      public RspBitmap addRangeExclusiveEnd(long start, long end)
    • addRange

      public RspBitmap addRange(long start, long end)
    • addRangeUnsafe

      public RspBitmap addRangeUnsafe(long start, long end)
    • addRangeUnsafeNoWriteCheck

      public void addRangeUnsafeNoWriteCheck(long first, long last)
    • addRangeUnsafeNoWriteCheck

      public int addRangeUnsafeNoWriteCheck(int fromIdx, long start, long end)
    • addRangesUnsafeNoWriteCheck

      public void addRangesUnsafeNoWriteCheck(RowSet.RangeIterator rit)
    • contains

      public boolean contains(long val)
    • remove

      public RspBitmap remove(long val)
    • removeUnsafe

      public RspBitmap removeUnsafe(long val)
    • removeUnsafeNoWriteCheck

      public RspBitmap removeUnsafeNoWriteCheck(long val)
    • removeUnsafeNoWriteCheck

      public void removeUnsafeNoWriteCheck(long val, long blockKey, int i)
    • removeRange

      public RspBitmap removeRange(long start, long end)
    • removeRangeUnsafe

      public RspBitmap removeRangeUnsafe(long start, long end)
    • or

      public static RspBitmap or(RspBitmap b1, RspBitmap b2)
      Return the logical or of two bitmaps as a new bitmap. This is equivalent to the union of the two bitmaps as sets. The arguments won't be modified.
      Parameters:
      b1 - a bitmap
      b2 - a bitmap
      Returns:
      b1 or b2 as a new bitmap.
    • orEquals

      public RspBitmap orEquals(RspBitmap other)
      Add every element on other to this bitmap.
    • orEqualsShifted

      public RspBitmap orEqualsShifted(long shiftAmount, RspBitmap other)
      For every key on other, add (key + shiftAmount) to this bitmap.
    • orEqualsUnsafe

      public RspBitmap orEqualsUnsafe(RspBitmap other)
      Add every element on other to this bitmap. Does not update cardinality cache. Caller must ensure finishMutations() is called before any operation depending on the cardinality cache being up to date are called.
    • orEqualsShiftedUnsafe

      public RspBitmap orEqualsShiftedUnsafe(long shiftAmount, RspBitmap other)
      For every key on other, add (key + shiftAmount) to this bitmap. Note shiftAmount is assumed to be a multiple of BLOCK_SIZE. Does not update cardinality cache. Caller must ensure finishMutations() is called before any operation depending on the cardinality cache being up to date are called.
    • appendShiftedUnsafeNoWriteCheck

      public void appendShiftedUnsafeNoWriteCheck(long shiftAmount, RspArray other)
    • and

      public static RspBitmap and(RspBitmap b1, RspBitmap b2)
      Return the logical and of two bitmaps as a new bitmap. This is equivalent to the intersection of the two bitmaps as sets.
      Parameters:
      b1 - a bitmap
      b2 - a bitmap
      Returns:
      b1 and b2 as a new bitmap.
    • andEquals

      public RspBitmap andEquals(RspBitmap other)
      Removes every element from this bitmap that is not in the other bitmap.
    • andEqualsUnsafe

      public RspBitmap andEqualsUnsafe(RspBitmap other)
    • andNotImpl

      public static RspBitmap andNotImpl(RspBitmap r1, RspBitmap r2)
      Return the logical result of r1 and not r2 as a new RspArray. The arguments won't be modified.
      Parameters:
      r1 - an RspArray
      r2 - an RspArray
      Returns:
      r1 and not r2 as a new RspArray.
    • andNot

      public static RspBitmap andNot(RspBitmap b1, RspBitmap b2)
      Return the logical result of r1 and not r2 as a new bitmap. This is equivalent to removing every element in b2 from b1. The arguments won't be modified.
      Parameters:
      b1 - a bitmap
      b2 - a bitmap
      Returns:
      b1 and not b2 as a new bitmap.
    • update

      public RspBitmap update(RspBitmap added, RspBitmap removed)
      Updates the bitmap by adding and removing the bitmaps given as parameter.
      Parameters:
      added - Elements to add. Assumed disjoint with removed.
      removed - Elements to remove. Assumed disjoint with added.
    • updateUnsafe

      public RspBitmap updateUnsafe(RspBitmap added, RspBitmap removed)
    • updateUnsafeNoWriteCheck

      public void updateUnsafeNoWriteCheck(RspBitmap added, RspBitmap removed)
    • andNotEquals

      public RspBitmap andNotEquals(RspBitmap other)
    • andNotEqualsUnsafe

      public RspBitmap andNotEqualsUnsafe(RspBitmap other)
      Remove every element in other from this bitmap.
    • applyOffset

      public RspBitmap applyOffset(long offset)
      Apply an offset to every value in this bitmap, mutating it.
      Parameters:
      offset - The offset to apply.
    • applyOffsetNoWriteCheck

      public RspBitmap applyOffsetNoWriteCheck(long offset)
    • applyOffsetOnNew

      public RspBitmap applyOffsetOnNew(long offset)
      Apply an offset to every value in this bitmap, returning a new bitmap (original is not changed).
      Parameters:
      offset - The offset to apply.
    • applyOffsetImpl

      public RspBitmap applyOffsetImpl(long offset, Supplier<RspBitmap> onZeroOffset, Supplier<RspBitmap> onAlignedOffset)
    • subrangeByPos

      public RspBitmap subrangeByPos(long firstPos, long lastPos, boolean returnNullIfEmptyResult)
    • subrangeByPos

      public RspBitmap subrangeByPos(long firstPos, long lastPos)
    • subrangeByValue

      public RspBitmap subrangeByValue(long start, long end, boolean returnNullIfEmptyResult)
    • subrangeByValue

      public RspBitmap subrangeByValue(long start, long end)
    • invert

      public void invert(LongRangeConsumer builder, RowSet.RangeIterator it, long maxPos)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • finishMutations

      public void finishMutations()
    • finishMutationsAndOptimize

      public void finishMutationsAndOptimize()
    • ixCowRef

      public RspBitmap ixCowRef()
      Specified by:
      ixCowRef in interface OrderedLongSet
    • ixInsert

      public RspBitmap ixInsert(long key)
      Specified by:
      ixInsert in interface OrderedLongSet
    • ixRelease

      public void ixRelease()
      Specified by:
      ixRelease in interface OrderedLongSet
    • ixRefCount

      public int ixRefCount()
      Specified by:
      ixRefCount in interface OrderedLongSet
      Returns:
      The number of references outstanding to this set; a set with more than one copies itself before it can be mutated
    • ixEntryCount

      public int ixEntryCount()
      Description copied from interface: OrderedLongSet
      A measure of what a traversal of this OrderedLongSet costs, which is different for each implementation. This is used, for example, to determine if a.insert(b) or b.insert(a) is less expensive to compute. The unit is not identical across types: spans for an RspBitmap, positions in the packed array for a SortedRanges, or simply one for a SingleRange.
      Specified by:
      ixEntryCount in interface OrderedLongSet
      Returns:
      The number of entries stored, zero when the set is empty
    • ixInsertRange

      public RspBitmap ixInsertRange(long startKey, long endKey)
      Specified by:
      ixInsertRange in interface OrderedLongSet
    • ixInsertSecondHalf

      public final OrderedLongSet ixInsertSecondHalf(LongChunk<OrderedRowKeys> values, int offset, int length)
      Specified by:
      ixInsertSecondHalf in interface OrderedLongSet
    • ixRemoveSecondHalf

      public final OrderedLongSet ixRemoveSecondHalf(LongChunk<OrderedRowKeys> values, int offset, int length)
      Specified by:
      ixRemoveSecondHalf in interface OrderedLongSet
    • ixAppendRange

      public RspBitmap ixAppendRange(long startKey, long endKey)
      Specified by:
      ixAppendRange in interface OrderedLongSet
    • ixRemove

      public RspBitmap ixRemove(long key)
      Specified by:
      ixRemove in interface OrderedLongSet
    • ixLastKey

      public long ixLastKey()
      Specified by:
      ixLastKey in interface OrderedLongSet
    • ixFirstKey

      public long ixFirstKey()
      Specified by:
      ixFirstKey in interface OrderedLongSet
    • ixGet

      public long ixGet(long pos)
      Specified by:
      ixGet in interface OrderedLongSet
    • ixGetKeysForPositions

      public void ixGetKeysForPositions(PrimitiveIterator.OfLong inputPositions, LongConsumer outputKeys)
      Specified by:
      ixGetKeysForPositions in interface OrderedLongSet
    • ixFind

      public long ixFind(long key)
      Specified by:
      ixFind in interface OrderedLongSet
    • ixCardinality

      public long ixCardinality()
      Specified by:
      ixCardinality in interface OrderedLongSet
    • ixIsEmpty

      public boolean ixIsEmpty()
      Specified by:
      ixIsEmpty in interface OrderedLongSet
    • ixInvertOnNew

      public OrderedLongSet ixInvertOnNew(OrderedLongSet keys, long maximumPosition)
      Description copied from interface: OrderedLongSet
      Invert the given OrderedLongSet.
      Specified by:
      ixInvertOnNew in interface OrderedLongSet
      Parameters:
      keys - OrderedLongSet of keys to invert
      maximumPosition - the largest position to add to indexBuilder, inclusive
      Returns:
      the inverse of keys
    • ixForEachLong

      public boolean ixForEachLong(LongAbortableConsumer lc)
      Specified by:
      ixForEachLong in interface OrderedLongSet
    • ixForEachLongRange

      public boolean ixForEachLongRange(LongRangeAbortableConsumer lc)
      Specified by:
      ixForEachLongRange in interface OrderedLongSet
    • ixSubindexByPosOnNew

      public OrderedLongSet ixSubindexByPosOnNew(long startPos, long endPosExclusive)
      Specified by:
      ixSubindexByPosOnNew in interface OrderedLongSet
    • ixSubindexByKeyOnNew

      public OrderedLongSet ixSubindexByKeyOnNew(long startKey, long endKey)
      Specified by:
      ixSubindexByKeyOnNew in interface OrderedLongSet
    • ixUpdate

      public OrderedLongSet ixUpdate(OrderedLongSet added, OrderedLongSet removed)
      Specified by:
      ixUpdate in interface OrderedLongSet
    • ixUpdateNoWriteCheck

      public OrderedLongSet ixUpdateNoWriteCheck(OrderedLongSet added, OrderedLongSet removed)
    • ixInsert

      public RspBitmap ixInsert(OrderedLongSet other)
      Specified by:
      ixInsert in interface OrderedLongSet
    • ixInsertNoWriteCheck

      public RspBitmap ixInsertNoWriteCheck(OrderedLongSet other)
    • insertOrderedLongSetUnsafeNoWriteCheck

      public void insertOrderedLongSetUnsafeNoWriteCheck(SingleRange ix)
    • insertOrderedLongSetUnsafeNoWriteCheck

      public void insertOrderedLongSetUnsafeNoWriteCheck(SortedRanges sr)
    • insertOrderedLongSetUnsafeNoWriteCheck

      public void insertOrderedLongSetUnsafeNoWriteCheck(RspBitmap rb)
    • ixRemove

      public OrderedLongSet ixRemove(OrderedLongSet other)
      Specified by:
      ixRemove in interface OrderedLongSet
    • ixRemoveNoWriteCheck

      public OrderedLongSet ixRemoveNoWriteCheck(OrderedLongSet other)
    • ixRetain

      public OrderedLongSet ixRetain(OrderedLongSet other)
      Specified by:
      ixRetain in interface OrderedLongSet
    • ixRetainNoWriteCheck

      public OrderedLongSet ixRetainNoWriteCheck(OrderedLongSet other)
    • ixRetainRange

      public OrderedLongSet ixRetainRange(long start, long end)
      Specified by:
      ixRetainRange in interface OrderedLongSet
    • ixRetainRangeNoWriteCheck

      public OrderedLongSet ixRetainRangeNoWriteCheck(long start, long end)
    • ixRemoveRange

      public OrderedLongSet ixRemoveRange(long startKey, long endKey)
      Specified by:
      ixRemoveRange in interface OrderedLongSet
    • ixIntersectOnNew

      public OrderedLongSet ixIntersectOnNew(OrderedLongSet other)
      Specified by:
      ixIntersectOnNew in interface OrderedLongSet
    • ixContainsRange

      public boolean ixContainsRange(long start, long end)
      Specified by:
      ixContainsRange in interface OrderedLongSet
    • ixOverlaps

      public boolean ixOverlaps(OrderedLongSet other)
      Specified by:
      ixOverlaps in interface OrderedLongSet
    • ixOverlapsRange

      public boolean ixOverlapsRange(long start, long end)
      Specified by:
      ixOverlapsRange in interface OrderedLongSet
    • subsetOf

      public boolean subsetOf(SortedRanges sr)
    • ixSubsetOf

      public boolean ixSubsetOf(OrderedLongSet other)
      Specified by:
      ixSubsetOf in interface OrderedLongSet
    • ixMinusOnNew

      public OrderedLongSet ixMinusOnNew(OrderedLongSet other)
      Specified by:
      ixMinusOnNew in interface OrderedLongSet
    • ixUnionOnNew

      public OrderedLongSet ixUnionOnNew(OrderedLongSet other)
      Specified by:
      ixUnionOnNew in interface OrderedLongSet
    • ixShiftOnNew

      public RspBitmap ixShiftOnNew(long shiftAmount)
      Specified by:
      ixShiftOnNew in interface OrderedLongSet
    • ixShiftInPlace

      public RspBitmap ixShiftInPlace(long shiftAmount)
      Specified by:
      ixShiftInPlace in interface OrderedLongSet
    • ixInsertWithShift

      public OrderedLongSet ixInsertWithShift(long shiftAmount, SortedRanges sr)
    • ixInsertWithShift

      public OrderedLongSet ixInsertWithShift(long shiftAmount, OrderedLongSet other)
      Specified by:
      ixInsertWithShift in interface OrderedLongSet
    • ixSearchIterator

      public RowSet.SearchIterator ixSearchIterator()
      Specified by:
      ixSearchIterator in interface OrderedLongSet
    • ixIterator

      public RowSet.Iterator ixIterator()
      Specified by:
      ixIterator in interface OrderedLongSet
    • ixReverseIterator

      public RowSet.SearchIterator ixReverseIterator()
      Specified by:
      ixReverseIterator in interface OrderedLongSet
    • ixRangeIterator

      public RowSet.RangeIterator ixRangeIterator()
      Specified by:
      ixRangeIterator in interface OrderedLongSet
    • ixCompact

      public OrderedLongSet ixCompact()
      Specified by:
      ixCompact in interface OrderedLongSet
    • ixValidate

      public void ixValidate(String failMsg)
      Specified by:
      ixValidate in interface OrderedLongSet
    • ixGetRowSequenceByPosition

      public RowSequence ixGetRowSequenceByPosition(long startPositionInclusive, long length)
      Specified by:
      ixGetRowSequenceByPosition in interface OrderedLongSet
    • ixGetRowSequenceByKeyRange

      public RowSequence ixGetRowSequenceByKeyRange(long startKeyInclusive, long endKeyInclusive)
      Specified by:
      ixGetRowSequenceByKeyRange in interface OrderedLongSet
    • ixGetRowSequenceIterator

      public RowSequence.Iterator ixGetRowSequenceIterator()
      Specified by:
      ixGetRowSequenceIterator in interface OrderedLongSet
    • ixRangesCountUpperBound

      public long ixRangesCountUpperBound()
      Specified by:
      ixRangesCountUpperBound in interface OrderedLongSet
    • ixGetAverageRunLengthEstimate

      public long ixGetAverageRunLengthEstimate()
      Specified by:
      ixGetAverageRunLengthEstimate in interface OrderedLongSet
    • ixToRspOnNew

      public RspBitmap ixToRspOnNew()
      Specified by:
      ixToRspOnNew in interface OrderedLongSet
    • toString

      public String toString()
      Overrides:
      toString in class RspArray<RspBitmap>