struct
Pf::BitSet(I)
- Pf::BitSet(I)
- Struct
- Value
- Object
Overview
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) where we're O(1), even though both will end up constant-time
in effect, the former is still slower). I know that casting can be annoying,
but its better to cast than to investigate an invisible performance bug!
Defined in:
permafrost/bit_set.crConstructors
-
.new(bits : I)
Constructs a bit set from the underlying bits.
Class Method Summary
-
.[]
Alias of
.empty. -
.[](*values : I) : BitSet(I)
Constructs a bit set with the given values.
-
.empty : BitSet(I)
Constructs an empty bit set.
Instance Method Summary
-
#&(other : BitSet(I)) : BitSet(I)
Returns the intersection of this and other sets.
-
#+(other : BitSet(I)) : BitSet(I)
Alias of
#|. -
#-(other : BitSet(I)) : BitSet(I)
Returns the difference of this and other sets.
-
#^(other : BitSet(I)) : BitSet(I)
Returns the symmetric difference of this and other sets.
-
#|(other : BitSet(I)) : BitSet(I)
Returns the union of this and other sets.
-
#add(value : I) : BitSet(I)
Adds value to this set.
-
#bits : I
Returns the underlying bits.
-
#complement : BitSet(I)
Returns a set containing all values not in this set.
-
#delete(value : I) : BitSet(I)
Removes value from this set.
-
#each(& : I -> ) : Nil
Yields values in this set.
-
#each_with_index(& : I, Int32 -> ) : Nil
Yields values in this set along with their index (
0,1,2, etc.; do not confuse with bit index). -
#empty? : Bool
Returns
trueif this set has no values. -
#full? : Bool
Returns
trueif this set has no spare capacity. -
#gte(lo : I) : BitSet(I)
Returns a set of values less than lo in this set.
-
#includes?(value : I) : Bool
Returns
trueif value exists in this set. - #inspect(io)
-
#intersects?(other : BitSet(I)) : Bool
Returns
trueif this and other sets have one or more values in common. -
#lt(hi : I) : BitSet(I)
Returns a set of values less than hi in this set.
-
#mex : I
Returns the minimum excluded value of this set.
-
#proper_subset_of?(other : BitSet(I)) : Bool
Returns
trueif all elements of this set are also elements of a larger other set. -
#proper_superset_of?(other : BitSet(I)) : Bool
Returns
trueif this set includes all elements from a strictly smaller other set. -
#rank(value : I) : Int32
Returns the rank of value: the number of values smaller than it.
-
#size : Int32
Returns the number of values in this set.
-
#subset_of?(other : BitSet(I)) : Bool
Returns
trueif all elements of this set are also elements of the other set. -
#superset_of?(other : BitSet(I)) : Bool
Returns
trueif this set includes all elements from the other set. - #to_s(io)
Constructor Detail
Class Method Detail
Instance Method Detail
Returns the intersection of this and other sets.
Returns the symmetric difference of this and other sets.
Yields values in this set along with their index (0, 1, 2, etc.; do not
confuse with bit index).
Returns true if this and other sets have one or more values in common.
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 forBitSet64). Note how the mex itself is outside of the set.
Returns true if all elements of this set are also elements of
a larger other set.
Returns true if this set includes all elements from a strictly
smaller other set.
Returns true if all elements of this set are also elements of
the other set.
Returns true if this set includes all elements from the other set.