bitset

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: BSD-2-Clause Imports: 4 Imported by: 0

README

GitHub go.mod Go version of a Go module License

BitSet

This is a simple, though very fast, bitset implementation in Go derived from the yourbasic/bit package.

Installation

go get github.com/KernelPryanic/bitset

Usage

Creating BitSets
// Create an empty bitset
empty := bitset.New()

// Create a bitset with initial values
set := bitset.New(1, 3, 5, 7)
Basic Operations
// Add elements
set.Add(9)          // Add single element
set.AddRange(2, 5)  // Add range [2,3,4]

// Check membership
exists := set.Contains(3)    // true
absent := set.Contains(6)    // false

// Remove elements
set.Delete(3)               // Remove single element
set.DeleteRange(2, 5)       // Remove range [2,3,4]

// Get set information
size := set.Size()          // Number of elements
isEmpty := set.Empty()      // Check if set is empty
max := set.Max()           // Get maximum element

// Iterate through elements
set.Visit(func(n int) bool {
    fmt.Printf("%d ", n)
    return false  // Return true to stop iteration
})
Set Operations
set1 := bitset.New(1, 2, 3, 4, 5)
set2 := bitset.New(4, 5, 6, 7, 8)

// Create new sets from operations
intersection := bitset.And(set1, set2)  // Elements in both sets
union := bitset.Or(set1, set2)         // Elements in either set
symDiff := bitset.Xor(set1, set2)      // Elements in one set but not both
diff := bitset.AndNot(set1, set2)      // Elements in set1 but not in set2

// Modify existing sets
set1.And(set2)    // Keep only elements present in both sets
set1.Or(set2)     // Add all elements from set2
set1.Xor(set2)    // Keep elements present in one set but not both
set1.AndNot(set2) // Remove all elements present in set2
Navigation
set := bitset.New(1, 3, 5, 7, 9)

// Find next/previous elements
next := set.Next(4)     // Returns 5 (next element after 4)
prev := set.Prev(6)     // Returns 5 (previous element before 6)

// Copy sets
copy := set.Copy()      // Create a new copy
set2 := bitset.New()
set2.Set(set)          // Replace contents of set2 with set
String Representation
set := bitset.New(1, 2, 3, 5, 7, 8, 9, 10)
fmt.Println(set) // Outputs: {1..3 5 7..10}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BitSet

type BitSet []uint64

BitSet is a set of non-negative integers represented as a slice of uint64 words, where each bit i in word w corresponds to the integer 64*n + i. The words are kept in ascending order, and the set is trimmed to remove trailing zero words.

func And

func And(s1, s2 BitSet) BitSet

And creates a new set that consists of all elements in both s1 and s2.

func AndNot

func AndNot(s1, s2 BitSet) BitSet

AndNot creates a new set that consists of all elements in s1 but not in s2.

func New

func New(n ...int) BitSet

New creates a new set with the given non-negative elements. If all n are negative, an empty set is created. The elements are stored in ascending order. The zero value of BitSet is an empty set.

func Or

func Or(s1, s2 BitSet) BitSet

Or creates a new set that contains all elements in s1 or s2.

func Xor

func Xor(s1, s2 BitSet) BitSet

Xor creates a new set that contains all elements in s1 or s2 but not both.

func (*BitSet) Add

func (bs *BitSet) Add(n int)

Add adds n to bs (no-op if n < 0).

func (*BitSet) AddRange

func (bs *BitSet) AddRange(m, n int)

AddRange adds all integers from m to n-1 to bs (no-op if m>=n).

func (*BitSet) And

func (bs *BitSet) And(other BitSet)

And keeps only bits set in both *bs and other.

func (*BitSet) AndNot

func (bs *BitSet) AndNot(other BitSet)

AndNot removes bits that are set in other from *bs.

func (BitSet) Contains

func (bs BitSet) Contains(n int) bool

Contains tells if n is in the set.

func (BitSet) Copy

func (bs BitSet) Copy() BitSet

Copy creates a new set that is a copy of bs.

func (*BitSet) Delete

func (bs *BitSet) Delete(n int)

Delete removes n from bs (no-op if n < 0 or not present).

func (*BitSet) DeleteRange

func (bs *BitSet) DeleteRange(m, n int)

DeleteRange removes all integers from m to n-1 (no-op if m>=n).

func (BitSet) Empty

func (bs BitSet) Empty() bool

Empty tells if the set is empty.

func (BitSet) Equal

func (bs BitSet) Equal(other BitSet) bool

Equal tells if bs and other are equal.

func (BitSet) Max

func (bs BitSet) Max() int

Max returns the maximum element of the bitset. If the set is empty, -1 is returned.

func (BitSet) Next

func (bs BitSet) Next(m int) int

Next returns the next element n, n > m, in the set, or -1 if there is no such element.

func (*BitSet) Or

func (bs *BitSet) Or(other BitSet)

Or sets bits that are set in either *bs or other.

func (BitSet) Prev

func (bs BitSet) Prev(m int) int

Prev returns the previous element n, n < m, in the set, or -1 if there is no such element.

func (*BitSet) Reset

func (bs *BitSet) Reset()

Reset resets the set without reallocation.

func (*BitSet) Set

func (bs *BitSet) Set(other BitSet)

Set replaces the contents of *bs with other.

func (BitSet) Size

func (bs BitSet) Size() int

Size returns the number of elements in the set.

func (BitSet) String

func (bs BitSet) String() string

String returns a string representation of the set. The elements are listed in ascending order.

Example: {0 2 4..7 9 11 13 15}

func (BitSet) Subset

func (bs BitSet) Subset(other BitSet) bool

Subset tells if bs is a subset of other.

func (BitSet) Visit

func (bs BitSet) Visit(do func(n int) bool) (aborted bool)

Visit calls the do function for each element of s in numerical order. If do returns true, Visit returns immediately, skipping any remaining elements, and returns true. It is safe for do to add or delete elements e, e ≤ n. The behavior of Visit is undefined if do changes the set in any other way.

func (BitSet) VisitAll

func (bs BitSet) VisitAll(do func(n int))

VisitAll calls do function for each element of s in numerical order.

func (*BitSet) Xor

func (bs *BitSet) Xor(other BitSet)

Xor toggles bits that are set in either *bs or other but not both.

type Word

type Word = uint64

Word is a convenience alias.

Jump to

Keyboard shortcuts

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