# Module `0x1::vector`
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](#@Constants_0)
- [Function `empty`](#0x1_vector_empty)
- [Function `length`](#0x1_vector_length)
- [Function `borrow`](#0x1_vector_borrow)
- [Function `push_back`](#0x1_vector_push_back)
- [Function `borrow_mut`](#0x1_vector_borrow_mut)
- [Function `pop_back`](#0x1_vector_pop_back)
- [Function `destroy_empty`](#0x1_vector_destroy_empty)
- [Function `swap`](#0x1_vector_swap)
- [Function `singleton`](#0x1_vector_singleton)
- [Function `reverse`](#0x1_vector_reverse)
- [Function `find`](#0x1_vector_find)
- [Function `append`](#0x1_vector_append)
- [Function `is_empty`](#0x1_vector_is_empty)
- [Function `contains`](#0x1_vector_contains)
- [Function `index_of`](#0x1_vector_index_of)
- [Function `remove`](#0x1_vector_remove)
- [Function `swap_remove`](#0x1_vector_swap_remove)
- [Function `for_each`](#0x1_vector_for_each)
- [Function `for_each_ref`](#0x1_vector_for_each_ref)
- [Function `for_each_mut`](#0x1_vector_for_each_mut)
- [Function `fold`](#0x1_vector_fold)
- [Function `map`](#0x1_vector_map)
- [Function `filter`](#0x1_vector_filter)
- [Function `move_range`](#0x1_vector_move_range)
- [Module Specification](#@Module_Specification_1)
- [Helper Functions](#@Helper_Functions_2)
## Constants
The index into the vector is out of bounds
const EINDEX_OUT_OF_BOUNDS: u64 = 131072;
## Function `empty`
Create an empty vector.
#[bytecode_instruction]
public fun empty<Element>(): vector<Element>
native public fun empty<Element>(): vector<Element>;
#[bytecode_instruction]
public fun length<Element>(v: &vector<Element>): u64
native public fun length<Element>(v: &vector<Element>): u64;
ith element of the vector v.
Aborts if i is out of bounds.
#[bytecode_instruction]
public fun borrow<Element>(v: &vector<Element>, i: u64): &Element
native public fun borrow<Element>(v: &vector<Element>, i: u64): ∈
e to the end of the vector v.
#[bytecode_instruction]
public fun push_back<Element>(v: &mut vector<Element>, e: Element)
native public fun push_back<Element>(v: &mut vector<Element>, e: Element);
ith 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
native public fun borrow_mut<Element>(v: &mut vector<Element>, i: u64): &mut Element;
v.
Aborts if v is empty.
#[bytecode_instruction]
public fun pop_back<Element>(v: &mut vector<Element>): Element
native public fun pop_back<Element>(v: &mut vector<Element>): Element;
v.
Aborts if v is not empty.
#[bytecode_instruction]
public fun destroy_empty<Element>(v: vector<Element>)
native public fun destroy_empty<Element>(v: vector<Element>);
ith and jth indices in the vector v.
Aborts if i or j is out of bounds.
#[bytecode_instruction]
public fun swap<Element>(v: &mut vector<Element>, i: u64, j: u64)
native public fun swap<Element>(v: &mut vector<Element>, i: u64, j: u64);
e.
public fun singleton<Element>(e: Element): vector<Element>
public fun singleton<Element>(e: Element): vector<Element> {
let v = empty();
push_back(&mut v, e);
v
}
aborts_if false;
ensures result == vec(e);
v in place.
public fun reverse<Element>(v: &mut vector<Element>)
public fun reverse<Element>(v: &mut vector<Element>) {
let len = length(v);
if (len == 0) return;
let front_index = 0;
let back_index = len -1;
while (front_index < back_index) {
swap(v, front_index, back_index);
front_index = front_index + 1;
back_index = back_index - 1;
}
}
pragma intrinsic = true;
public fun find<Element>(v: &vector<Element>, f: |&Element|bool): (bool, u64)
public inline fun find<Element>(v: &vector<Element>, f: |&Element|bool): (bool, u64) {
let find = false;
let found_index = 0;
let i = 0;
let len = length(v);
while ({
spec {
invariant i <= len;
invariant forall j: num where j >= 0 && j < i: !f(borrow(v, j));
invariant find ==> i < len && f(borrow(v, i));
};
i < len
}) {
// Cannot call return in an inline function so we need to resort to break here.
if (f(borrow(v, i))) {
find = true;
found_index = i;
break
};
i = i + 1;
};
spec {
assert !find <==> (forall j: num where j >= 0 && j < len: !f(v[j]));
};
(find, found_index)
}
other vector into the lhs vector.
public fun append<Element>(lhs: &mut vector<Element>, other: vector<Element>)
public fun append<Element>(lhs: &mut vector<Element>, other: vector<Element>) {
reverse(&mut other);
while (!is_empty(&other)) push_back(lhs, pop_back(&mut other));
destroy_empty(other);
}
pragma intrinsic = true;
true if the vector v has no elements and false otherwise.
public fun is_empty<Element>(v: &vector<Element>): bool
public fun is_empty<Element>(v: &vector<Element>): bool {
length(v) == 0
}
pragma intrinsic = true;
e is in the vector v.
Otherwise, returns false.
public fun contains<Element>(v: &vector<Element>, e: &Element): bool
public fun contains<Element>(v: &vector<Element>, e: &Element): bool {
let i = 0;
let len = length(v);
while (i < len) {
if (borrow(v, i) == e) return true;
i = i + 1;
};
false
}
pragma intrinsic = true;
(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)
public fun index_of<Element>(v: &vector<Element>, e: &Element): (bool, u64) {
let i = 0;
let len = length(v);
while (i < len) {
if (borrow(v, i) == e) return (true, i);
i = i + 1;
};
(false, 0)
}
pragma intrinsic = true;
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
public fun remove<Element>(v: &mut vector<Element>, i: u64): Element {
let len = length(v);
// i out of bounds; abort
if (i >= len) abort EINDEX_OUT_OF_BOUNDS;
len = len - 1;
while (i < len) swap(v, i, { i = i + 1; i });
pop_back(v)
}
pragma intrinsic = true;
ith element of the vector v with the last element and then pop the element.
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
public fun swap_remove<Element>(v: &mut vector<Element>, i: u64): Element {
assert!(!is_empty(v), EINDEX_OUT_OF_BOUNDS);
let last_idx = length(v) - 1;
swap(v, i, last_idx);
pop_back(v)
}
pragma intrinsic = true;
public fun for_each<Element>(v: vector<Element>, f: |Element|)
public inline fun for_each<Element>(v: vector<Element>, f: |Element|) {
reverse(&mut v); // We need to reverse the vector to consume it efficiently
while (!is_empty(&v)) {
let e = pop_back(&mut v);
f(e);
};
}
public fun for_each_ref<Element>(v: &vector<Element>, f: |&Element|)
public inline fun for_each_ref<Element>(v: &vector<Element>, f: |&Element|) {
let i = 0;
while (i < length(v)) {
f(borrow(v, i));
i = i + 1
}
}
public fun for_each_mut<Element>(v: &mut vector<Element>, f: |&mut Element|)
public inline fun for_each_mut<Element>(v: &mut vector<Element>, f: |&mut Element|) {
let i = 0;
while (i < length(v)) {
f(borrow_mut(v, i));
i = i + 1
}
}
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
public inline fun fold<Accumulator, Element>(
v: vector<Element>,
init: Accumulator,
f: |Accumulator,Element|Accumulator
): Accumulator {
let accu = init;
for_each(v, |elem| accu = f(accu, elem));
accu
}
public fun map<Element, NewElement>(v: vector<Element>, f: |Element|NewElement): vector<NewElement>
public inline fun map<Element, NewElement>(
v: vector<Element>,
f: |Element|NewElement
): vector<NewElement> {
let result = vector<NewElement>[];
for_each(v, |elem| push_back(&mut result, f(elem)));
result
}
p(e) is not true.
public fun filter<Element: drop>(v: vector<Element>, p: |&Element|bool): vector<Element>
public inline fun filter<Element:drop>(
v: vector<Element>,
p: |&Element|bool
): vector<Element> {
let result = vector<Element>[];
for_each(v, |elem| {
if (p(&elem)) push_back(&mut result, elem);
});
result
}
[removal_position, removal_position + length) from vector from,
to vector to, inserting them starting at the insert_position.
In the from vector, elements after the selected range are moved left to fill the hole
(i.e. range is removed, while the order of the rest of the elements is kept)
In the to vector, elements after the insert_position are moved to the right to make
space for new elements (i.e. range is inserted, while the order of the rest of the
elements is kept).
Move prevents from having two mutable references to the same value, so from and to
vectors are always distinct.
public fun move_range<T>(from: &mut vector<T>, removal_position: u64, length: u64, to: &mut vector<T>, insert_position: u64)
native public fun move_range<T>(
from: &mut vector<T>,
removal_position: u64,
length: u64,
to: &mut vector<T>,
insert_position: u64
);
v1 is equal to the result of adding e at the end of v2
fun eq_push_back<Element>(v1: vector<Element>, v2: vector<Element>, e: Element): bool {
len(v1) == len(v2) + 1 &&
v1[len(v1)-1] == e &&
v1[0..len(v1)-1] == v2[0..len(v2)]
}
Check if v is equal to the result of concatenating v1 and v2
fun eq_append<Element>(v: vector<Element>, v1: vector<Element>, v2: vector<Element>): bool {
len(v) == len(v1) + len(v2) &&
v[0..len(v1)] == v1 &&
v[len(v1)..len(v)] == v2
}
Check v1 is equal to the result of removing the first element of v2
fun eq_pop_front<Element>(v1: vector<Element>, v2: vector<Element>): bool {
len(v1) + 1 == len(v2) &&
v1 == v2[1..len(v2)]
}
Check that v1 is equal to the result of removing the element at index i from v2.
fun eq_remove_elem_at_index<Element>(i: u64, v1: vector<Element>, v2: vector<Element>): bool {
len(v1) + 1 == len(v2) &&
v1[0..i] == v2[0..i] &&
v1[i..len(v1)] == v2[i + 1..len(v2)]
}
[//]: # ("File containing references which can be used from documentation")