bitset

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustNew

func MustNew[
	BS BitSet[B, V, BT],
	B, V constraints.Unsigned,
	BT BoundaryTrait[V],
](values ...V) BS

MustNew is the same as New but panics on errors.

func New

func New[
	BS BitSet[B, V, BT],
	B, V constraints.Unsigned,
	BT BoundaryTrait[V],
](values ...V) (BS, error)

New creates a new BitSet.

It returns an error if the B, V, and BT types are not compatible, that is:

  • boundaries is a correct range, i.e., (BT{}).Min() <= (BT{}).Max();
  • possible set range [0; <bits in B> - 1] must include boundaries [(BT{}).Min(); (BT{}).Max()].

Types

type BitSet added in v0.2.0

type BitSet[
	B, V constraints.Unsigned,
	BT BoundaryTrait[V],
] struct {
	// contains filtered or unexported fields
}

BitSet is a set that implemented on unsafe.Sizeof(B(0)) bytes and can contain values in the range from (BT{}).Min() to (BT{}).Max() inclusive.

All operations on the set have constant time complexity.

Use the Has method to check whether the given value is in the set.

The *Set and *Unset methods return a new set and error in case of exceeding the range. But they also have Must* and NonStrict* versions to panic or do nothing in case of the error. To change the current set, use *Mut* methods.

BitSet must be created via the New or MustNew functions, otherwise, it will panic on all the operations.

func (BitSet[B, V, BT]) Has added in v0.2.0

func (bs BitSet[B, V, BT]) Has(value V) bool

Has checks whether BitSet contains the given value.

func (*BitSet[B, V, BT]) MustMutSet added in v0.2.0

func (bs *BitSet[B, V, BT]) MustMutSet(value V)

MustMutSet sets the given value in BitSet or panics if the value is out of range.

func (*BitSet[B, V, BT]) MustMutUnset added in v0.2.0

func (bs *BitSet[B, V, BT]) MustMutUnset(value V)

MustMutUnset unsets the given value in BitSet or panics if the value is out of range.

func (BitSet[B, V, BT]) MustSet added in v0.2.0

func (bs BitSet[B, V, BT]) MustSet(value V) BitSet[B, V, BT]

MustSet sets the given value in a new copy of BitSet or panics if the value is out of range.

func (BitSet[B, V, BT]) MustUnset added in v0.2.0

func (bs BitSet[B, V, BT]) MustUnset(value V) BitSet[B, V, BT]

MustUnset unsets the given value in a new copy of BitSet or panics if the value is out of range.

func (*BitSet[B, V, BT]) MutSet added in v0.2.0

func (bs *BitSet[B, V, BT]) MutSet(value V) error

MutSet sets the given value in BitSet. It returns an error if the value is out of range.

func (*BitSet[B, V, BT]) MutUnset added in v0.2.0

func (bs *BitSet[B, V, BT]) MutUnset(value V) error

MutUnset unsets the given value in BitSet. It returns an error if the value is out of range.

func (*BitSet[B, V, BT]) NonStrictMutSet added in v0.2.0

func (bs *BitSet[B, V, BT]) NonStrictMutSet(value V)

NonStrictMutSet sets the given value in BitSet. It ignores if the value is out of range.

func (*BitSet[B, V, BT]) NonStrictMutUnset added in v0.2.0

func (bs *BitSet[B, V, BT]) NonStrictMutUnset(value V)

NonStrictMutUnset unsets the given value in BitSet. It ignores if the value is out of range.

func (BitSet[B, V, BT]) NonStrictSet added in v0.2.0

func (bs BitSet[B, V, BT]) NonStrictSet(value V) BitSet[B, V, BT]

NonStrictSet sets the given value in a new copy of BitSet. It ignores if the value is out of range.

func (BitSet[B, V, BT]) NonStrictUnset added in v0.2.0

func (bs BitSet[B, V, BT]) NonStrictUnset(value V) BitSet[B, V, BT]

NonStrictUnset unsets the given value in a new copy of BitSet. It ignores if the value is out of range.

func (BitSet[B, V, BT]) Set added in v0.2.0

func (bs BitSet[B, V, BT]) Set(value V) (BitSet[B, V, BT], error)

Set sets the given value in a new copy of BitSet. It returns the origin set and an error if the value is out of range.

func (BitSet[B, V, BT]) Unset added in v0.2.0

func (bs BitSet[B, V, BT]) Unset(value V) (BitSet[B, V, BT], error)

Unset unsets the given value in a new copy of BitSet. It returns the origin set and an error if the value is out of range.

type BoundaryTrait

type BoundaryTrait[V constraints.Unsigned] interface {
	~struct{}

	// Min returns the minimum allowed value.
	//
	// Prefer to return a constant value.
	Min() V

	// Max returns the maximum allowed value.
	//
	// Prefer to return a constant value.
	Max() V
}

BoundaryTrait is a trait that defines the range from [BoundaryTrait.Min] to [BoundaryTrait.Max].

Jump to

Keyboard shortcuts

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