The generic type parameter T
can be any valid AssemblyScript type.
Creates or restores a persistent vector with a given storage prefix. Always use a unique storage prefix for different collections.
let vec = new PersistentVector<string>("v") // note the prefix must be unique (per NEAR account)
A prefix to use for every key of this vector.
Returns the last element of the vector
let vec = new PersistentVector<string>("v")
// the phrase "the quick brown fox" is well known in English
vec.push("the")
vec.push("quick")
vec.push("brown")
vec.push("fox")
assert(vec.back == "fox", "PersistentVector returned surprising results, time for a break ;)")
The last element of the vector. Asserts that the vector is not empty.
Returns the first element of the vector
// see alias method: front
The first element of the vector. Asserts that the vector is not empty.
Returns the first element of the vector
let vec = new PersistentVector<string>("v")
// the phrase "the quick brown fox" is well known in English
vec.push("the")
vec.push("quick")
vec.push("brown")
vec.push("fox")
assert(vec.front == "the", "PersistentVector returned surprising results, time for a break ;)")
The first element of the vector. Asserts that the vector is not empty.
Checks if the vector is empty
let vec = new PersistentVector<string>("v")
assert(vec.isEmpty)
vec.push("hello world")
assert(!vec.isEmpty)
True if the vector is empty.
Returns the last element of the vector
// see alias method: back
The last element of the vector. Asserts that the vector is not empty.
Checks whether the index is within the range of the vector indices
let vec = new PersistentVector<string>("v")
vec.containsIndex(0) // false
vec.push("hello world")
vec.containsIndex(0) // true
The index to check.
True if the given index is within the range of the vector indices.
Removes the last element from the vector and returns it. Asserts that the vector is not empty. Decreases the length of the vector.
let vec = new PersistentVector<string>("v")
let text = "hello world"
vec.push(text)
vec.length // 1
let element = vec.pop()
vec.length // 0
assert(element == text, "PersistentVector returned surprising results, time for a break ;)")
The removed last element of the vector.
Removes the last element from the vector and returns it. Asserts that the vector is not empty. Decreases the length of the vector.
// see alias method: pop()
The removed last element of the vector.
Adds a new element to the end of the vector. Increases the length of the vector.
let vec = new PersistentVector<string>("v")
vec.length // 0
vec.push("hello world")
vec.length // 1
A new element to add.
The index of a newly added element
Adds a new element to the end of the vector. Increases the length of the vector.
// see alias method: push()
A new element to add.
The index of a newly added element
Inserts the element at index, returns evicted element. Panics if index is out of bounds.
let vec = new PersistentVector<string>("v")
// the phrase "the quick brown fox" is well known in English
vec.push("the")
vec.push("quick")
vec.push("red")
vec.push("fox")
let element = vec.replace(2, "brown")
assert(element == "red", "PersistentVector returned surprising results, time for a break ;)")
assert(vec[2] == "brown", "PersistentVector returned surprising results, time for a break ;)")
The index of the element to replace
The new value of the element to replace
The element that was replaced
Removes an element from the vector and returns it. The removed element is replaced by the last element of the vector. Does not preserve ordering, but is O(1). Panics if index is out of bounds.
let vec = new PersistentVector<string>("v")
// the phrase "the quick brown fox" is well known in English
vec.push("the")
vec.push("quick")
vec.push("red")
vec.push("fox")
vec.push("brown")
let element = vec.swap_remove(2)
assert(element == "red", "PersistentVector returned surprising results, time for a break ;)")
assert(vec[2] == "brown", "PersistentVector returned surprising results, time for a break ;)")
The element that was removed
Generated using TypeDoc
This class is one of several convenience collections built on top of the
Storage
class exposed by the NEAR platform. It implements a vector -- a persistent array.To create a vector
let vec = new PersistentVector<string>("v") // choose a unique prefix per account
To use the vector
IMPORTANT NOTE: Since all data stored on the blockchain is kept in a single key-value store under the contract account, you must always use a unique storage prefix for different collections to avoid data collision.