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 ¶
- type Iterator
- type Persistent
- func (v *Persistent) Assoc(i int, val interface{}) Vector
- func (v Persistent) AssocValue(i int, val interface{}) (Persistent, bool)
- func (v *Persistent) Conj(val interface{}) Vector
- func (v Persistent) ConjValue(val interface{}) Persistent
- func (v Persistent) ConjValueInto(val interface{}, storage *TailStorage) Persistent
- func (v *Persistent) Index(i int) (interface{}, bool)
- func (v *Persistent) Iterator() Iterator
- func (v *Persistent) Len() int
- func (v *Persistent) MarshalJSON() ([]byte, error)
- func (v *Persistent) Pop() Vector
- func (v Persistent) PopValue() (Persistent, bool)
- func (v Persistent) ReplaceLastValueInto(val interface{}, storage *ReplaceTailStorage) (Persistent, bool)
- func (v *Persistent) SubVector(begin, end int) Vector
- type ReplaceTailStorage
- type TailStorage
- type Transient
- type Vector
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 Build ¶ added in v0.7.15
func Build(n int, at func(int) interface{}) Persistent
Build returns a persistent vector of n elements, reading element i from at(i). It fills the leaf nodes and the tail directly and links the tree bottom up, so bulk construction writes each element once instead of cloning the root path on every full tail as Conj does.
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 ¶
NewTransient returns a new transient vector.
func (*Transient) Persistent ¶
func (t *Transient) Persistent() *Persistent
persistent returns a persistent vector from the transient vector.
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.