{"modulePath":"github.com/glojurelang/glojure","version":"v0.7.25","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package vector\n\nPackage vector implements persistent vector.\n\nThis is a Go clone of Clojure's PersistentVector type ([https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentVector.java](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](https://hypirion.com/musings/understanding-persistent-vector-pt-1).\n\nThis code has been copied from the package \"src.elv.sh/pkg/persistent/vector\" with modifications for efficiency.\n\n## Types\n\n```go\ntype InlineTailStorage struct {\n\t// contains filtered or unexported fields\n}\n```\nInlineTailStorage allows a collection wrapper to own its immutable tail descriptor and values in the same allocation. Values must not be mutated.\n\n```go\ntype Iterator interface {\n\t// Elem returns the element at the current position.\n\tElem() interface{}\n\t// HasElem returns whether the iterator is pointing to an element.\n\tHasElem() bool\n\t// Next moves the iterator to the next position.\n\tNext()\n}\n```\nIterator is an iterator over vector elements. It can be used like this:\n\n\tfor it := v.Iterator(); it.HasElem(); it.Next() {\n\t    elem := it.Elem()\n\t    // do something with elem...\n\t}\n\n```go\ntype Persistent struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc Build(n int, at func(int) interface{}) Persistent\n```\nBuild 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.\n\n```go\nfunc InitInlineTail(storage *InlineTailStorage, values []any) Persistent\n```\n\n```go\nfunc NewPersistent(elems ...interface{}) Persistent\n```\nNewPersistent 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.\n\n```go\nfunc (v *Persistent) Assoc(i int, val interface{}) Vector\n```\n\n```go\nfunc (v Persistent) AssocValue(i int, val interface{}) (Persistent, bool)\n```\nAssocValue returns updated persistent state by value, avoiding a wrapper allocation for callers that embed Persistent directly.\n\n```go\nfunc (v *Persistent) Conj(val interface{}) Vector\n```\n\n```go\nfunc (v Persistent) ConjValue(val interface{}) Persistent\n```\nConjValue returns updated persistent state by value, avoiding a wrapper allocation for callers that embed Persistent directly.\n\n```go\nfunc (v Persistent) ConjValueInto(val interface{}, storage *TailStorage) Persistent\n```\nConjValueInto 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.\n\n```go\nfunc (v *Persistent) Index(i int) (interface{}, bool)\n```\n\n```go\nfunc (v *Persistent) Iterator() Iterator\n```\n\n```go\nfunc (v *Persistent) Len() int\n```\nCount returns the number of elements in a Vector.\n\n```go\nfunc (v *Persistent) MarshalJSON() ([]byte, error)\n```\n\n```go\nfunc (v *Persistent) Pop() Vector\n```\n\n```go\nfunc (v Persistent) PopValue() (Persistent, bool)\n```\nPopValue returns updated persistent state by value, avoiding a wrapper allocation for callers that embed Persistent directly.\n\n```go\nfunc (v Persistent) ReplaceLastValueInto(\n\tval interface{},\n\tstorage *ReplaceTailStorage,\n) (Persistent, bool)\n```\nReplaceLastValueInto 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.\n\n```go\nfunc (v *Persistent) SubVector(begin, end int) Vector\n```\n\n```go\ntype ReplaceTailStorage struct {\n\t// contains filtered or unexported fields\n}\n```\nReplaceTailStorage reserves storage for replacing the last tail entry without copying either a base tail or a delta chain.\n\n```go\ntype TailStorage struct {\n\t// contains filtered or unexported fields\n}\n```\nTailStorage 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.\n\n```go\ntype Transient struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc NewTransient(vi Vector) *Transient\n```\nNewTransient returns a new transient vector.\n\n```go\nfunc (t *Transient) Assoc(i int, val interface{}) *Transient\n```\n\n```go\nfunc (t *Transient) Conj(v interface{}) *Transient\n```\nConj adds an element to the end of the vector.\n\n```go\nfunc (t *Transient) Count() int\n```\n\n```go\nfunc (t *Transient) Index(i int) (any, bool)\n```\n\n```go\nfunc (t *Transient) Persistent() *Persistent\n```\npersistent returns a persistent vector from the transient vector.\n\n```go\nfunc (t *Transient) Pop() *Transient\n```\n\n```go\ntype Vector interface {\n\tjson.Marshaler\n\t// Len returns the length of the vector.\n\tLen() int\n\t// Index returns the i-th element of the vector, if it exists. The second\n\t// return value indicates whether the element exists.\n\tIndex(i int) (interface{}, bool)\n\t// Assoc returns an almost identical Vector, with the i-th element\n\t// replaced. If the index is smaller than 0 or greater than the length of\n\t// the vector, it returns nil. If the index is equal to the size of the\n\t// vector, it is equivalent to Conj.\n\tAssoc(i int, val interface{}) Vector\n\t// Conj returns an almost identical Vector, with an additional element\n\t// appended to the end.\n\tConj(val interface{}) Vector\n\t// Pop returns an almost identical Vector, with the last element removed. It\n\t// returns nil if the vector is already empty.\n\tPop() Vector\n\t// SubVector returns a subvector containing the elements from i up to but\n\t// not including j.\n\tSubVector(i, j int) Vector\n\t// Iterator returns an iterator over the vector.\n\tIterator() Iterator\n}\n```\nVector 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.\n\n```go\nvar Empty Vector = \u0026Persistent{}\n```\nEmpty is an empty Vector.\n\n```go\nfunc New(elems ...interface{}) Vector\n```\nNew returns a new Vector with the given elements.\n\n","path":"github.com/glojurelang/glojure/internal/persistent/vector","name":"vector","synopsis":"Package vector implements persistent vector.","isRedistributable":true}
