Return the intersection of two bitsets as a new immutable bitset.
Return the intersection of two bitsets as a new immutable bitset.
The resulting bitset will only contain a value if that value is present in both input bitsets.
Return a bitset that contains n
and whose other values are identical to this one's.
Return a bitset that contains n
and whose other values are identical to this one's. If this bitset
already contains n
then this method does nothing.
Return a bitset that does not contain n
and whose other values are identical to this one's.
Return a bitset that does not contain n
and whose other values are identical to this one's. If this
bitset does not contain n
then this method does nothing.
Return this bitset minus the bits contained in the other bitset as a new immutable bitset.
Return this bitset minus the bits contained in the other bitset as a new immutable bitset.
The resulting bitset will contain exactly those values which do appear in the left-hand side but do not appear in the right-hand side.
If the bitsets do not intersect, the left-hand side will be returned.
Return the exclusive-or of two bitsets as a new immutable bitset.
Look for a particular value in the bitset.
Look for a particular value in the bitset.
Returns whether this value's bit is set.
Returns whether the two bitsets intersect or not.
Returns whether the two bitsets intersect or not.
Equivalent to (x & y).nonEmpty but faster.
Returns false if this bitset contains values, true otherwise.
Iterate across all values in the bitset.
Iterate across all values in the bitset.
Values in the iterator will be seen in "unsigned order" (e.g. if present, -1 will always come last). Here's an abbreviated view of this order in practice:
0, 1, 2, ... 2147483646, 2147483647, -2147483648, -2147483647, ... -1
(This "unsigned order" is identical to the tree's internal order.)
Iterate across all values in the bitset in reverse order.
Iterate across all values in the bitset in reverse order.
The order here is exactly the reverse of .iterator
.
Returns the number of distinct values in this bitset.
Returns the number of distinct values in this bitset.
For branches, this method will return the sum of the sizes of all its subtrees. For leaves it returns the number of bits set in the leaf (i.e. the number of values the leaf contains).
Return the union of two bitsets as a new immutable bitset.
Return the union of two bitsets as a new immutable bitset.
If either bitset contains a given value, the resulting bitset will also contain it.
Return a compacted bitset containing the same values as this one.
Return a compacted bitset containing the same values as this one.
This method is used to prune out "empty" branches that don't contain values. By default, bitset does not try to remove empty leaves when removing values (since repeatedly checking for this across many deletions would be expensive).
The bitset returned will have the same values as the current bitset, but is guaranteed not to contain any empty branches. Empty branches are not usually observable but would result in increased memory usage.
Universal equality.
Universal equality.
This method will only return true if the right argument is also a BitSet
. It does not attempt to coerce
either argument in any way (unlike Scala collections, for example).
Two bitsets can be equal even if they have different underlying tree structure. (For example, one bitset's tree may have empty branches that the other lacks.)
Universal hash code.
Universal hash code.
Bitsets that are the equal will hash to the same value. As in equals
, the values present determine the
hash code, as opposed to the tree structure.
Returns true if this bitset contains values, false otherwise.
Present a view of this bitset as a scala.Set[Int]
.
Present a view of this bitset as a scala.Set[Int]
.
This is provided for compatibility with Scala collections. Many of the set operations are implemented in
terms of BitSet
, but other operations (for example map
) may copy these values into a different Set
implementation.
Produce a string representation of this BitSet.
Produce a string representation of this BitSet.
This representation will contain all the values in the bitset. For large bitsets, this operation may be very expensive.
A fast, immutable BitSet.
This implementation is taken from cats-collections. https://github.com/typelevel/cats-collections/blob/0336992942aba9aba4a322b629447fcabe251920/core/src/main/scala/cats/collections/BitSet.scala
A Bitset is a specialized type of set that tracks the
Int
values it contains: for each integer value, a BitSet uses a single bit to track whether the value is present (1) or absent (0). Bitsets are often sparse, since "missing" bits can be assumed to be zero.Unlike scala's default immutable this BitSet does not do a full copy on each added value.
Internally the implementation is a tree. Each leaf uses an Array[Long] value to hold up to 2048 bits, and each branch uses an Array[BitSet] to hold up to 32 subtrees (null subtrees are treated as empty).
Bitset treats the values it stores as 32-bit unsigned values, which is relevant to the internal addressing methods as well as the order used by
iterator
.The benchmarks suggest this bitset is MUCH faster than Scala's built-in bitset for cases where you may need many modifications and merges, (for example in a BloomFilter).