vector

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2024 License: EPL-1.0, EPL-1.0 Imports: 5 Imported by: 0

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 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() *vector

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 = &vector{}

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