bitvector

package module
v0.0.0-...-21f2e7c Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2023 License: MIT Imports: 1 Imported by: 0

README

Bitvector library written in go

This library contains a simple bitvector implementation. Additionaly there are methods for efficient rank queries and naive select queries.

Complexity

Time (Query) Time (Construction) Space
Bitvector O(1) O(n) o(n)
Rank.Rank0 O(1) O(n) o(n)
Rank.Rank1 O(1) O(n) o(n)
Rank0Once O(n) - -
Rank1Once O(n) - -
Select0Once O(n) - -
Select1Once O(n) - -

Example

package main

import (
	"fmt"

	"github.com/Atgoogat/bitvector"
)

func main() {
	bv := bitvector.NewBitvector(10)

	for i := 0; i < 10; i += 2 {
		bv.Set(i) // set every second bit
	}

	rank := bitvector.NewRank(bv)

	for i := 0; i <= 10; i += 1 {
		fmt.Println(rank.Rank0(i))
	}
}

// Output:
// 0
// 0
// 1
// 1
// 2
// 2
// 3
// 3
// 4
// 4
// 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Rank0Once

func Rank0Once(bv ReadonlyBitvector, index int) int

Return how many zeros there are before index. For index = 0 => 0 Complexity: O(n) (n = index) Consider using Rank.Rank0 if you do multiple requests

func Rank1Once

func Rank1Once(bv ReadonlyBitvector, index int) int

Return how many ones there are before index. For index = 0 => 0 Complexity: O(n) (n = index) Consider using Rank.Rank1 if you do multiple requests

func Select0Once

func Select0Once(bv ReadonlyBitvector, ith int) (index int)

Return the index where the ith-zero is. When there is not ith-zero bv.Len() will be returned. Complexity: O(n)

func Select1Once

func Select1Once(bv ReadonlyBitvector, ith int) (index int)

Return the index where the ith-one is. When there is not ith-one bv.Len() will be returned. Complexity: O(n)

Types

type Bitvector

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

func NewBitvector

func NewBitvector(size int) Bitvector

Create a new fixed size bitvector

func (*Bitvector) Clear

func (bv *Bitvector) Clear(index int)

Clear bit at index

func (Bitvector) Get

func (bv Bitvector) Get(index int) bool

Get bit at index

func (Bitvector) GetWord

func (bv Bitvector) GetWord(index int) uint64

Get word at index (word index)

func (Bitvector) Len

func (bv Bitvector) Len() int

Length (in bits)

func (Bitvector) LenWords

func (bv Bitvector) LenWords() int

Length of underlying words

func (Bitvector) Popcount

func (bv Bitvector) Popcount() int

func (*Bitvector) Set

func (bv *Bitvector) Set(index int)

Set bit at index

type Rank

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

Datastructure to allow for fast rank queries

func NewRank

func NewRank(bv ReadonlyBitvector) Rank

Build rank datastructure for this bitvector

Caution: Any changes to the underlying bitvector lead to undefined behaviour regarding the rank queries Complexity: O(n) (n == bv.Len())

func (Rank) Rank0

func (r Rank) Rank0(index int) (rank int)

Returns how many zeros there are before index. For index = 0 => 0 Complexity: O(1)

func (Rank) Rank1

func (r Rank) Rank1(index int) (rank int)

Return how many ones there are before index. For index = 0 => 0 Complexity: O(1)

type ReadonlyBitvector

type ReadonlyBitvector interface {
	Get(index int) bool
	GetWord(index int) uint64
	Len() int
	LenWords() int
}

ReadonlyInterface for Bitvector

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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