struct Pf::BitSet(I)

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.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(bits : I) #

Constructs a bit set from the underlying bits.


[View source]

Class Method Detail

def self.[] #

Alias of .empty.


[View source]
def self.[](*values : I) : BitSet(I) #

Constructs a bit set with the given values.


[View source]
def self.empty : BitSet(I) #

Constructs an empty bit set.


[View source]

Instance Method Detail

def &(other : BitSet(I)) : BitSet(I) #

Returns the intersection of this and other sets.


[View source]
def +(other : BitSet(I)) : BitSet(I) #

Alias of #|.


[View source]
def -(other : BitSet(I)) : BitSet(I) #

Returns the difference of this and other sets.


[View source]
def ^(other : BitSet(I)) : BitSet(I) #

Returns the symmetric difference of this and other sets.


[View source]
def |(other : BitSet(I)) : BitSet(I) #

Returns the union of this and other sets.


[View source]
def add(value : I) : BitSet(I) #

Adds value to this set.


[View source]
def bits : I #

Returns the underlying bits.


[View source]
def complement : BitSet(I) #

Returns a set containing all values not in this set.


[View source]
def delete(value : I) : BitSet(I) #

Removes value from this set.


[View source]
def each(& : I -> ) : Nil #

Yields values in this set.


[View source]
def 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).


[View source]
def empty? : Bool #

Returns true if this set has no values.


[View source]
def full? : Bool #

Returns true if this set has no spare capacity.


[View source]
def gte(lo : I) : BitSet(I) #

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


[View source]
def includes?(value : I) : Bool #

Returns true if value exists in this set.


[View source]
def inspect(io) #

[View source]
def intersects?(other : BitSet(I)) : Bool #

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


[View source]
def lt(hi : I) : BitSet(I) #

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


[View source]
def mex : I #

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.

[View source]
def proper_subset_of?(other : BitSet(I)) : Bool #

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


[View source]
def proper_superset_of?(other : BitSet(I)) : Bool #

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


[View source]
def rank(value : I) : Int32 #

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


[View source]
def size : Int32 #

Returns the number of values in this set.


[View source]
def subset_of?(other : BitSet(I)) : Bool #

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


[View source]
def superset_of?(other : BitSet(I)) : Bool #

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


[View source]
def to_s(io) #

[View source]