func Base64StdEncoding()
Base64StdEncoding Marshal/Unmarshal BitSet with base64.StdEncoding(Default: base64.URLEncoding)
func Cap() uint
Cap returns the total possible capacity, or number of bits
func LittleEndian()
LittleEndian Marshal/Unmarshal Binary as Little Endian(Default: binary.BigEndian)
A BitSet is a set of bits. The zero value of a BitSet is an empty set of length 0.
type BitSet struct {
// contains filtered or unexported fields
}
func From(buf []uint64) *BitSet
From is a constructor used to create a BitSet from an array of integers
func New(length uint) (bset *BitSet)
New creates a new BitSet with a hint that length bits will be required
func (b *BitSet) All() bool
All returns true if all bits are set, false otherwise. Returns true for empty sets.
func (b *BitSet) Any() bool
Any returns true if any bit is set, false otherwise
func (b *BitSet) BinaryStorageSize() int
BinaryStorageSize returns the binary storage requirements
func (b *BitSet) Bytes() []uint64
Bytes returns the bitset as array of integers
func (b *BitSet) Clear(i uint) *BitSet
Clear bit i to 0
func (b *BitSet) ClearAll() *BitSet
ClearAll clears the entire BitSet
func (b *BitSet) Clone() *BitSet
Clone this BitSet
func (b *BitSet) Compact() *BitSet
Compact shrinks BitSet to so that we preserve all set bits, while minimizing memory usage. Compact calls Shrink.
func (b *BitSet) Complement() (result *BitSet)
Complement computes the (local) complement of a biset (up to length bits)
func (b *BitSet) Copy(c *BitSet) (count uint)
Copy into a destination BitSet Returning the size of the destination BitSet like array copy
func (b *BitSet) Count() uint
Count (number of set bits). Also known as "popcount" or "population count".
func (b *BitSet) DeleteAt(i uint) *BitSet
DeleteAt deletes the bit at the given index position from within the bitset All the bits residing on the left of the deleted bit get shifted right by 1 The running time of this operation may potentially be relatively slow, O(length)
func (b *BitSet) Difference(compare *BitSet) (result *BitSet)
Difference of base set and other set This is the BitSet equivalent of &^ (and not)
func (b *BitSet) DifferenceCardinality(compare *BitSet) uint
DifferenceCardinality computes the cardinality of the differnce
func (b *BitSet) DumpAsBits() string
DumpAsBits dumps a bit set as a string of bits
func (b *BitSet) Equal(c *BitSet) bool
Equal tests the equivalence of two BitSets. False if they are of different sizes, otherwise true only if all the same bits are set
func (b *BitSet) Flip(i uint) *BitSet
Flip bit at i. If i>= Cap(), this function will panic. Warning: using a very large value for 'i' may lead to a memory shortage and a panic: the caller is responsible for providing sensible parameters in line with their memory capacity.
func (b *BitSet) FlipRange(start, end uint) *BitSet
FlipRange bit in [start, end). If end>= Cap(), this function will panic. Warning: using a very large value for 'end' may lead to a memory shortage and a panic: the caller is responsible for providing sensible parameters in line with their memory capacity.
func (b *BitSet) InPlaceDifference(compare *BitSet)
InPlaceDifference computes the difference of base set and other set This is the BitSet equivalent of &^ (and not)
func (b *BitSet) InPlaceIntersection(compare *BitSet)
InPlaceIntersection destructively computes the intersection of base set and the compare set. This is the BitSet equivalent of & (and)
func (b *BitSet) InPlaceSymmetricDifference(compare *BitSet)
InPlaceSymmetricDifference creates the destructive SymmetricDifference of base set and other set This is the BitSet equivalent of ^ (xor)
func (b *BitSet) InPlaceUnion(compare *BitSet)
InPlaceUnion creates the destructive union of base set and compare set. This is the BitSet equivalent of | (or).
func (b *BitSet) InsertAt(idx uint) *BitSet
InsertAt takes an index which indicates where a bit should be inserted. Then it shifts all the bits in the set to the left by 1, starting from the given index position, and sets the index position to 0.
Depending on the size of your BitSet, and where you are inserting the new entry, this method could be extremely slow and in some cases might cause the entire BitSet to be recopied.
func (b *BitSet) Intersection(compare *BitSet) (result *BitSet)
Intersection of base set and other set This is the BitSet equivalent of & (and)
func (b *BitSet) IntersectionCardinality(compare *BitSet) uint
IntersectionCardinality computes the cardinality of the union
func (b *BitSet) IsStrictSuperSet(other *BitSet) bool
IsStrictSuperSet returns true if this is a strict superset of the other set
func (b *BitSet) IsSuperSet(other *BitSet) bool
IsSuperSet returns true if this is a superset of the other set
func (b *BitSet) Len() uint
Len returns the number of bits in the BitSet. Note the difference to method Count, see example.
▹ Example
func (b *BitSet) MarshalBinary() ([]byte, error)
MarshalBinary encodes a BitSet into a binary form and returns the result.
func (b *BitSet) MarshalJSON() ([]byte, error)
MarshalJSON marshals a BitSet as a JSON structure
func (b *BitSet) NextClear(i uint) (uint, bool)
NextClear returns the next clear bit from the specified index, including possibly the current index along with an error code (true = valid, false = no bit found i.e. all bits are set)
func (b *BitSet) NextSet(i uint) (uint, bool)
NextSet returns the next bit set from the specified index, including possibly the current index along with an error code (true = valid, false = no set bit found) for i,e := v.NextSet(0); e; i,e = v.NextSet(i + 1) {...}
Users concerned with performance may want to use NextSetMany to retrieve several values at once.
func (b *BitSet) NextSetMany(i uint, buffer []uint) (uint, []uint)
NextSetMany returns many next bit sets from the specified index, including possibly the current index and up to cap(buffer). If the returned slice has len zero, then no more set bits were found
buffer := make([]uint, 256) // this should be reused j := uint(0) j, buffer = bitmap.NextSetMany(j, buffer) for ; len(buffer) > 0; j, buffer = bitmap.NextSetMany(j,buffer) { for k := range buffer { do something with buffer[k] } j += 1 }
It is possible to retrieve all set bits as follow:
indices := make([]uint, bitmap.Count()) bitmap.NextSetMany(0, indices)
However if bitmap.Count() is large, it might be preferable to use several calls to NextSetMany, for performance reasons.
func (b *BitSet) None() bool
None returns true if no bit is set, false otherwise. Returns true for empty sets.
func (b *BitSet) ReadFrom(stream io.Reader) (int64, error)
ReadFrom reads a BitSet from a stream written using WriteTo
func (b *BitSet) Set(i uint) *BitSet
Set bit i to 1, the capacity of the bitset is automatically increased accordingly. If i>= Cap(), this function will panic. Warning: using a very large value for 'i' may lead to a memory shortage and a panic: the caller is responsible for providing sensible parameters in line with their memory capacity.
func (b *BitSet) SetTo(i uint, value bool) *BitSet
SetTo sets bit i to value. If i>= Cap(), this function will panic. Warning: using a very large value for 'i' may lead to a memory shortage and a panic: the caller is responsible for providing sensible parameters in line with their memory capacity.
func (b *BitSet) Shrink(lastbitindex uint) *BitSet
Shrink shrinks BitSet so that the provided value is the last possible set value. It clears all bits > the provided index and reduces the size and length of the set.
Note that the parameter value is not the new length in bits: it is the maximal value that can be stored in the bitset after the function call. The new length in bits is the parameter value + 1. Thus it is not possible to use this function to set the length to 0, the minimal value of the length after this function call is 1.
A new slice is allocated to store the new bits, so you may see an increase in memory usage until the GC runs. Normally this should not be a problem, but if you have an extremely large BitSet its important to understand that the old BitSet will remain in memory until the GC frees it.
func (b *BitSet) String() string
String creates a string representation of the Bitmap
func (b *BitSet) SymmetricDifference(compare *BitSet) (result *BitSet)
SymmetricDifference of base set and other set This is the BitSet equivalent of ^ (xor)
func (b *BitSet) SymmetricDifferenceCardinality(compare *BitSet) uint
SymmetricDifferenceCardinality computes the cardinality of the symmetric difference
func (b *BitSet) Test(i uint) bool
Test whether bit i is set.
func (b *BitSet) Union(compare *BitSet) (result *BitSet)
Union of base set and other set This is the BitSet equivalent of | (or)
func (b *BitSet) UnionCardinality(compare *BitSet) uint
UnionCardinality computes the cardinality of the uniton of the base set and the compare set.
func (b *BitSet) UnmarshalBinary(data []byte) error
UnmarshalBinary decodes the binary form generated by MarshalBinary.
func (b *BitSet) UnmarshalJSON(data []byte) error
UnmarshalJSON unmarshals a BitSet from JSON created using MarshalJSON
func (b *BitSet) WriteTo(stream io.Writer) (int64, error)
WriteTo writes a BitSet to a stream
Error is used to distinguish errors (panics) generated in this package.
type Error string