bitarray

package module
v0.0.0-...-119a84a Latest Latest
Warning

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

Go to latest
Published: May 27, 2020 License: MIT Imports: 3 Imported by: 0

README

BitArray

A bit array (also known as bit map, bit set, bit string, or bit vector) is an array data structure that compactly stores bits.

Installation

go get github.com/aermolaev/bitarray

Simple Example

package main

import (
	"fmt"

	"github.com/aermolaev/bitarray"
)

func main() {
	b := bitarray.NewBitArray(1_000_000)

	b.Mark(4000)
	fmt.Println(b.Get(4000)) // true

	b.Unmark(4000)
	fmt.Println(b.Get(4000)) // false

	i := b.MarkFree()        // 0
	fmt.Println(i)           // true
	fmt.Println(b.Get(i))    // true
	fmt.Println(b.IsEmpty()) // false
	fmt.Println(b.HasRoom()) // true
}

Documentation

Index

Constants

View Source
const (
	BitBlockNotFound = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BitArray

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

BitArray array of binary values.

func NewBitArray

func NewBitArray(capacity int64) *BitArray

NewBitArray creates and initializes a new BitArray using capacity as its initial capacity.

func (*BitArray) Cap

func (b *BitArray) Cap() int

Cap returns the BitArray capacity, that is, the total bits allocated for the data.

func (*BitArray) Get

func (b *BitArray) Get(index int64) (res bool)

Get returns the value of the bit with the specified index.

func (*BitArray) HasRoom

func (b *BitArray) HasRoom() bool

HasRoom reports true if this BitArray contains bits that are set to true.

func (*BitArray) IsEmpty

func (b *BitArray) IsEmpty() bool

IsEmpty reports true if this BitArray contains no bits that are set to true.

func (*BitArray) Len

func (b *BitArray) Len() int

Len returns the number of occupied bits.

func (*BitArray) Mark

func (b *BitArray) Mark(index int64)

Mark sets the bit at the specified index to true.

func (*BitArray) MarkFree

func (b *BitArray) MarkFree() (index int64)

MarkFree finds the index of the first bit that is set to false and sets the bit to true. Returns index of changed bit. Returns BitBlockNotFound unless array has room.

func (*BitArray) Reset

func (b *BitArray) Reset()

Reset resets BitArray to initial state.

func (*BitArray) Set

func (b *BitArray) Set(index int64, mark bool) (changed bool)

Set sets the bit at the specified index to the specified value.

func (*BitArray) Unmark

func (b *BitArray) Unmark(index int64)

Unmark sets the bit at the specified index to false.

type BitBlock

type BitBlock uint64

Jump to

Keyboard shortcuts

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