intsets

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxInt is the maximum integer that can be stored in a set.
	MaxInt = int(^uint(0) >> 1)
	// MinInt is the minimum integer that can be stored in a set.
	MinInt = -MaxInt - 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Fast

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

Fast keeps track of a set of integers. It does not perform any allocations when the values are in the range [0, smallCutoff). It is not thread-safe.

func MakeFast

func MakeFast(vals ...int) Fast

MakeFast returns a set initialized with the given values.

func (*Fast) Add

func (s *Fast) Add(i int)

Add adds a value to the set. No-op if the value is already in the set. If the large set is not nil and the value is within the range [0, 63], the value is added to both the large and small sets.

func (*Fast) AddRange

func (s *Fast) AddRange(from, to int)

AddRange adds values 'from' up to 'to' (inclusively) to the set. E.g. AddRange(1,5) adds the values 1, 2, 3, 4, 5 to the set. 'to' must be >= 'from'. AddRange is always more efficient than individual Adds.

func (Fast) Contains

func (s Fast) Contains(i int) bool

Contains returns true if the set contains the value.

func (Fast) Copy

func (s Fast) Copy() Fast

Copy returns a copy of s which can be modified independently.

func (*Fast) CopyFrom

func (s *Fast) CopyFrom(other Fast)

CopyFrom sets the receiver to a copy of other, which can then be modified independently.

func (*Fast) Decode

func (s *Fast) Decode(br io.ByteReader) error

Decode does the opposite of Encode. The contents of the receiver are overwritten.

func (Fast) Difference

func (s Fast) Difference(rhs Fast) Fast

Difference returns the elements of s that are not in rhs as a new set.

func (*Fast) DifferenceWith

func (s *Fast) DifferenceWith(rhs Fast)

DifferenceWith removes any elements in rhs from this set.

func (Fast) Empty

func (s Fast) Empty() bool

Empty returns true if the set is empty.

func (*Fast) Encode

func (s *Fast) Encode(buf *bytes.Buffer) error

Encode the set and write it to a bytes.Buffer using binary.varint byte encoding.

This method cannot be used if the set contains negative elements.

If the set has only elements in the range [0, 63], we encode a 0 followed by a 64-bit bitmap. Otherwise, we encode a length followed by each element.

WARNING: this is used by plan gists, so if this encoding changes, explain.gistVersion needs to be bumped.

func (Fast) Equals

func (s Fast) Equals(rhs Fast) bool

Equals returns true if the two sets are identical.

func (Fast) ForEach

func (s Fast) ForEach(f func(i int))

ForEach calls a function for each value in the set (in increasing order).

func (Fast) Intersection

func (s Fast) Intersection(rhs Fast) Fast

Intersection returns the intersection of s and rhs as a new set.

func (*Fast) IntersectionWith

func (s *Fast) IntersectionWith(rhs Fast)

IntersectionWith removes any elements not in rhs from this set.

func (Fast) Intersects

func (s Fast) Intersects(rhs Fast) bool

Intersects returns true if s has any elements in common with rhs.

func (Fast) Len

func (s Fast) Len() int

Len returns the number of the elements in the set.

func (Fast) Next

func (s Fast) Next(startVal int) (int, bool)

Next returns the first value in the set which is >= startVal. If there is no value, the second return value is false.

func (Fast) Ordered

func (s Fast) Ordered() []int

Ordered returns a slice with all the integers in the set, in increasing order.

func (*Fast) Remove

func (s *Fast) Remove(i int)

Remove removes a value from the set. No-op if the value is not in the set.

func (Fast) String

func (s Fast) String() string

String returns a list representation of elements. Sequential runs of positive numbers are shown as ranges. For example, for the set {0, 1, 2, 5, 6, 10}, the output is "(0-2,5,6,10)".

func (Fast) SubsetOf

func (s Fast) SubsetOf(rhs Fast) bool

SubsetOf returns true if rhs contains all the elements in s.

func (Fast) Union

func (s Fast) Union(rhs Fast) Fast

Union returns the union of s and rhs as a new set.

func (*Fast) UnionWith

func (s *Fast) UnionWith(rhs Fast)

UnionWith adds all the elements from rhs to this set.

type Sparse

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

Sparse is a set of integers. It is not thread-safe. It must be copied with the Copy method.

Sparse is implemented as a linked list of blocks, each containing an offset and a bitmap. A block with offset=o contains an integer o+b if the b-th bit of the bitmap is set. Block offsets are always divisible by smallCutoff.

For example, here is a diagram of the set {0, 1, 128, 129, 512}, where each block is denoted by {offset, bitmap}:

{0, ..011} ---> {128, ..011} ---> {512, ..001}

Sparse is inspired by golang.org/x/tools/container/intsets. Sparse implements a smaller API, providing only the methods required by Fast. The omission of a Max method allows us to use a singly-linked list here instead of a circular, doubly-linked list.

func (*Sparse) Add

func (s *Sparse) Add(i int)

Add adds an integer to the set.

func (*Sparse) Clear

func (s *Sparse) Clear()

Clear empties the set.

func (Sparse) Contains

func (s Sparse) Contains(i int) bool

Contains returns true if the set contains the given integer.

func (*Sparse) Copy

func (s *Sparse) Copy(rhs *Sparse)

Copy sets the receiver to a copy of rhs, which can then be modified independently.

func (*Sparse) DifferenceWith

func (s *Sparse) DifferenceWith(rhs *Sparse)

DifferenceWith removes any elements in rhs from this set.

func (Sparse) Empty

func (s Sparse) Empty() bool

Empty returns true if the set contains no integers.

func (*Sparse) Equals

func (s *Sparse) Equals(rhs *Sparse) bool

Equals returns true if the two sets are identical.

func (*Sparse) IntersectionWith

func (s *Sparse) IntersectionWith(rhs *Sparse)

IntersectionWith removes any elements not in rhs from this set.

func (*Sparse) Intersects

func (s *Sparse) Intersects(rhs *Sparse) bool

Intersects returns true if s has any elements in common with rhs.

func (Sparse) Len

func (s Sparse) Len() int

Len returns the number of integers in the set.

func (*Sparse) LowerBound

func (s *Sparse) LowerBound(startVal int) int

LowerBound returns the smallest element >= startVal, or MaxInt if there is no such element.

func (*Sparse) Min

func (s *Sparse) Min() int

Min returns the minimum value in the set. If the set is empty, MaxInt is returned.

func (*Sparse) Remove

func (s *Sparse) Remove(i int)

Remove removes an integer from the set.

func (*Sparse) SubsetOf

func (s *Sparse) SubsetOf(rhs *Sparse) bool

SubsetOf returns true if rhs contains all the elements in s.

func (*Sparse) UnionWith

func (s *Sparse) UnionWith(rhs *Sparse)

UnionWith adds all the elements from rhs to this set.

Jump to

Keyboard shortcuts

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