bitutil

package
v17.0.0-...-d7a5777 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 7 more Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BitMask        = [8]byte{1, 2, 4, 8, 16, 32, 64, 128}
	FlippedBitMask = [8]byte{254, 253, 251, 247, 239, 223, 191, 127}
)
View Source
var (
	// PrecedingBitmask is a convenience set of values as bitmasks for checking
	// prefix bits of a byte
	PrecedingBitmask = [8]byte{0, 1, 3, 7, 15, 31, 63, 127}
	// TrailingBitmask is the bitwise complement version of kPrecedingBitmask
	TrailingBitmask = [8]byte{255, 254, 252, 248, 240, 224, 192, 128}
)

Functions

func BitIsNotSet

func BitIsNotSet(buf []byte, i int) bool

BitIsNotSet returns true if the bit at index i in buf is not set (0).

func BitIsSet

func BitIsSet(buf []byte, i int) bool

BitIsSet returns true if the bit at index i in buf is set (1).

func BitmapAnd

func BitmapAnd(left, right []byte, lOffset, rOffset int64, out []byte, outOffset int64, length int64)

func BitmapAndAlloc

func BitmapAndAlloc(mem memory.Allocator, left, right []byte, lOffset, rOffset int64, length, outOffset int64) *memory.Buffer

func BitmapAndNot

func BitmapAndNot(left, right []byte, lOffset, rOffset int64, out []byte, outOffset int64, length int64)

func BitmapAndNotAlloc

func BitmapAndNotAlloc(mem memory.Allocator, left, right []byte, lOffset, rOffset int64, length, outOffset int64) *memory.Buffer

func BitmapEquals

func BitmapEquals(left, right []byte, lOffset, rOffset int64, length int64) bool

func BitmapOp

func BitmapOp(op bitOp, left, right []byte, lOffset, rOffset int64, out []byte, outOffset, length int64)

func BitmapOpAlloc

func BitmapOpAlloc(mem memory.Allocator, op bitOp, left, right []byte, lOffset, rOffset int64, length int64, outOffset int64) *memory.Buffer

func BitmapOr

func BitmapOr(left, right []byte, lOffset, rOffset int64, out []byte, outOffset int64, length int64)

func BitmapOrAlloc

func BitmapOrAlloc(mem memory.Allocator, left, right []byte, lOffset, rOffset int64, length, outOffset int64) *memory.Buffer

func BitmapXor

func BitmapXor(left, right []byte, lOffset, rOffset int64, out []byte, outOffset int64, length int64)

func BitmapXorAlloc

func BitmapXorAlloc(mem memory.Allocator, left, right []byte, lOffset, rOffset int64, length, outOffset int64) *memory.Buffer

func BytesForBits

func BytesForBits(bits int64) int64

func CeilByte

func CeilByte(size int) int

CeilByte rounds size to the next multiple of 8.

func CeilByte64

func CeilByte64(size int64) int64

CeilByte64 rounds size to the next multiple of 8.

func ClearBit

func ClearBit(buf []byte, i int)

ClearBit sets the bit at index i in buf to 0.

func CopyBitmap

func CopyBitmap(src []byte, srcOffset, length int, dst []byte, dstOffset int)

CopyBitmap copies the bitmap indicated by src, starting at bit offset srcOffset, and copying length bits into dst, starting at bit offset dstOffset.

func CountSetBits

func CountSetBits(buf []byte, offset, n int) int

CountSetBits counts the number of 1's in buf up to n bits.

func InvertBitmap

func InvertBitmap(src []byte, srcOffset, length int, dst []byte, dstOffset int)

InvertBitmap copies a bit range of a bitmap, inverting it as it copies over into the destination.

func IsMultipleOf64

func IsMultipleOf64(v int64) bool

IsMultipleOf64 returns whether v is a multiple of 64

func IsMultipleOf8

func IsMultipleOf8(v int64) bool

IsMultipleOf8 returns whether v is a multiple of 8.

func NextPowerOf2

func NextPowerOf2(x int) int

NextPowerOf2 rounds x to the next power of two.

func SetBit

func SetBit(buf []byte, i int)

SetBit sets the bit at index i in buf to 1.

func SetBitTo

func SetBitTo(buf []byte, i int, val bool)

SetBitTo sets the bit at index i in buf to val.

func SetBitsTo

func SetBitsTo(bits []byte, startOffset, length int64, areSet bool)

SetBitsTo is a convenience function to quickly set or unset all the bits in a bitmap starting at startOffset for length bits.

func VisitWordsAndWrite

func VisitWordsAndWrite(args []Bitmap, out []Bitmap, visitor func(in, out []uint64)) error

VisitWordsAndWrite visits words of bits from each input bitmap and collects outputs to a slice of output Bitmaps.

All bitmaps must have identical lengths. The first bit in a visited bitmap may be offset within the first visited word, but words will otherwise contain densely packed bits loaded from the bitmap. That offset within the first word is returned.

