language/move-stdlib/docs/Vector.md
<a name="0x1_Vector"></a>
0x1::VectorA 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 <code>singleton_vector</code>. 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.
emptylengthborrowpush_backborrow_mutpop_backdestroy_emptyswapsingletonreverseappendis_emptycontainsindex_ofremoveswap_remove<a name="@Constants_0"></a>
<a name="0x1_Vector_EINDEX_OUT_OF_BOUNDS"></a>
The index into the vector is out of bounds
<pre><code><b>const</b> <a href="Vector.md#0x1_Vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>: u64 = 0; </code></pre><a name="0x1_Vector_empty"></a>
emptyCreate an empty vector.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_empty">empty</a><Element>(): vector<Element> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_empty">empty</a><Element>(): vector<Element>; </code></pre> </details><a name="0x1_Vector_length"></a>
lengthReturn the length of the vector.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_length">length</a><Element>(v: &vector<Element>): u64 </code></pre> <details> <summary>Implementation</summary> <pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_length">length</a><Element>(v: &vector<Element>): u64; </code></pre> </details><a name="0x1_Vector_borrow"></a>
borrowAcquire an immutable reference to the <code>i</code>th element of the vector <code>v</code>. Aborts if <code>i</code> is out of bounds.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_borrow">borrow</a><Element>(v: &vector<Element>, i: u64): &Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_borrow">borrow</a><Element>(v: &vector<Element>, i: u64): ∈ </code></pre> </details><a name="0x1_Vector_push_back"></a>
push_backAdd element <code>e</code> to the end of the vector <code>v</code>.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_push_back">push_back</a><Element>(v: &<b>mut</b> vector<Element>, e: Element) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_push_back">push_back</a><Element>(v: &<b>mut</b> vector<Element>, e: Element); </code></pre> </details><a name="0x1_Vector_borrow_mut"></a>
borrow_mutReturn a mutable reference to the <code>i</code>th element in the vector <code>v</code>. Aborts if <code>i</code> is out of bounds.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_borrow_mut">borrow_mut</a><Element>(v: &<b>mut</b> vector<Element>, i: u64): &<b>mut</b> Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_borrow_mut">borrow_mut</a><Element>(v: &<b>mut</b> vector<Element>, i: u64): &<b>mut</b> Element; </code></pre> </details><a name="0x1_Vector_pop_back"></a>
pop_backPop an element from the end of vector <code>v</code>. Aborts if <code>v</code> is empty.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_pop_back">pop_back</a><Element>(v: &<b>mut</b> vector<Element>): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_pop_back">pop_back</a><Element>(v: &<b>mut</b> vector<Element>): Element; </code></pre> </details><a name="0x1_Vector_destroy_empty"></a>
destroy_emptyDestroy the vector <code>v</code>. Aborts if <code>v</code> is not empty.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_destroy_empty">destroy_empty</a><Element>(v: vector<Element>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_destroy_empty">destroy_empty</a><Element>(v: vector<Element>); </code></pre> </details><a name="0x1_Vector_swap"></a>
swapSwaps the elements at the <code>i</code>th and <code>j</code>th indices in the vector <code>v</code>. Aborts if <code>i</code>or <code>j</code> is out of bounds.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_swap">swap</a><Element>(v: &<b>mut</b> vector<Element>, i: u64, j: u64) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_swap">swap</a><Element>(v: &<b>mut</b> vector<Element>, i: u64, j: u64); </code></pre> </details><a name="0x1_Vector_singleton"></a>
singletonReturn an vector of size one containing element <code>e</code>.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_singleton">singleton</a><Element>(e: Element): vector<Element> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_singleton">singleton</a><Element>(e: Element): vector<Element> { <b>let</b> v = <a href="Vector.md#0x1_Vector_empty">empty</a>(); <a href="Vector.md#0x1_Vector_push_back">push_back</a>(&<b>mut</b> v, e); v } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>aborts_if</b> <b>false</b>; <b>ensures</b> result == vec(e); </code></pre> </details><a name="0x1_Vector_reverse"></a>
reverseReverses the order of the elements in the vector <code>v</code> in place.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_reverse">reverse</a><Element>(v: &<b>mut</b> vector<Element>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_reverse">reverse</a><Element>(v: &<b>mut</b> vector<Element>) { <b>let</b> len = <a href="Vector.md#0x1_Vector_length">length</a>(v); <b>if</b> (len == 0) <b>return</b> (); <b>let</b> front_index = 0; <b>let</b> back_index = len -1; <b>while</b> (front_index < back_index) { <a href="Vector.md#0x1_Vector_swap">swap</a>(v, front_index, back_index); front_index = front_index + 1; back_index = back_index - 1; } } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> intrinsic = <b>true</b>; </code></pre> </details><a name="0x1_Vector_append"></a>
appendPushes all of the elements of the <code>other</code> vector into the <code>lhs</code> vector.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_append">append</a><Element>(lhs: &<b>mut</b> vector<Element>, other: vector<Element>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_append">append</a><Element>(lhs: &<b>mut</b> vector<Element>, other: vector<Element>) { <a href="Vector.md#0x1_Vector_reverse">reverse</a>(&<b>mut</b> other); <b>while</b> (!<a href="Vector.md#0x1_Vector_is_empty">is_empty</a>(&other)) <a href="Vector.md#0x1_Vector_push_back">push_back</a>(lhs, <a href="Vector.md#0x1_Vector_pop_back">pop_back</a>(&<b>mut</b> other)); <a href="Vector.md#0x1_Vector_destroy_empty">destroy_empty</a>(other); } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> intrinsic = <b>true</b>; </code></pre> </details><a name="0x1_Vector_is_empty"></a>
is_emptyReturn <code><b>true</b></code> if the vector <code>v</code> has no elements and <code><b>false</b></code> otherwise.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_is_empty">is_empty</a><Element>(v: &vector<Element>): bool </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_is_empty">is_empty</a><Element>(v: &vector<Element>): bool { <a href="Vector.md#0x1_Vector_length">length</a>(v) == 0 } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> intrinsic = <b>true</b>; </code></pre> </details><a name="0x1_Vector_contains"></a>
containsReturn true if <code>e</code> is in the vector <code>v</code>.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_contains">contains</a><Element>(v: &vector<Element>, e: &Element): bool </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_contains">contains</a><Element>(v: &vector<Element>, e: &Element): bool { <b>let</b> i = 0; <b>let</b> len = <a href="Vector.md#0x1_Vector_length">length</a>(v); <b>while</b> (i < len) { <b>if</b> (<a href="Vector.md#0x1_Vector_borrow">borrow</a>(v, i) == e) <b>return</b> <b>true</b>; i = i + 1; }; <b>false</b> } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> intrinsic = <b>true</b>; </code></pre> </details><a name="0x1_Vector_index_of"></a>
index_ofReturn <code>(<b>true</b>, i)</code> if <code>e</code> is in the vector <code>v</code> at index <code>i</code>. Otherwise, returns <code>(<b>false</b>, 0)</code>.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_index_of">index_of</a><Element>(v: &vector<Element>, e: &Element): (bool, u64) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_index_of">index_of</a><Element>(v: &vector<Element>, e: &Element): (bool, u64) { <b>let</b> i = 0; <b>let</b> len = <a href="Vector.md#0x1_Vector_length">length</a>(v); <b>while</b> (i < len) { <b>if</b> (<a href="Vector.md#0x1_Vector_borrow">borrow</a>(v, i) == e) <b>return</b> (<b>true</b>, i); i = i + 1; }; (<b>false</b>, 0) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> intrinsic = <b>true</b>; </code></pre> </details><a name="0x1_Vector_remove"></a>
removeRemove the <code>i</code>th element of the vector <code>v</code>, shifting all subsequent elements. This is O(n) and preserves ordering of elements in the vector. Aborts if <code>i</code> is out of bounds.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_remove">remove</a><Element>(v: &<b>mut</b> vector<Element>, i: u64): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_remove">remove</a><Element>(v: &<b>mut</b> vector<Element>, i: u64): Element { <b>let</b> len = <a href="Vector.md#0x1_Vector_length">length</a>(v); // i out of bounds; <b>abort</b> <b>if</b> (i >= len) <b>abort</b> <a href="Vector.md#0x1_Vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>; len = len - 1; <b>while</b> (i < len) <a href="Vector.md#0x1_Vector_swap">swap</a>(v, i, { i = i + 1; i }); <a href="Vector.md#0x1_Vector_pop_back">pop_back</a>(v) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> intrinsic = <b>true</b>; </code></pre> </details><a name="0x1_Vector_swap_remove"></a>
swap_removeSwap the <code>i</code>th element of the vector <code>v</code> 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 <code>i</code> is out of bounds.
<pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_swap_remove">swap_remove</a><Element>(v: &<b>mut</b> vector<Element>, i: u64): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="Vector.md#0x1_Vector_swap_remove">swap_remove</a><Element>(v: &<b>mut</b> vector<Element>, i: u64): Element { <b>assert</b>(!<a href="Vector.md#0x1_Vector_is_empty">is_empty</a>(v), <a href="Vector.md#0x1_Vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>); <b>let</b> last_idx = <a href="Vector.md#0x1_Vector_length">length</a>(v) - 1; <a href="Vector.md#0x1_Vector_swap">swap</a>(v, i, last_idx); <a href="Vector.md#0x1_Vector_pop_back">pop_back</a>(v) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> intrinsic = <b>true</b>; </code></pre> </details><a name="@Module_Specification_1"></a>
<a name="@Helper_Functions_2"></a>
Check if <code>v1</code> is equal to the result of adding <code>e</code> at the end of <code>v2</code>
<a name="0x1_Vector_eq_push_back"></a>
<pre><code><b>fun</b> <a href="Vector.md#0x1_Vector_eq_push_back">eq_push_back</a><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)] } </code></pre>Check if <code>v</code> is equal to the result of concatenating <code>v1</code> and <code>v2</code>
<a name="0x1_Vector_eq_append"></a>
<pre><code><b>fun</b> <a href="Vector.md#0x1_Vector_eq_append">eq_append</a><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 } </code></pre>Check <code>v1</code> is equal to the result of removing the first element of <code>v2</code>
<a name="0x1_Vector_eq_pop_front"></a>
<pre><code><b>fun</b> <a href="Vector.md#0x1_Vector_eq_pop_front">eq_pop_front</a><Element>(v1: vector<Element>, v2: vector<Element>): bool { len(v1) + 1 == len(v2) && v1 == v2[1..len(v2)] } </code></pre>Check that <code>v1</code> is equal to the result of removing the element at index <code>i</code> from <code>v2</code>.
<a name="0x1_Vector_eq_remove_elem_at_index"></a>
<pre><code><b>fun</b> <a href="Vector.md#0x1_Vector_eq_remove_elem_at_index">eq_remove_elem_at_index</a><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)] } </code></pre>