bitset

package
v0.0.0-...-ad3f07b Latest Latest
Warning

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

Go to latest
Published: May 25, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Encoding = base64.URLEncoding

Functions

func Cap

func Cap() uint

Cap returns the total possible capacity, or number of bits that can be stored in the BitSet theoretically. Under 32-bit system, it is 4294967295 and under 64-bit system, it is 18446744073709551615. Note that this is further limited by the maximum allocation size in Go, and your available memory, as any Go data structure.

Types

type BitSet

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

func New

func New(length uint) *BitSet

func (*BitSet) All

func (b *BitSet) All() bool

All returns true if all bits are set, false otherwise. Returns true for empty sets.

func (*BitSet) Any

func (b *BitSet) Any() bool

Any returns true if any bit is set, false otherwise

func (*BitSet) Clear

func (b *BitSet) Clear(i uint) *BitSet

Clear bit i to 0. This never cause a memory allocation. It is always safe.

func (*BitSet) ClearAll

func (b *BitSet) ClearAll() *BitSet

ClearAll clears the entire BitSet.

func (*BitSet) Compact

func (b *BitSet) Compact() *BitSet

Compact shrinks BitSet to so that we preserve all set bits, while minimizing memory usage. Compact calls Shrink.

func (*BitSet) Count

func (b *BitSet) Count() uint

func (*BitSet) Flip

func (b *BitSet) Flip(i uint) *BitSet

Flip bit at i.

func (*BitSet) FlipRange

func (b *BitSet) FlipRange(start, end uint) *BitSet

FlipRange bit in [start, end).

func (*BitSet) Iterate

func (b *BitSet) Iterate() iter.Seq[uint]

func (*BitSet) Len

func (b *BitSet) Len() uint

Len returns the number of bits in the BitSet. Note that it differ from Count function.

func (*BitSet) MarshalBinary

func (b *BitSet) MarshalBinary() ([]byte, error)

MarshalBinary encodes a BitSet into a binary form and returns the result. Please see WriteTo for details.

func (*BitSet) MarshalJSON

func (b *BitSet) MarshalJSON() ([]byte, error)

MarshalJSON marshals a BitSet as a JSON structure

func (*BitSet) None

func (b *BitSet) None() bool

None returns true if no bit is set, false otherwise. Returns true for empty sets.

func (*BitSet) ReadFrom

func (b *BitSet) ReadFrom(stream io.Reader) (int64, error)

ReadFrom reads a BitSet from a stream written using WriteTo The format is: 1. uint64 length 2. []uint64 set See WriteTo for details. Upon success, the number of bytes read is returned. If the current BitSet is not large enough to hold the data, it is extended. In case of error, the BitSet is either left unchanged or made empty if the error occurs too late to preserve the content.

Performance: if this function is used to read from a disk or network connection, it might be beneficial to wrap the stream in a bufio.Reader. E.g.,

f, err := os.Open("myfile")
r := bufio.NewReader(f)

func (*BitSet) Set

func (b *BitSet) Set(i uint) *BitSet

Set bit i to 1, the capacity of the bitset is automatically increased accordingly. Warning: using a very large value for 'i' may lead to a memory shortage and a panic: the caller is responsible for providing sensible parameters in line with their memory capacity. The memory usage is at least slightly over i/8 bytes.

func (*BitSet) SetAll

func (b *BitSet) SetAll() *BitSet

SetAll sets the entire BitSet

func (*BitSet) SetTo

func (b *BitSet) SetTo(i uint, value bool) *BitSet

SetTo sets bit i to value. Warning: using a very large value for 'i' may lead to a memory shortage and a panic: the caller is responsible for providing sensible parameters in line with their memory capacity.

func (*BitSet) Shrink

func (b *BitSet) Shrink(lastbitindex uint) *BitSet

Shrink shrinks BitSet so that the provided value is the last possible set value. It clears all bits > the provided index and reduces the size and length of the set.

func (*BitSet) String

func (b *BitSet) String() string

String creates a string representation of the BitSet. It is only intended for human-readable output and not for serialization.

func (*BitSet) Test

func (b *BitSet) Test(i uint) bool

Test whether bit i is set.

func (*BitSet) UnmarshalBinary

func (b *BitSet) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes the binary form generated by MarshalBinary. Please see WriteTo for details.

func (*BitSet) UnmarshalJSON

func (b *BitSet) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a BitSet from JSON created using MarshalJSON

func (*BitSet) WriteTo

func (b *BitSet) WriteTo(stream io.Writer) (int64, error)

WriteTo writes a BitSet to a stream. The format is: 1. uint64 length 2. []uint64 set The length is the number of bits in the BitSet.

The set is a slice of uint64s containing between length and length + 63 bits. It is interpreted as a big-endian array of uint64s by default (see BinaryOrder()) meaning that the first 8 bits are stored at byte index 7, the next 8 bits are stored at byte index 6... the bits 64 to 71 are stored at byte index 8, etc. If you change the binary order, you need to do so for both reading and writing. We recommend using the default binary order.

Upon success, the number of bytes written is returned.

Performance: if this function is used to write to a disk or network connection, it might be beneficial to wrap the stream in a bufio.Writer. E.g.,

      f, err := os.Create("myfile")
	       w := bufio.NewWriter(f)

Jump to

Keyboard shortcuts

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