bitutil

package
v9.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 8 more Imports: 7 Imported by: 16

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 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 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.

Types

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()

Jump to

Keyboard shortcuts

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