gobitset

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

gobitset

Simple uint64 bitset for Go.

Sets are uint64 values. Elements are integers from 0 to 63 (inclusive). Sets can be used as keys in maps. Basic operations like union and intersection are executed by bit operations.

Using this library will not make your code much shorter. It will only enhance readability. E.g., Union(set1, set2) isn't shorter than set1 | set2, but it's easier to read. And

it := set.GetIterator()
for it.HasNext() {
    fmt.Printf("Value: %d\n", it.NextInt())
}

is probably easier to grasp than

it := set
for it > 0 {
    intValue := TrailingZeros64(it)
    it &^= uint64(1) << intValue
    fmt.Printf("Value: %d\n", intValue)
}

Documentation

Index

Constants

View Source
const EmptySet = Bitset(0)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bitset

type Bitset uint64

func AddInts

func AddInts(bs Bitset, intValues ...int) Bitset

AddInts returns a bitset containing the elements of bs and all added integer values

func DeleteInts

func DeleteInts(bs Bitset, intValues ...int) Bitset

DeleteInts retunrs a bitset containing the elements of bs except for the given integers values

func Intersection

func Intersection(bs Bitset, elementOrSet Bitset) Bitset

Intersection returns the intersection of two bitsets bs1 ∩ bs2

func MakeSet

func MakeSet(intValues ...int) Bitset

MakeSet makes a bitset from one or more integers.

func SetMinus

func SetMinus(bs1 Bitset, bs2 Bitset) Bitset

SetMinus returns the set-difference bs1\bs2

func Union

func Union(bs1 Bitset, bs2 Bitset) Bitset

Union returns the union o two bitsets bs1 ∪ bs2

func (Bitset) GetElements

func (bs Bitset) GetElements() []Bitset

GetElements returns all elements of a bitsets. "Elements" are equivalent to to singleton sets containing one and only one integer.

func (Bitset) GetInts

func (bs Bitset) GetInts() []int

GetInts returns all integer values of a bitset

func (Bitset) GetIterator

func (bs Bitset) GetIterator() *Iterator

GetIterator returns an iterator to loop across all elements of a set:

it := set.GetIterator()
for it.HasNext() {
    fmt.Printf("Value: %d\n", it.NextInt())
}

or after

resultSet := EmptySet()
it := set.GetIterator()
for it.HasNext() {
    resultSet |= it.Next()
}

resultSet and set coincide.

func (Bitset) GetSmallestInt

func (bs Bitset) GetSmallestInt() int

GetSmallestInt returns the smallest integer of a bitset.

func (Bitset) IsStrictSubsetOf

func (bs1 Bitset) IsStrictSubsetOf(bs2 Bitset) bool

IsStrictSubsetOf returns true if b1 is subset of bs2 but not equal to b2

func (Bitset) IsStrictSupersetOf

func (bs1 Bitset) IsStrictSupersetOf(bs2 Bitset) bool

IsStrictSupersetOf returns true if b1 is a superset of b2 but not equal to b2

func (Bitset) IsSubsetOf

func (bs1 Bitset) IsSubsetOf(bs2 Bitset) bool

IsSubsetOf returns true if b1 is subset of bs2 (or equal to b2)

func (Bitset) IsSupersetOf

func (bs1 Bitset) IsSupersetOf(bs2 Bitset) bool

IsSupersetOf returns true if b1 is a superset of (or equal to) b2

func (Bitset) Len

func (bs Bitset) Len() int

Len returns the number of elements in a set

type Iterator

type Iterator uint64

func (*Iterator) HasNext

func (it *Iterator) HasNext() bool

HasNext returns true if there is at least one element left to iterate

func (*Iterator) Next

func (it *Iterator) Next() Bitset

Next returns the next element of the bitset for iteration

func (*Iterator) NextInt

func (it *Iterator) NextInt() int

NextInt returns the next integer contained in the bitset

func (*Iterator) NumRemaining

func (it *Iterator) NumRemaining() int

NumRemaining returns the remaining number of elements to iterate across

func (*Iterator) RemainingSet

func (it *Iterator) RemainingSet() Bitset

RemainingSet returns a bitset containing the remaining elements to loop across

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL