bitset

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: MIT Imports: 5 Imported by: 0

README

bitset: Bitsets for Go

This package includes a radix-tree implementation that uses Hash Array Mapped Tries as well as a more traditional dense implementation.

Documentation

Overview

Package bitset implements bitsets. A bitset is a set of non-negative integers represented using a bit for each integer.

There are three implementations:

Use Dense for the common case where you know the largest value that could be in the set, and you want to use a sequence of bits. Addition, removal and membership tests on a Dense bitset are very fast, and memory is proportional to the largest possible value (one bit per possible value).

Use Sparse for bitsets whose values can come from a wide range. Sparse bitsets take more time per operation, but can use less memory than a Dense bitset if the set contains relatively few elements drawn from a large range. For example, A Dense bitset with the elements 1000, 2000, ...., 1_000_000 would occupy 122K, while a Sparse one would take about 57K.

Set64 is a faster version of Dense when the largest possible value is 63.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dense

type Dense struct {
	// contains filtered or unexported fields
}

Dense is a standard bitset, represented as a sequence of bits. See Sparse in this package for a more memory-efficient storage scheme for sparse bitsets.

func NewDense

func NewDense(capacity int) *Dense

NewDense creates a set capable of representing values in the range [0, capacity), at least. The Cap method reports the exact capacity. NewDense panics if capacity is negative.

func (*Dense) Add

func (s *Dense) Add(n uint)

Add adds n to s.

func (*Dense) AddIn

func (s1 *Dense) AddIn(s2 *Dense)

AddIn adds all the elements in s2 to s1. It sets s1 to the union of s1 and s2.

func (*Dense) Cap

func (s *Dense) Cap() int

Cap returns the maximum number of elements the set can contain, which is one greater than the largest element it can contain.

func (*Dense) Clear

func (s *Dense) Clear()

Clear removes all elements from s.

func (*Dense) Complement

func (s *Dense) Complement()

Complement replaces s with its complement.

func (*Dense) Contains

func (s *Dense) Contains(n uint) bool

Contains reports whether s contains s.

func (*Dense) Copy

func (s *Dense) Copy() *Dense

Copy returns a copy of s.

func (*Dense) Elements

func (s *Dense) Elements(f func([]uint) bool)

Elements calls f on successive slices of the set's elements, from lowest to highest. If f returns false, the iteration stops. The slice passed to f will be reused when f returns.

func (*Dense) Empty

func (s *Dense) Empty() bool

Empty reports whether s has no elements.

func (*Dense) Equal

func (s1 *Dense) Equal(s2 *Dense) bool

Equal reports whether s2 has the same elements as s1. It may have a different capacity.

func (*Dense) Len

func (s *Dense) Len() int

Len returns the number of elements in s.

func (*Dense) LenRemoveIn added in v0.1.1

func (s1 *Dense) LenRemoveIn(s2 *Dense) int

LenRemoveIn returns what s1.Len() would be after s1.RemoveIn(s2), without modifying s1.

func (*Dense) Remove

func (s *Dense) Remove(n uint)

Remove removes n from s.

func (*Dense) RemoveIn

func (s1 *Dense) RemoveIn(s2 *Dense)

RemoveIn removes from s1 all the elements that are in s2. It sets s1 to the set difference of s1 and s2.

func (*Dense) RemoveNotIn

func (s1 *Dense) RemoveNotIn(s2 *Dense)

RemoveNotIn removes from s1 all the elements that are not in s2. It sets s1 to the intersection of s1 and s2.

func (*Dense) SetCap

func (s *Dense) SetCap(newCapacity int)

SetCap changes the capacity of s.

type Set64

type Set64 uint64

Set64 is an efficient representation of a bitset that can represent integers in the range [0, 64). For efficiency, the methods of Set64 perform no bounds checking on their arguments.

func Set64From

func Set64From(els ...uint8) Set64

Set64From constructs a Set64 from a list of elements.

func (*Set64) Add

func (s *Set64) Add(u uint8)

Add adds u to s.

func (*Set64) AddIn

func (s1 *Set64) AddIn(s2 Set64)

AddIn adds all the elements in s2 to s1. It sets s1 to the union of s1 and s2.

func (*Set64) Clear

func (s *Set64) Clear()

Clear removes all elements from s.

func (*Set64) Complement

func (s *Set64) Complement()

Complement replaces s with its complement.

func (*Set64) Contains

func (s *Set64) Contains(u uint8) bool

Contains reports whether s contains u.

func (Set64) Empty

func (s Set64) Empty() bool

Empty reports whether s has no elements.

func (Set64) Equal

func (s1 Set64) Equal(s2 Set64) bool

Equal reports whether two bitsets have the same elements.

func (Set64) Len

func (s Set64) Len() int

Len returns the number of elements in s.

func (*Set64) Remove

func (s *Set64) Remove(u uint8)

Remove removes u from s.

func (*Set64) RemoveIn

func (s1 *Set64) RemoveIn(s2 Set64)

RemoveIn removes from s1 all the elements that are in s2. It sets s1 to the set difference of s1 and s2.

func (*Set64) RemoveNotIn

func (s1 *Set64) RemoveNotIn(s2 Set64)

RemoveNotIn removes from s1 all the elements that are not in s2. It sets s1 to the intersection of s1 and s2.

func (Set64) String

func (s Set64) String() string

String returns a representation of s in standard set notation.

type Sparse

type Sparse struct {
	// contains filtered or unexported fields
}

Sparse is a sparse bitset. It can represent any uint64 and uses memory proportional to the number of elements it contains.

func NewSparse

func NewSparse() *Sparse

NewSparse creates a new Sparse bitset.

func (*Sparse) Add

func (s *Sparse) Add(n uint)

Add adds n to s.

func (*Sparse) Add64

func (s *Sparse) Add64(n uint64)

Add64 adds n to s.

func (*Sparse) AddIn

func (s1 *Sparse) AddIn(s2 *Sparse)

AddIn adds all the elements in s2 to s1. It sets s1 to the union of s1 and s2.

func (*Sparse) Clear

func (s *Sparse) Clear()

Clear removes all elements from s.

func (*Sparse) Contains

func (s *Sparse) Contains(n uint) bool

Contains reports whether s contains s.

func (*Sparse) Contains64

func (s *Sparse) Contains64(n uint64) bool

Contains64 reports whether s contains s.

func (*Sparse) Copy

func (s *Sparse) Copy() *Sparse

Copy returns a copy of s.

func (*Sparse) Elements

func (s *Sparse) Elements(f func([]uint64) bool)

Elements calls f on successive slices of the set's elements, from lowest to highest. If f returns false, the iteration stops. The slice passed to f will be reused when f returns.

func (*Sparse) Empty

func (s *Sparse) Empty() bool

Empty reports whether s has no elements.

func (*Sparse) Equal

func (s1 *Sparse) Equal(s2 *Sparse) bool

Equal reports whether two bitsets have the same elements.

func (*Sparse) Len

func (s *Sparse) Len() int

Len returns the number of elements in s.

func (*Sparse) Remove

func (s *Sparse) Remove(n uint)

Remove removes n from s.

func (*Sparse) Remove64

func (s *Sparse) Remove64(n uint64)

Remove64 removes n from s.

func (*Sparse) RemoveIn

func (s1 *Sparse) RemoveIn(s2 *Sparse)

RemoveIn removes from s1 all the elements that are in s2. It sets s1 to the set difference of s1 and s2.

func (*Sparse) RemoveNotIn

func (s1 *Sparse) RemoveNotIn(s2 *Sparse)

RemoveNotIn removes from s1 all the elements that are not in s2. It sets s1 to the intersection of s1 and s2.

func (*Sparse) String

func (s *Sparse) String() string

String returns a representation of s in standard set notation.

Jump to

Keyboard shortcuts

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