vector

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: EPL-1.0, EPL-1.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package vector implements persistent vector.

This is a Go clone of Clojure's PersistentVector type (https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentVector.java). For an introduction to the internals, see https://hypirion.com/musings/understanding-persistent-vector-pt-1.

This code has been copied from the package "src.elv.sh/pkg/persistent/vector" with modifications for efficiency.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator interface {
	// Elem returns the element at the current position.
	Elem() interface{}
	// HasElem returns whether the iterator is pointing to an element.
	HasElem() bool
	// Next moves the iterator to the next position.
	Next()
}

Iterator is an iterator over vector elements. It can be used like this:

for it := v.Iterator(); it.HasElem(); it.Next() {
    elem := it.Elem()
    // do something with elem...
}

type Persistent added in v0.7.2

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

func NewPersistent added in v0.7.2

func NewPersistent(elems ...interface{}) Persistent

NewPersistent constructs a persistent vector value. Callers that embed the state in a larger persistent collection can use the value directly and avoid allocating a second wrapper object.

func (*Persistent) Assoc added in v0.7.2

func (v *Persistent) Assoc(i int, val interface{}) Vector

func (Persistent) AssocValue added in v0.7.2

func (v Persistent) AssocValue(i int, val interface{}) (Persistent, bool)

AssocValue returns updated persistent state by value, avoiding a wrapper allocation for callers that embed Persistent directly.

func (*Persistent) Conj added in v0.7.2

func (v *Persistent) Conj(val interface{}) Vector

func (Persistent) ConjValue added in v0.7.2

func (v Persistent) ConjValue(val interface{}) Persistent

ConjValue returns updated persistent state by value, avoiding a wrapper allocation for callers that embed Persistent directly.

func (Persistent) ConjValueInto added in v0.7.2

func (v Persistent) ConjValueInto(val interface{}, storage *TailStorage) Persistent

ConjValueInto returns updated persistent state using caller-owned storage for the newest tail entry. The storage must remain alive as long as the returned vector or a vector derived from it remains reachable.

func (*Persistent) Index added in v0.7.2

func (v *Persistent) Index(i int) (interface{}, bool)

func (*Persistent) Iterator added in v0.7.2

func (v *Persistent) Iterator() Iterator

func (*Persistent) Len added in v0.7.2

func (v *Persistent) Len() int

Count returns the number of elements in a Vector.

func (*Persistent) MarshalJSON added in v0.7.2

func (v *Persistent) MarshalJSON() ([]byte, error)

func (*Persistent) Pop added in v0.7.2

func (v *Persistent) Pop() Vector

func (Persistent) PopValue added in v0.7.2

func (v Persistent) PopValue() (Persistent, bool)

PopValue returns updated persistent state by value, avoiding a wrapper allocation for callers that embed Persistent directly.

func (Persistent) ReplaceLastValueInto added in v0.7.2

func (v Persistent) ReplaceLastValueInto(
	val interface{},
	storage *ReplaceTailStorage,
) (Persistent, bool)

ReplaceLastValueInto returns persistent state with its final value replaced. When the tail ends in a delta, storage holds the replacement delta so the operation does not have to materialize and copy the tail.

func (*Persistent) SubVector added in v0.7.2

func (v *Persistent) SubVector(begin, end int) Vector

type ReplaceTailStorage added in v0.7.2

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

ReplaceTailStorage reserves storage for replacing the last tail entry without copying either a base tail or a delta chain.

type TailStorage added in v0.7.2

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

TailStorage reserves space for one immutable tail append. It lets wrappers that embed Persistent co-allocate their newest tail entry with the wrapper while keeping the tail representation private to this package.

type Transient

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

func NewTransient

func NewTransient(vi Vector) *Transient

NewTransient returns a new transient vector.

func (*Transient) Assoc

func (t *Transient) Assoc(i int, val interface{}) *Transient

func (*Transient) Conj

func (t *Transient) Conj(v interface{}) *Transient

Conj adds an element to the end of the vector.

func (*Transient) Count

func (t *Transient) Count() int

func (*Transient) Index

func (t *Transient) Index(i int) (any, bool)

func (*Transient) Persistent

func (t *Transient) Persistent() *Persistent

persistent returns a persistent vector from the transient vector.

func (*Transient) Pop

func (t *Transient) Pop() *Transient

type Vector

type Vector interface {
	json.Marshaler
	// Len returns the length of the vector.
	Len() int
	// Index returns the i-th element of the vector, if it exists. The second
	// return value indicates whether the element exists.
	Index(i int) (interface{}, bool)
	// Assoc returns an almost identical Vector, with the i-th element
	// replaced. If the index is smaller than 0 or greater than the length of
	// the vector, it returns nil. If the index is equal to the size of the
	// vector, it is equivalent to Conj.
	Assoc(i int, val interface{}) Vector
	// Conj returns an almost identical Vector, with an additional element
	// appended to the end.
	Conj(val interface{}) Vector
	// Pop returns an almost identical Vector, with the last element removed. It
	// returns nil if the vector is already empty.
	Pop() Vector
	// SubVector returns a subvector containing the elements from i up to but
	// not including j.
	SubVector(i, j int) Vector
	// Iterator returns an iterator over the vector.
	Iterator() Iterator
}

Vector is a persistent sequential container for arbitrary values. It supports O(1) lookup by index, modification by index, and insertion and removal operations at the end. Being a persistent variant of the data structure, it is immutable, and provides O(1) operations to create modified versions of the vector that shares the underlying data structure, making it suitable for concurrent access. The empty value is a valid empty vector.

var Empty Vector = &Persistent{}

Empty is an empty Vector.

func New

func New(elems ...interface{}) Vector

New returns a new Vector with the given elements.

Jump to

Keyboard shortcuts

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