package batteries
Install
Dune Dependency
Authors
Maintainers
Sources
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb
doc/batteries.unthreaded/BatBitSet/index.html
Module BatBitSet
Source
Efficient bit sets.
A bitset is an array of boolean values that can be accessed with indexes like an array but provides a better memory usage (divided by Sys.word_size; either 32 or 64) for a very small speed trade-off. It can provide efficient storage of dense sets of nonnegative integers near zero. Sparse sets should use BatSet
, sets with large ranges of contiguous ints should use BatISet
.
Create an empty bitset of capacity 0, the bitset will automatically expand when needed.
Example: BitSet.empty ()
Create an empty bitset with at least an initial capacity (in number of bits).
Example: BitSet.create 0 = BitSet.empty ()
Create a full bitset with at least initial capacity (in number of bits). All the bit under the defined capacity will be set.
Example: BitSet.count (BitSet.create_full n) = n
Copy a bitset : further modifications of first one will not affect the copy.
Example: let a = Bitset.create 8 in let b = BitSet.copy a in BitSet.set a 6; BitSet.mem a 6 && not (BitSet.mem b 6)
mem s n
returns true if nth-bit in the bitset s
is set, or false otherwise.
Example: let a = BitSet.create_full 256 in not (BitSet.mem a 300)
count s
returns the number of bits set in the bitset s
. Also known as Population Count, or cardinal
for sets.
Example: BitSet.count (BitSet.of_list [6;4;2;2;1]) = 4
next_set_bit s n
returns Some m
when m
is the next set element with index greater than or equal n
, or None if no such element exists (i.e. n
is greater than the largest element)
More efficient than scanning with repeated BitSet.mem
.
In-place Update
These functions modify an existing bitset.
differentiate_sym s t
sets s
to the symmetrical difference of the sets s
and t
.
Return new bitset
These functions return a new bitset that shares nothing with the input bitset. This is not as efficient as the in-place update.
Boilerplate code
enum s
returns an enumeration of bits which are set in the bitset s
.
of_enum ~cap e
builds a bitset of capacity cap
an enumeration of ints e
.
Note: Performance of this function may be poor if enumeration is in increasing order and the max.
compare s1 s2
compares two bitsets using a lexicographic ordering. Highest bit indexes are compared first. The capacity of the bitsets is not important for this comparison, only the bits starting with the highest set bit and going down.
equal s1 s2
returns true if, and only if, all bits values in s1 are the same as in s2.
ord s1 s2
returns BatOrd.Lt
, BatOrd.Eq
or BatOrd.Gt
if compare s1 s2
is, respectively, < 0
, 0
or > 0
.