A variable-sized container that can hold any type. Indexing is 0-based, and
vectors are growable. This module has many native functions.
Verification of modules that use this one uses model functions that are implemented
directly in Boogie. The specification language has built-in functions operations such
as singleton_vector
. There are some helper functions defined here for specifications in other
modules as well.
Note: We did not verify most of the Move functions here because many have loops, requiring loop invariants to prove, and the return on investment didn't seem worth it for these simple 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
reverse_slice
- Function
append
- Function
reverse_append
- Function
trim
- Function
trim_reverse
- Function
is_empty
- Function
contains
- Function
index_of
- Function
find
- Function
insert
- Function
remove
- Function
remove_value
- Function
swap_remove
- Function
for_each
- Function
for_each_reverse
- Function
for_each_ref
- Function
zip
- Function
zip_reverse
- Function
zip_ref
- Function
enumerate_ref
- Function
for_each_mut
- Function
zip_mut
- Function
enumerate_mut
- Function
fold
- Function
foldr
- Function
map_ref
- Function
zip_map_ref
- Function
map
- Function
zip_map
- Function
filter
- Function
partition
- Function
rotate
- Function
rotate_slice
- Function
stable_partition
- Function
any
- Function
all
- Function
destroy
- Function
range
- Function
range_with_step
- Function
slice
- Module Specification
The index into the vector is out of bounds
const EINDEX_OUT_OF_BOUNDS: u64 = 131072;
The index into the vector is out of bounds
const EINVALID_RANGE: u64 = 131073;
The range in slice
is invalid.
const EINVALID_SLICE_RANGE: u64 = 131076;
The step provided in range
is invalid, must be greater than zero.
const EINVALID_STEP: u64 = 131075;
The length of the vectors are not equal.
const EVECTORS_LENGTH_MISMATCH: u64 = 131074;
Create an empty vector.
Return the length of the vector.
Acquire an immutable reference to the i
th element of the vector v
.
Aborts if i
is out of bounds.
Add element e
to the end of the vector v
.
Return a mutable reference to the i
th element in the vector v
.
Aborts if i
is out of bounds.
#[bytecode_instruction]
public fun borrow_mut<Element>(v: &mut vector<Element>, i: u64): &mut Element
Pop an element from the end of vector v
.
Aborts if v
is empty.
Destroy the vector v
.
Aborts if v
is not empty.
#[bytecode_instruction]
public fun destroy_empty<Element>(v: vector<Element>)
Swaps the elements at the i
th and j
th indices in the vector v
.
Aborts if i
or j
is out of bounds.
Return an vector of size one containing element e
.
Reverses the order of the elements in the vector v
in place.
Reverses the order of the elements [left, right) in the vector v
in place.
public fun reverse_slice<Element>(v: &mut vector<Element>, left: u64, right: u64)
Pushes all of the elements of the other
vector into the lhs
vector.
public fun reverse_append<Element>(lhs: &mut vector<Element>, other: vector<Element>)
Trim a vector to a smaller size, returning the evicted elements in order
Trim a vector to a smaller size, returning the evicted elements in reverse order
public fun trim_reverse<Element>(v: &mut vector<Element>, new_len: u64): vector<Element>
Return true
if the vector v
has no elements and false
otherwise.
Return true if e
is in the vector v
.
Return (true, i)
if e
is in the vector v
at index i
.
Otherwise, returns (false, 0)
.
Return (true, i)
if there's an element that matches the predicate. If there are multiple elements that match
the predicate, only the index of the first one is returned.
Otherwise, returns (false, 0)
.
Insert a new element at position 0 <= i <= length, using O(length - i) time. Aborts if out of bounds.
Remove the i
th 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.
Remove the first occurrence of a given value in the vector v
and return it in a vector, shifting all
subsequent elements.
This is O(n) and preserves ordering of elements in the vector.
This returns an empty vector if the value isn't present in the vector.
Note that this cannot return an option as option uses vector and there'd be a circular dependency between option
and vector.
public fun remove_value<Element>(v: &mut vector<Element>, val: &Element): vector<Element>
Swap the i
th 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
Apply the function to each element in the vector, consuming it.
Apply the function to each element in the vector, consuming it.
public fun for_each_reverse<Element>(v: vector<Element>, f: |Element|())
Apply the function to a reference of each element in the vector.
public fun for_each_ref<Element>(v: &vector<Element>, f: |&Element|())
Apply the function to each pair of elements in the two given vectors, consuming them.
public fun zip<Element1, Element2>(v1: vector<Element1>, v2: vector<Element2>, f: |(Element1, Element2)|())
Apply the function to each pair of elements in the two given vectors in the reverse order, consuming them. This errors out if the vectors are not of the same length.
public fun zip_reverse<Element1, Element2>(v1: vector<Element1>, v2: vector<Element2>, f: |(Element1, Element2)|())
Apply the function to the references of each pair of elements in the two given vectors. This errors out if the vectors are not of the same length.
public fun zip_ref<Element1, Element2>(v1: &vector<Element1>, v2: &vector<Element2>, f: |(&Element1, &Element2)|())
Apply the function to a reference of each element in the vector with its index.
public fun enumerate_ref<Element>(v: &vector<Element>, f: |(u64, &Element)|())
Apply the function to a mutable reference to each element in the vector.
public fun for_each_mut<Element>(v: &mut vector<Element>, f: |&mut Element|())
Apply the function to mutable references to each pair of elements in the two given vectors. This errors out if the vectors are not of the same length.
public fun zip_mut<Element1, Element2>(v1: &mut vector<Element1>, v2: &mut vector<Element2>, f: |(&mut Element1, &mut Element2)|())
Apply the function to a mutable reference of each element in the vector with its index.
public fun enumerate_mut<Element>(v: &mut vector<Element>, f: |(u64, &mut Element)|())
Fold the function over the elements. For example, fold(vector[1,2,3], 0, f)
will execute
f(f(f(0, 1), 2), 3)
public fun fold<Accumulator, Element>(v: vector<Element>, init: Accumulator, f: |(Accumulator, Element)|Accumulator): Accumulator
Fold right like fold above but working right to left. For example, fold(vector[1,2,3], 0, f)
will execute
f(1, f(2, f(3, 0)))
public fun foldr<Accumulator, Element>(v: vector<Element>, init: Accumulator, f: |(Element, Accumulator)|Accumulator): Accumulator
Map the function over the references of the elements of the vector, producing a new vector without modifying the original vector.
public fun map_ref<Element, NewElement>(v: &vector<Element>, f: |&Element|NewElement): vector<NewElement>
Map the function over the references of the element pairs of two vectors, producing a new vector from the return values without modifying the original vectors.
public fun zip_map_ref<Element1, Element2, NewElement>(v1: &vector<Element1>, v2: &vector<Element2>, f: |(&Element1, &Element2)|NewElement): vector<NewElement>
Map the function over the elements of the vector, producing a new vector.
Map the function over the element pairs of the two vectors, producing a new vector.
public fun zip_map<Element1, Element2, NewElement>(v1: vector<Element1>, v2: vector<Element2>, f: |(Element1, Element2)|NewElement): vector<NewElement>
Filter the vector using the boolean function, removing all elements for which p(e)
is not true.
Partition, sorts all elements for which pred is true to the front. Preserves the relative order of the elements for which pred is true, BUT NOT for the elements for which pred is false.
rotate(&mut [1, 2, 3, 4, 5], 2) -> [3, 4, 5, 1, 2] in place, returns the split point ie. 3 in the example above
Same as above but on a sub-slice of an array [left, right) with left <= rot <= right returns the
Partition the array based on a predicate p, this routine is stable and thus preserves the relative order of the elements in the two partitions.
public fun stable_partition<Element>(v: &mut vector<Element>, p: |&Element|bool): u64
Return true if any element in the vector satisfies the predicate.
Return true if all elements in the vector satisfy the predicate.
Destroy a vector, just a wrapper around for_each_reverse with a descriptive name when used in the context of destroying a vector.