sparse

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package sparse implements a special sparse array with popcount compression for max. 256 items.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array256

type Array256[T any] struct {
	bitset.BitSet256
	Items []T
}

Array256 is a generic implementation of a sparse array with popcount compression for max. 256 items with payload T.

func (*Array256[T]) Clear

func (a *Array256[T]) Clear(uint)

Clear of the underlying bitset is forbidden. The bitset and the items are coupled. An unsynchronized Clear() disturbs the coupling between bitset and Items[].

func (*Array256[T]) Copy

func (a *Array256[T]) Copy() *Array256[T]

Copy returns a shallow copy of the Array. The elements are copied using assignment, this is no deep clone.

func (*Array256[T]) DeleteAt

func (a *Array256[T]) DeleteAt(i uint8) (value T, exists bool)

DeleteAt a value at i from the sparse array, zeroes the tail.

func (*Array256[T]) Get

func (a *Array256[T]) Get(i uint8) (value T, ok bool)

Get the value at i from sparse array.

example: a.Get(5) -> a.Items[1]

                        ⬇
BitSet256:   [0|0|1|0|0|1|0|...|1] <- 3 bits set
Items:       [*|*|*]               <- len(Items) = 3
                ⬆

BitSet256.Test(5):     true
BitSet256.Rank(5):     2,

func (*Array256[T]) InsertAt

func (a *Array256[T]) InsertAt(i uint8, value T) (exists bool)

InsertAt a value at i into the sparse array. If the value already exists, overwrite it with val and return true.

func (*Array256[T]) Len

func (a *Array256[T]) Len() int

Len returns the number of items in sparse array.

func (*Array256[T]) MustGet

func (a *Array256[T]) MustGet(i uint8) T

MustGet use it only after a successful test or the behavior is undefined, it will NOT PANIC.

func (*Array256[T]) Set

func (a *Array256[T]) Set(uint)

Set of the underlying bitset is forbidden. The bitset and the items are coupled. An unsynchronized Set() disturbs the coupling between bitset and Items[].

func (*Array256[T]) UpdateAt

func (a *Array256[T]) UpdateAt(i uint8, cb func(T, bool) T) (newValue T, wasPresent bool)

UpdateAt or set the value at i via callback. The new value is returned and true if the value was already present.

Jump to

Keyboard shortcuts

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