Module std::vector
A variable-sized container that can hold any type. Indexing is 0-based, and vectors are growable. This module has many native functions.
- Constants
- Function empty
- Function length
- Function borrow
- Function push_back
- Function borrow_mut
- Function pop_back
- Function destroy_empty
- Function swap
- Function singleton
- Function reverse
- Function append
- Function is_empty
- Function contains
- Function index_of
- Function remove
- Function insert
- Function swap_remove
- Function skip
- Function take
- Macro function tabulate
- Macro function destroy
- Macro function do
- Macro function do_ref
- Macro function do_mut
- Macro function map
- Macro function map_ref
- Macro function filter
- Macro function partition
- Macro function find_index
- Macro function find_indices
- Macro function count
- Macro function fold
- Function flatten
- Macro function any
- Macro function all
- Macro function zip_do
- Macro function zip_do_reverse
- Macro function zip_do_ref
- Macro function zip_do_mut
- Macro function zip_map
- Macro function zip_map_ref
- Macro function insertion_sort_by
- Macro function merge_sort_by
- Macro function is_sorted_by
- Macro function take_while
- Macro function skip_while
Constants
The index into the vector is out of bounds
const EINDEX_OUT_OF_BOUNDS: u64 = 131072;
Function empty
Create an empty vector.
public fun empty<Element>(): vector<Element>
Function length
Return the length of the vector.
public fun length<Element>(v: &vector<Element>): u64
Function borrow
Acquire an immutable reference to the ith element of the vector v.
Aborts if i is out of bounds.
public fun borrow<Element>(v: &vector<Element>, i: u64): &Element
Function push_back
Add element e to the end of the vector v.
public fun push_back<Element>(v: &mut vector<Element>, e: Element)
Function borrow_mut
Return a mutable reference to the ith element in the vector v.
Aborts if i is out of bounds.
public fun borrow_mut<Element>(v: &mut vector<Element>, i: u64): &mut Element
Implementation
public native fun borrow_mut<Element>(v: &mut vector<Element>, i: u64): &mut Element;
Function pop_back
Pop an element from the end of vector v.
Aborts if v is empty.
public fun pop_back<Element>(v: &mut vector<Element>): Element
Function destroy_empty
Destroy the vector v.
Aborts if v is not empty.
public fun destroy_empty<Element>(v: vector<Element>)
Implementation
public native fun destroy_empty<Element>(v: vector<Element>);
Function swap
Swaps the elements at the ith and jth indices in the vector v.
Aborts if i or j is out of bounds.
public fun swap<Element>(v: &mut vector<Element>, i: u64, j: u64)
Function singleton
Return an vector of size one containing element e.
public fun singleton<Element>(e: Element): vector<Element>
Function reverse
Reverses the order of the elements in the vector v in place.
public fun reverse<Element>(v: &mut vector<Element>)
Implementation
Function append
Pushes all of the elements of the other vector into the lhs vector.
public fun append<Element>(lhs: &mut vector<Element>, other: vector<Element>)
Function is_empty
Return
true if the vector v has no elements and false otherwise.
public fun is_empty<Element>(v: &vector<Element>): bool
Function contains
Return true if e is in the vector v.
Otherwise, returns false.
public fun contains<Element>(v: &vector<Element>, e: &Element): bool
Function index_of
Return
(true, i) if e is in the vector v at index i.
Otherwise, returns (false, 0).
public fun index_of<Element>(v: &vector<Element>, e: &Element): (bool, u64)
Function remove
Remove the ith element of the vector v, shifting all subsequent elements.
This is O(n) and preserves ordering of elements in the vector.
Aborts if i is out of bounds.
public fun remove<Element>(v: &mut vector<Element>, i: u64): Element
Function insert
Insert e at position i in the vector v.
If i is in bounds, this shifts the old v[i] and all subsequent elements to the right.
If
i == v.length(), this adds e to the end of the vector.
This is O(n) and preserves ordering of elements in the vector.
Aborts if i > v.length()
public fun insert<Element>(v: &mut vector<Element>, e: Element, i: u64)
Function swap_remove
Swap the ith element of the vector v with the last element and then pop the vector.
This is O(1), but does not preserve ordering of elements in the vector.
Aborts if i is out of bounds.
public fun swap_remove<Element>(v: &mut vector<Element>, i: u64): Element
Implementation
public fun swap_remove<Element>(v: &mut vector<Element>, i: u64): Element {
assert!(v.length() != 0, EINDEX_OUT_OF_BOUNDS);
let last_idx = v.length() - 1;
v.swap(i, last_idx);
v.pop_back()
}
Function skip
Return a new vector containing the elements of v except the first n elements.
If
n > length, returns an empty vector.
public fun skip<T: drop>(v: vector<T>, n: u64): vector<T>
Function take
Take the first n elements of the vector v and drop the rest.
Aborts if n is greater than the length of v.
public fun take<T: drop>(v: vector<T>, n: u64): vector<T>
Macro function tabulate
Create a vector of length n by calling the function f on each index.
public macro fun tabulate<$T>($n: u64, $f: |u64| -> $T): vector<$T>
Macro function destroy
Destroy the vector v by calling f on each element and then destroying the vector.
Does not preserve the order of elements in the vector (starts from the end of the vector).
public macro fun destroy<$T, $R: drop>($v: vector<$T>, $f: |$T| -> $R)
Macro function do
Destroy the vector v by calling f on each element and then destroying the vector.
Preserves the order of elements in the vector.
public macro fun do<$T, $R: drop>($v: vector<$T>, $f: |$T| -> $R)
Macro function do_ref
Perform an action f on each element of the vector v. The vector is not modified.
public macro fun do_ref<$T, $R: drop>($v: &vector<$T>, $f: |&$T| -> $R)
Macro function do_mut
Perform an action f on each element of the vector v.
The function f takes a mutable reference to the element.
public macro fun do_mut<$T, $R: drop>($v: &mut vector<$T>, $f: |&mut $T| -> $R)
Macro function map
Map the vector v to a new vector by applying the function f to each element.
Preserves the order of elements in the vector, first is called first.
public macro fun map<$T, $U>($v: vector<$T>, $f: |$T| -> $U): vector<$U>
Macro function map_ref
Map the vector v to a new vector by applying the function f to each element.
Preserves the order of elements in the vector, first is called first.
public macro fun map_ref<$T, $U>($v: &vector<$T>, $f: |&$T| -> $U): vector<$U>
Macro function filter
Filter the vector v by applying the function f to each element.
Return a new vector containing only the elements for which f returns
true.
public macro fun filter<$T: drop>($v: vector<$T>, $f: |&$T| -> bool): vector<$T>
Macro function partition
Split the vector v into two vectors by applying the function f to each element.
Return a tuple containing two vectors: the first containing the elements for which f returns
true,
and the second containing the elements for which f returns false.
public macro fun partition<$T>($v: vector<$T>, $f: |&$T| -> bool): (vector<$T>, vector<$T>)
Macro function find_index
Finds the index of first element in the vector v that satisfies the predicate f.
Returns some(index) if such an element is found, otherwise none().
public macro fun find_index<$T>($v: &vector<$T>, $f: |&$T| -> bool): std::option::Option<u64>
Implementation
public macro fun find_index<$T>($v: &vector<$T>, $f: |&$T| -> bool): Option<u64> {
let v = $v;
'find_index: {
v.length().do!(|i| if ($f(&v[i])) return 'find_index option::some(i));
option::none()
}
}
Macro function find_indices
Finds all indices of elements in the vector v that satisfy the predicate f.
Returns a vector of indices of all found elements.
public macro fun find_indices<$T>($v: &vector<$T>, $f: |&$T| -> bool): vector<u64>
Macro function count
Count how many elements in the vector v satisfy the predicate f.
public macro fun count<$T>($v: &vector<$T>, $f: |&$T| -> bool): u64
Macro function fold
Reduce the vector v to a single value by applying the function f to each element.
Similar to fold_left in Rust and reduce in Python and JavaScript.
public macro fun fold<$T, $Acc>($v: vector<$T>, $init: $Acc, $f: |$Acc, $T| -> $Acc): $Acc