bitvector

package module
v0.0.0-...-286a776 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2019 License: Apache-2.0 Imports: 2 Imported by: 1

README

teivah/bitvector

Go Report Card

Overview

A bit vector is an array data structure that compactly stores bits.

This library is based on 5 static different data structures:

  • 8-bit vector: relies on an internal uint8
  • 16-bit vector: relies on an internal uint16
  • 32-bit vector: relies on an internal uint32
  • 64-bit vector: relies on an internal uint64
  • 128-bit vector: relies on two internal uint64 (for ASCII problems)

The rationale of using a static integer compared to a dynamic []byte is first of all to save memory. There is no structure and/or slice overhead. Hence, you might be interested in this library for memory-bound computation.

Also, the operations (get, set, etc.) are way more efficient. A simple benchmark shows that it's about 10 times more efficient than using a byte slice. Moreover, there is a guarantee that the internal bit vectors will not escape to the heap and remain only at the stack level.

Yet, the only drawback is to have a fixed-size bit vector (8, 16, 32, 64 or 128). If you require a dynamic bit vector, you should take a look at dropbox/godropbox for example.

Installation

go get github.com/teivah/bitvector

Documentation

Initialization
  • 8-bit vector:
var bv bitvector.Len8
  • 16-bit vector:
var bv bitvector.Len16
  • 32-bit vector:
var bv bitvector.Len32
  • 64-bit vector:
var bv bitvector.Len64
  • 128-bit vector:
var bv bitvector.Ascii
// Or to reinitialize the bit vector
bv = bitvector.NewAscii()
Operations
  • Set ith bit:
bv = bv.Set(i, true)
bv = bv.Set(i, false)
  • Get ith bit:
b := bv.Get(i) // bool
  • Toggle (flip) ith bit:
bv = bv.Toggle(i)
  • Clear bits from index i (included) to index j (excluded):
bv = bv.Clear(i, j)
  • Count the number of bits set to 1:
i := bv.Count() // uint8
  • And operator:
bv := bv1.And(bv2)
  • Or operator:
bv := bv1.Or(bv2)
  • Xor operator:
bv := bv1.Xor(bv2)
  • AndNot operator:
bv := bv1.AndNot(bv2)
  • Push operator (left shift):
bv = bv.Push(2)
  • Pop operator (right shift):
bv = bv.Pop(2)
  • Convert the internal bit vector structure to a string:
s := bv.String() // string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ascii

type Ascii [2]Len64

Ascii is a 64-bit vector

func NewAscii

func NewAscii() Ascii

func (Ascii) And

func (bv Ascii) And(bv2 Ascii) Ascii

And operator

func (Ascii) AndNot

func (bv Ascii) AndNot(bv2 Ascii) Ascii

AndNot operator

func (Ascii) Clear

func (bv Ascii) Clear(i, j uint8) Ascii

Clear bits from index i (included) to index j (excluded)

func (Ascii) Count

func (bv Ascii) Count() uint8

Count the number of bits set to 1

func (Ascii) Get

func (bv Ascii) Get(i uint8) bool

Get ith bit

func (Ascii) Or

func (bv Ascii) Or(bv2 Ascii) Ascii

Or operator

func (Ascii) Set

func (bv Ascii) Set(i uint8, b bool) Ascii

Set ith bit

func (Ascii) String

func (bv Ascii) String() string

func (Ascii) Toggle

func (bv Ascii) Toggle(i uint8) Ascii

Toggle ith bit

func (Ascii) Xor

func (bv Ascii) Xor(bv2 Ascii) Ascii

Xor operator

type Len16

type Len16 uint16

Len16 is a 16-bit vector

func (Len16) And

func (bv Len16) And(bv2 Len16) Len16

And operator

func (Len16) AndNot

func (bv Len16) AndNot(bv2 Len16) Len16

AndNot operator

func (Len16) Clear

func (bv Len16) Clear(i, j uint8) Len16

Clear bits from index i (included) to index j (excluded)

func (Len16) Count

func (bv Len16) Count() uint8

Count the number of bits set to 1

func (Len16) Get

func (bv Len16) Get(i uint8) bool

Get ith bit

func (Len16) Or

func (bv Len16) Or(bv2 Len16) Len16

Or operator

func (Len16) Pop