NOTE: this function is efficient on 3+ sufficiently large bitmaps. It also has a large prolog/epilog overhead and should be used carefully in other cases. For 2 or fewer bitmaps, and/or smaller bitmaps, try BitmapReader and or other utilities.

Types

type Bitmap

type Bitmap struct {
	Data        []byte
	Offset, Len int64
}

type BitmapReader

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

BitmapReader is a simple bitmap reader for a byte slice.

func NewBitmapReader

func NewBitmapReader(bitmap []byte, offset, length int) *BitmapReader

NewBitmapReader creates and returns a new bitmap reader for the given bitmap

func (*BitmapReader) Len

func (b *BitmapReader) Len() int

Len returns the total number of bits in the bitmap

func (*BitmapReader) Next

func (b *BitmapReader) Next()

Next advances the reader to the next bit in the bitmap.

func (*BitmapReader) NotSet

func (b *BitmapReader) NotSet() bool

NotSet returns true if the current bit is not set

func (*BitmapReader) Pos

func (b *BitmapReader) Pos() int

Pos returns the current bit position in the bitmap that the reader is looking at

func (*BitmapReader) Set

func (b *BitmapReader) Set() bool

Set returns true if the current bit is set

type BitmapWordReader

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

BitmapWordReader is a reader for bitmaps that reads a word at a time (a word being an 8 byte uint64) and then provides functions to grab the individual trailing bytes after the last word

func NewBitmapWordReader

func NewBitmapWordReader(bitmap []byte, offset, length int) *BitmapWordReader

NewBitmapWordReader sets up a word reader, calculates the number of trailing bits and number of trailing bytes, along with the number of words.

func (*BitmapWordReader) NextTrailingByte

func (bm *BitmapWordReader) NextTrailingByte() (val byte, validBits int)

NextTrailingByte returns the next trailing byte of the bitmap after the last word along with the number of valid bits in that byte. When validBits < 8, that is the last byte.

If the bitmap ends on a byte alignment, then the last byte can also return 8 valid bits. Thus the TrailingBytes function should be used to know how many trailing bytes to read.

func (*BitmapWordReader) NextWord

func (bm *BitmapWordReader) NextWord() uint64

NextWord returns the next full word read from the bitmap, should not be called if Words() is 0 as it will step outside of the bounds of the bitmap slice and panic.

We don't perform the bounds checking in order to improve performance.

func (*BitmapWordReader) TrailingBytes

func (bm *BitmapWordReader) TrailingBytes() int

func (*BitmapWordReader) Words

func (bm *BitmapWordReader) Words() int

type BitmapWordWriter

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

BitmapWordWriter is a bitmap writer for writing a full word at a time (a word being a uint64). After the last full word is written, PutNextTrailingByte can be used to write the remaining trailing bytes.

func NewBitmapWordWriter

func NewBitmapWordWriter(bitmap []byte, start, len int) *BitmapWordWriter

NewBitmapWordWriter initializes a new bitmap word writer which will start writing into the byte slice at bit offset start, expecting to write len bits.

func (*BitmapWordWriter) PutNextTrailingByte

func (bm *BitmapWordWriter) PutNextTrailingByte(b byte, validBits int)

PutNextTrailingByte writes the number of bits indicated by validBits from b to the bitmap.

func (*BitmapWordWriter) PutNextWord

func (bm *BitmapWordWriter) PutNextWord(word uint64)

PutNextWord writes the given word to the bitmap, potentially splitting across two adjacent words.

type BitmapWriter

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

BitmapWriter is a simple writer for writing bitmaps to byte slices

func NewBitmapWriter

func NewBitmapWriter(bitmap []byte, start, length int) *BitmapWriter

NewBitmapWriter returns a sequential bitwise writer that preserves surrounding bit values as it writes.

func (*BitmapWriter) AppendBools

func (b *BitmapWriter) AppendBools(in []bool) int

AppendBools writes a series of booleans to the bitmapwriter and returns the number of remaining bytes left in the buffer for writing.

func (*BitmapWriter) Clear

func (b *BitmapWriter) Clear()

func (*BitmapWriter) Finish

func (b *BitmapWriter) Finish()

Finish flushes the final byte out to the byteslice in case it was not already on a byte aligned boundary.

func (*BitmapWriter) Next

func (b *BitmapWriter) Next()

Next increments the writer to the next bit for writing.

func (*BitmapWriter) Pos

func (b *BitmapWriter) Pos() int

func (*BitmapWriter) Reset

func (b *BitmapWriter) Reset(start, length int)

Reset resets the position and view of the slice to restart writing a bitmap to the same byte slice.

func (*BitmapWriter) Set

func (b *BitmapWriter) Set()

type OptionalBitIndexer

type OptionalBitIndexer struct {
	Bitmap []byte
	Offset int
}

OptionalBitIndexer is a convenience wrapper for getting bits from a bitmap which may or may not be nil.

func (*OptionalBitIndexer) GetBit

func (b *OptionalBitIndexer) GetBit(i int) bool

Jump to

Keyboard shortcuts

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