struct

Pf::BitSet(I)

Inherits Struct / Value / Object

An immutable set of integers backed by a fixed-width integer of type I.

For BitSet(I), the maximum value is bit_width(I) - 1. For example, for BitSet8, you can store values 0-7, and for BitSet128, 0-127.

I must be one of UInt8..UInt128. You are advised however to use one of the aliases BitSet8..BitSet128 instead of dealing with this struct directly.

NOTE: For consistency, most methods accept and return I. This may require a few casts here and there on your end. A notable exception is size: almost everything in Crystal expects its return result to be an Int32, so we cast it on our end. For similar reasons, we do not include Enumerable and/or Indexable here: its methods (such as includes?), which are less constrained, will conflict with our policy of accepting I only, resulting in spooky suboptimal performance (Enumerable's includes? is O(N) whereas we're O(1); even though both will end up constant-time in effect, the former is still slower). Instead, we provide ix, which gives you an Indexable over the values in the set.

Constructors

new(bits : I)

Constructs a bit set from the underlying bits.

Source

Class methods

[]

Alias of empty.

Source
[](*values : I) : BitSet(I)

Constructs a bit set with the given values.

Source
empty

Constructs an empty bit set.

Source

Instance methods

&(other : BitSet(I)) : BitSet(I)

Returns the intersection of this and other sets.

Source
+(other : BitSet(I)) : BitSet(I)

Alias of |.

Source
-(other : BitSet(I)) : BitSet(I)

Returns the difference of this and other sets.

Source
^(other : BitSet(I)) : BitSet(I)

Returns the symmetric difference of this and other sets.

Source
|(other : BitSet(I)) : BitSet(I)

Returns the union of this and other sets.

Source
add(value : I) : BitSet(I)

Adds value to this set.

Source
bits

Returns the underlying bits.

Source
complement

Returns a set containing all values not in this set.

Source
delete(value : I) : BitSet(I)

Removes value from this set.

Source
each

Yields values in this set.

Source
each_with_index

Yields values in this set along with their index (0, 1, 2, etc.; do not confuse with bit index).

Source
empty?

Returns true if this set has no values.

Source
full?

Returns true if this set has no spare capacity.

Source
gte(lo : I) : BitSet(I)

Returns a set of values less than lo in this set.

Source
includes?(value : I) : Bool

Returns true if value exists in this set.

Source
inspect(io)
Source
intersects?(other : BitSet(I)) : Bool

Returns true if this and other sets have one or more values in common.

Source
lt(hi : I) : BitSet(I)

Returns a set of values less than hi in this set.

Source
mex

Returns the minimum excluded value of this set.

  • Mex of an empty set is 0
  • Mex of a full set is the bit width of I (e.g. 64 for BitSet64). Note how the mex itself is outside of the set.
Source
proper_subset_of?(other : BitSet(I)) : Bool

Returns true if all elements of this set are also elements of a larger other set.

Source
proper_superset_of?(other : BitSet(I)) : Bool

Returns true if this set includes all elements from a strictly smaller other set.

Source
rank(value : I) : Int32

Returns the rank of value: the number of values smaller than it.

Source
select(n : I) : I

Returns the nth value in this set. Raises IndexError if n is out of bounds.

Source
select?(n : I) : I | Nil

Returns the nth value in this set. Returns nil if n is out of bounds.

Source
size

Returns the number of values in this set.

Source
subset_of?(other : BitSet(I)) : Bool

Returns true if all elements of this set are also elements of the other set.

Source
superset_of?(other : BitSet(I)) : Bool

Returns true if this set includes all elements from the other set.

Source
to_s(io)
Source