func (bv Len16) Pop(i uint8) Len16

Pop right shifts the bits

func (Len16) Push

func (bv Len16) Push(i uint8) Len16

Push left shifts the bits

func (Len16) Set

func (bv Len16) Set(i uint8, b bool) Len16

Set ith bit

func (Len16) String

func (bv Len16) String() string

func (Len16) Toggle

func (bv Len16) Toggle(i uint8) Len16

Toggle ith bit

func (Len16) Xor

func (bv Len16) Xor(bv2 Len16) Len16

Xor operator

type Len32

type Len32 uint32

Len32 is a 32-bit vector

func (Len32) And

func (bv Len32) And(bv2 Len32) Len32

And operator

func (Len32) AndNot

func (bv Len32) AndNot(bv2 Len32) Len32

AndNot operator

func (Len32) Clear

func (bv Len32) Clear(i, j uint8) Len32

Clear bits from index i (included) to index j (excluded)

func (Len32) Count

func (bv Len32) Count() uint8

Count the number of bits set to 1

func (Len32) Get

func (bv Len32) Get(i uint8) bool

Get ith bit

func (Len32) Or

func (bv Len32) Or(bv2 Len32) Len32

Or operator

func (Len32) Pop

func (bv Len32) Pop(i uint8) Len32

Pop right shifts the bits

func (Len32) Push

func (bv Len32) Push(i uint8) Len32

Push left shifts the bits

func (Len32) Set

func (bv Len32) Set(i uint8, b bool) Len32

Set ith bit

func (Len32) String

func (bv Len32) String() string

func (Len32) Toggle

func (bv Len32) Toggle(i uint8) Len32

Toggle ith bit

func (Len32) Xor

func (bv Len32) Xor(bv2 Len32) Len32

Xor operator

type Len64

type Len64 uint64

Len64 is a 64-bit vector

func (Len64) And

func (bv Len64) And(bv2 Len64) Len64

And operator

func (Len64) AndNot

func (bv Len64) AndNot(bv2 Len64) Len64

AndNot operator

func (Len64) Clear

func (bv Len64) Clear(i, j uint8) Len64

Clear bits from index i (included) to index j (excluded)

func (Len64) Count

func (bv Len64) Count() uint8

Count the number of bits set to 1

func (Len64) Get

func (bv Len64) Get(i uint8) bool

Get ith bit

func (Len64) Or

func (bv Len64) Or(bv2 Len64) Len64

Or operator

func (Len64) Pop

func (bv Len64) Pop(i uint8) Len64

Pop right shifts the bits

func (Len64) Push

func (bv Len64) Push(i uint8) Len64

Push left shifts the bits

func (Len64) Set

func (bv Len64) Set(i uint8, b bool) Len64

Set ith bit

func (Len64) String

func (bv Len64) String() string

func (Len64) Toggle

func (bv Len64) Toggle(i uint8) Len64

Toggle ith bit

func (Len64) Xor

func (bv Len64) Xor(bv2 Len64) Len64

Xor operator

type Len8

type Len8 uint8

Len8 is a 8-bit vector

func (Len8) And

func (bv Len8) And(bv2 Len8) Len8

And operator

func (Len8) AndNot

func (bv Len8) AndNot(bv2 Len8) Len8

AndNot operator

func (Len8) Clear

func (bv Len8) Clear(i, j uint8) Len8

Clear bits from index i (included) to index j (excluded)

func (Len8) Count

func (bv Len8) Count() uint8

Count the number of bits set to 1

func (Len8) Get

func (bv Len8) Get(i uint8) bool

Get ith bit

func (Len8) Or

func (bv Len8) Or(bv2 Len8) Len8

Or operator

func (Len8) Pop

func (bv Len8) Pop(i uint8) Len8

Pop right shifts the bits

func (Len8) Push

func (bv Len8) Push(i uint8) Len8

Push left shifts the bits

func (Len8) Set

func (bv Len8) Set(i uint8, b bool) Len8

Set ith bit

func (Len8) String

func (bv Len8) String() string

func (Len8) Toggle

func (bv Len8) Toggle(i uint8) Len8

Toggle ith bit

func (Len8) Xor

func (bv Len8) Xor(bv2 Len8) Len8

Xor operator

Jump to

Keyboard shortcuts

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