bitfield

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2021 License: Apache-2.0, MIT Imports: 6 Imported by: 235

README

go-bitfield

CircleCI standard-readme compliant

Advanced RLE+ implementation

Features iterator based primitives that scale with number of runs instead of number of bits.

License

The Filecoin Project is dual-licensed under Apache 2.0 and MIT terms:

Documentation

Index

Constants

View Source
const MaxEncodedSize = 32 << 10

MaxEncodedSize is the maximum encoded size of a bitfield. When expanded into a slice of runs, a bitfield of this size should not exceed 2MiB of memory.

This bitfield can fit at least 3072 sparse elements.

Variables

View Source
var (
	ErrBitFieldTooMany = errors.New("to many items in RLE")
	ErrNoBitsSet       = errors.New("bitfield has no set bits")
)

Functions

This section is empty.

Types

type BitField

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

func CutBitField added in v0.1.0

func CutBitField(a, b BitField) (BitField, error)

CutBitField cuts bitfield B from bitfield A. For every bit in B cut from A, subsequent entries in A are shifted down by one.

For example:

a: 0 1 0 1 1 1
b: 0 1 1 0 0 0

c: 0     1 1 1 // cut
c: 0 1 1 1     // remove holes

func IntersectBitField

func IntersectBitField(a, b BitField) (BitField, error)

IntersectBitField returns the intersection of the two BitFields.

For example, given two BitFields:

0 1 1 0 1
1 1 0 1 0

IntersectBitField would return

0 1 0 0 0

This operation's runtime is O(number of runs).

func MergeBitFields

func MergeBitFields(a, b BitField) (BitField, error)

MergeBitFields returns the union of the two BitFields.

For example, given two BitFields:

0 1 1 0 1
1 1 0 1 0

MergeBitFields would return

1 1 1 1 1

This operation's runtime is O(number of runs).

func MultiMerge

func MultiMerge(bfs ...BitField) (BitField, error)

MultiMerge returns the unions of all the passed BitFields.

Calling MultiMerge is identical to calling MergeBitFields repeatedly, just more efficient when merging more than two BitFields.

This operation's runtime is O(number of runs * number of bitfields).

func New

func New() BitField

New constructs a new BitField.

func NewFromBytes

func NewFromBytes(rle []byte) (BitField, error)

NewFromBytes deserializes the encoded bitfield.

func NewFromIter

func NewFromIter(r rlepluslazy.RunIterator) (BitField, error)

NewFromIter constructs a BitField from the RunIterator.

func NewFromSet

func NewFromSet(setBits []uint64) BitField

NewFromSet constructs a bitfield from the given set.

func SubtractBitField

func SubtractBitField(a, b BitField) (BitField, error)

SubtractBitField returns the difference between the two BitFields. That is, it returns a bitfield of all bits set in a but not set in b.

For example, given two BitFields:

0 1 1 0 1 // a
1 1 0 1 0 // b

SubtractBitFields would return

0 0 1 0 1

This operation's runtime is O(number of runs).

func (BitField) All

func (bf BitField) All(max uint64) ([]uint64, error)

All returns a slice of set bits in sorted order.

For example, given:

1 0 0 1

All will return:

[]uint64{0, 3}

This operation's runtime is O(number of bits).

func (BitField) AllMap

func (bf BitField) AllMap(max uint64) (map[uint64]bool, error)

AllMap returns a map of all set bits.

For example, given:

1 0 0 1

All will return:

map[uint64]bool{0: true, 3: true}

This operation's runtime is O(number of bits).

func (BitField) BitIterator added in v0.0.3

func (bf BitField) BitIterator() (rlepluslazy.BitIterator, error)

BitIterator iterates over the bits in the bitmap

func (BitField) Copy

func (bf BitField) Copy() (BitField, error)

Copy flushes the bitfield and returns a copy that can be mutated without changing the original values

func (BitField) Count

func (bf BitField) Count() (uint64, error)

Count counts the non-zero bits in the bitfield.

For example, given:

1 0 1 1

Count() will return 3.

This operation's runtime is O(number of runs).

func (BitField) First

func (bf BitField) First() (uint64, error)

First returns the index of the first set bit. This function returns ErrNoBitsSet when no bits have been set.

This operation's runtime is O(1).

func (BitField) ForEach

func (bf BitField) ForEach(f func(uint64) error) error

ForEach iterates over each set bit.

This operation's runtime is O(bits set).

func (BitField) IsEmpty

func (bf BitField) IsEmpty() (bool, error)

IsEmpty returns true if the bitset is empty.

This operation's runtime is O(1).

func (BitField) IsSet

func (bf BitField) IsSet(x uint64) (bool, error)

IsSet returns true if the given bit is set.

This operation's runtime is O(number of runs).

func (BitField) Last added in v0.1.2

func (bf BitField) Last() (uint64, error)

Last returns the index of the last set bit. This function returns ErrNoBitsSet when no bits have been set.

This operation's runtime is O(n).

func (BitField) MarshalCBOR

func (bf BitField) MarshalCBOR(w io.Writer) error

func (BitField) MarshalJSON

func (bf BitField) MarshalJSON() ([]byte, error)

func (BitField) RunIterator added in v0.0.3

func (bf BitField) RunIterator() (rlepluslazy.RunIterator, error)

func (BitField) Set

func (bf BitField) Set(bit uint64)

Set sets the given bit in the BitField

This operation's runtime is O(1) up-front. However, it adds an O(bits explicitly set) cost to all other operations.

func (BitField) Slice

func (bf BitField) Slice(start, count uint64) (BitField, error)

Slice treats the BitField as an ordered set of set bits, then slices this set.

That is, it skips start set bits, then returns the next count set bits.

For example, given:

1 0 1 1 0 1 1

bf.Slice(2, 2) would return:

0 0 0 1 0 1 0

This operation's runtime is O(number of runs).

func (*BitField) UnmarshalCBOR

func (bf *BitField) UnmarshalCBOR(r io.Reader) error

func (*BitField) UnmarshalJSON

func (bf *BitField) UnmarshalJSON(b []byte) error

func (BitField) Unset

func (bf BitField) Unset(bit uint64)

Unset unsets given bit in the BitField

This operation's runtime is O(1). However, it adds an O(bits explicitly unset) cost to all other operations.

Directories

Path Synopsis
rle

Jump to

Keyboard shortcuts

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