Options
All
  • Public
  • Public/Protected
  • All
Menu

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

vec.push(value)
vec.pop(value)
vec.length

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.

Type parameters

Hierarchy

  • PersistentVector

Index

Constructors

constructor

  • 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)
    

    Parameters

    • prefix: string

      A prefix to use for every key of this vector.

    Returns PersistentVector

Accessors

back

  • get back(): T
  • 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 ;)")
    

    Returns T

    The last element of the vector. Asserts that the vector is not empty.

first

  • get first(): T

front

  • get front(): T
  • 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 ;)")
    

    Returns T

    The first element of the vector. Asserts that the vector is not empty.

isEmpty

  • get isEmpty(): bool

last

  • get last(): T

Methods

containsIndex

  • containsIndex(index: i32): bool
  • 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
    

    Parameters

    • index: i32

      The index to check.

    Returns bool

    True if the given index is within the range of the vector indices.

pop

  • pop(): T
  • 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 ;)")
    

    Returns T

    The removed last element of the vector.

popBack

  • popBack(): T
  • 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()
    

    Returns T

    The removed last element of the vector.

push

  • push(element: T): i32
  • 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
    

    Parameters

    • element: T

      A new element to add.

    Returns i32

    The index of a newly added element

pushBack

  • pushBack(element: T): i32
  • Adds a new element to the end of the vector. Increases the length of the vector.

    // see alias method: push()
    

    Parameters

    • element: T

      A new element to add.

    Returns i32

    The index of a newly added element

replace

  • replace(index: i32, new_element: T): T
  • 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 ;)")
    

    Parameters

    • index: i32

      The index of the element to replace

    • new_element: T

      The new value of the element to replace

    Returns T

    The element that was replaced

swap_remove

  • swap_remove(index: i32): T
  • 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 ;)")
    

    Parameters

    • index: i32

    Returns T

    The element that was removed

Generated using TypeDoc