crates/sui-framework/docs/std/vector.md
A variable-sized container that can hold any type. Indexing is 0-based, and vectors are growable. This module has many native functions.
emptylengthborrowpush_backborrow_mutpop_backdestroy_emptyswapsingletonreverseappendis_emptycontainsindex_ofremoveinsertswap_removeskiptaketabulatedestroydodo_refdo_mutmapmap_reffilterpartitionfind_indexfind_indicescountfoldflattenanyallzip_dozip_do_reversezip_do_refzip_do_mutzip_mapzip_map_refinsertion_sort_bymerge_sort_byis_sorted_bytake_whileskip_while<a name="@Constants_0"></a>
<a name="std_vector_EINDEX_OUT_OF_BOUNDS"></a>
The index into the vector is out of bounds
<pre><code><b>const</b> <a href="../std/vector.md#std_vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>: <a href="../std/u64.md#std_u64">u64</a> = 131072; </code></pre><a name="std_vector_empty"></a>
emptyCreate an empty vector.
<pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_empty">empty</a><Element>(): <a href="../std/vector.md#std_vector">vector</a><Element> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="../std/vector.md#std_vector_empty">empty</a><Element>(): <a href="../std/vector.md#std_vector">vector</a><Element>; </code></pre> </details><a name="std_vector_length"></a>
lengthReturn the length of the vector.
<pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_length">length</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>): <a href="../std/u64.md#std_u64">u64</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="../std/vector.md#std_vector_length">length</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>): <a href="../std/u64.md#std_u64">u64</a>; </code></pre> </details><a name="std_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="../std/vector.md#std_vector_borrow">borrow</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>, i: <a href="../std/u64.md#std_u64">u64</a>): &Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="../std/vector.md#std_vector_borrow">borrow</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>, i: <a href="../std/u64.md#std_u64">u64</a>): ∈ </code></pre> </details><a name="std_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="../std/vector.md#std_vector_push_back">push_back</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, e: Element) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="../std/vector.md#std_vector_push_back">push_back</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, e: Element); </code></pre> </details><a name="std_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="../std/vector.md#std_vector_borrow_mut">borrow_mut</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, i: <a href="../std/u64.md#std_u64">u64</a>): &<b>mut</b> Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="../std/vector.md#std_vector_borrow_mut">borrow_mut</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, i: <a href="../std/u64.md#std_u64">u64</a>): &<b>mut</b> Element; </code></pre> </details><a name="std_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="../std/vector.md#std_vector_pop_back">pop_back</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="../std/vector.md#std_vector_pop_back">pop_back</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>): Element; </code></pre> </details><a name="std_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="../std/vector.md#std_vector_destroy_empty">destroy_empty</a><Element>(v: <a href="../std/vector.md#std_vector">vector</a><Element>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="../std/vector.md#std_vector_destroy_empty">destroy_empty</a><Element>(v: <a href="../std/vector.md#std_vector">vector</a><Element>); </code></pre> </details><a name="std_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="../std/vector.md#std_vector_swap">swap</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, i: <a href="../std/u64.md#std_u64">u64</a>, j: <a href="../std/u64.md#std_u64">u64</a>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="../std/vector.md#std_vector_swap">swap</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, i: <a href="../std/u64.md#std_u64">u64</a>, j: <a href="../std/u64.md#std_u64">u64</a>); </code></pre> </details><a name="std_vector_singleton"></a>
singletonReturn an vector of size one containing element <code>e</code>.
<pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_singleton">singleton</a><Element>(e: Element): <a href="../std/vector.md#std_vector">vector</a><Element> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_singleton">singleton</a><Element>(e: Element): <a href="../std/vector.md#std_vector">vector</a><Element> { <b>let</b> <b>mut</b> v = <a href="../std/vector.md#std_vector_empty">empty</a>(); v.<a href="../std/vector.md#std_vector_push_back">push_back</a>(e); v } </code></pre> </details><a name="std_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="../std/vector.md#std_vector_reverse">reverse</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_reverse">reverse</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>) { <b>let</b> len = v.<a href="../std/vector.md#std_vector_length">length</a>(); <b>if</b> (len == 0) <b>return</b>; <b>let</b> <b>mut</b> front_index = 0; <b>let</b> <b>mut</b> back_index = len - 1; <b>while</b> (front_index < back_index) { v.<a href="../std/vector.md#std_vector_swap">swap</a>(front_index, back_index); front_index = front_index + 1; back_index = back_index - 1; } } </code></pre> </details><a name="std_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="../std/vector.md#std_vector_append">append</a><Element>(lhs: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, other: <a href="../std/vector.md#std_vector">vector</a><Element>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_append">append</a><Element>(lhs: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, other: <a href="../std/vector.md#std_vector">vector</a><Element>) { other.<a href="../std/vector.md#std_vector_do">do</a>!(|e| lhs.<a href="../std/vector.md#std_vector_push_back">push_back</a>(e)); } </code></pre> </details><a name="std_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="../std/vector.md#std_vector_is_empty">is_empty</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>): <a href="../std/bool.md#std_bool">bool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_is_empty">is_empty</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>): <a href="../std/bool.md#std_bool">bool</a> { v.<a href="../std/vector.md#std_vector_length">length</a>() == 0 } </code></pre> </details><a name="std_vector_contains"></a>
containsReturn true if <code>e</code> is in the vector <code>v</code>. Otherwise, returns false.
<pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_contains">contains</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>, e: &Element): <a href="../std/bool.md#std_bool">bool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_contains">contains</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>, e: &Element): <a href="../std/bool.md#std_bool">bool</a> { <b>let</b> <b>mut</b> i = 0; <b>let</b> len = v.<a href="../std/vector.md#std_vector_length">length</a>(); <b>while</b> (i < len) { <b>if</b> (&v[i] == e) <b>return</b> <b>true</b>; i = i + 1; }; <b>false</b> } </code></pre> </details><a name="std_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="../std/vector.md#std_vector_index_of">index_of</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>, e: &Element): (<a href="../std/bool.md#std_bool">bool</a>, <a href="../std/u64.md#std_u64">u64</a>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_index_of">index_of</a><Element>(v: &<a href="../std/vector.md#std_vector">vector</a><Element>, e: &Element): (<a href="../std/bool.md#std_bool">bool</a>, <a href="../std/u64.md#std_u64">u64</a>) { <b>let</b> <b>mut</b> i = 0; <b>let</b> len = v.<a href="../std/vector.md#std_vector_length">length</a>(); <b>while</b> (i < len) { <b>if</b> (&v[i] == e) <b>return</b> (<b>true</b>, i); i = i + 1; }; (<b>false</b>, 0) } </code></pre> </details><a name="std_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="../std/vector.md#std_vector_remove">remove</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, i: <a href="../std/u64.md#std_u64">u64</a>): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_remove">remove</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, <b>mut</b> i: <a href="../std/u64.md#std_u64">u64</a>): Element { <b>let</b> <b>mut</b> len = v.<a href="../std/vector.md#std_vector_length">length</a>(); // i out of bounds; <b>abort</b> <b>if</b> (i >= len) <b>abort</b> <a href="../std/vector.md#std_vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>; len = len - 1; <b>while</b> (i < len) { v.<a href="../std/vector.md#std_vector_swap">swap</a>(i, { i = i + 1; i }); }; v.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>() } </code></pre> </details><a name="std_vector_insert"></a>
insertInsert <code>e</code> at position <code>i</code> in the vector <code>v</code>. If <code>i</code> is in bounds, this shifts the old <code>v[i]</code> and all subsequent elements to the right. If <code>i == v.<a href="../std/vector.md#std_vector_length">length</a>()</code>, this adds <code>e</code> to the end of the vector. This is O(n) and preserves ordering of elements in the vector. Aborts if <code>i > v.<a href="../std/vector.md#std_vector_length">length</a>()</code>
<pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_insert">insert</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, e: Element, i: <a href="../std/u64.md#std_u64">u64</a>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_insert">insert</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, e: Element, <b>mut</b> i: <a href="../std/u64.md#std_u64">u64</a>) { <b>let</b> len = v.<a href="../std/vector.md#std_vector_length">length</a>(); // i too big <b>abort</b> <b>if</b> (i > len) <b>abort</b> <a href="../std/vector.md#std_vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>; v.<a href="../std/vector.md#std_vector_push_back">push_back</a>(e); <b>while</b> (i < len) { v.<a href="../std/vector.md#std_vector_swap">swap</a>(i, len); i = i + 1 } } </code></pre> </details><a name="std_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="../std/vector.md#std_vector_swap_remove">swap_remove</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, i: <a href="../std/u64.md#std_u64">u64</a>): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_swap_remove">swap_remove</a><Element>(v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><Element>, i: <a href="../std/u64.md#std_u64">u64</a>): Element { <b>assert</b>!(v.<a href="../std/vector.md#std_vector_length">length</a>() != 0, <a href="../std/vector.md#std_vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>); <b>let</b> last_idx = v.<a href="../std/vector.md#std_vector_length">length</a>() - 1; v.<a href="../std/vector.md#std_vector_swap">swap</a>(i, last_idx); v.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>() } </code></pre> </details><a name="std_vector_skip"></a>
skipReturn a new vector containing the elements of <code>v</code> except the first <code>n</code> elements. If <code>n > <a href="../std/vector.md#std_vector_length">length</a></code>, returns an empty vector.
<pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_skip">skip</a><T: drop>(v: <a href="../std/vector.md#std_vector">vector</a><T>, n: <a href="../std/u64.md#std_u64">u64</a>): <a href="../std/vector.md#std_vector">vector</a><T> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_skip">skip</a><T: drop>(<b>mut</b> v: <a href="../std/vector.md#std_vector">vector</a><T>, n: <a href="../std/u64.md#std_u64">u64</a>): <a href="../std/vector.md#std_vector">vector</a><T> { <b>let</b> len = v.<a href="../std/vector.md#std_vector_length">length</a>(); <b>if</b> (n >= len) <b>return</b> <a href="../std/vector.md#std_vector">vector</a>[]; <b>let</b> <b>mut</b> r = <a href="../std/vector.md#std_vector_tabulate">tabulate</a>!(len - n, |_| v.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>()); r.<a href="../std/vector.md#std_vector_reverse">reverse</a>(); r } </code></pre> </details><a name="std_vector_take"></a>
takeTake the first <code>n</code> elements of the vector <code>v</code> and drop the rest. Aborts if <code>n</code> is greater than the length of <code>v</code>.
<pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_take">take</a><T: drop>(v: <a href="../std/vector.md#std_vector">vector</a><T>, n: <a href="../std/u64.md#std_u64">u64</a>): <a href="../std/vector.md#std_vector">vector</a><T> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_take">take</a><T: drop>(<b>mut</b> v: <a href="../std/vector.md#std_vector">vector</a><T>, n: <a href="../std/u64.md#std_u64">u64</a>): <a href="../std/vector.md#std_vector">vector</a><T> { <b>assert</b>!(n <= v.<a href="../std/vector.md#std_vector_length">length</a>()); <b>if</b> (n == v.<a href="../std/vector.md#std_vector_length">length</a>()) <b>return</b> v; v.<a href="../std/vector.md#std_vector_reverse">reverse</a>(); <a href="../std/vector.md#std_vector_tabulate">tabulate</a>!(n, |_| v.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>()) } </code></pre> </details><a name="std_vector_tabulate"></a>
tabulateCreate a vector of length <code>n</code> by calling the function <code>f</code> on each index.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_tabulate">tabulate</a><$T>($n: <a href="../std/u64.md#std_u64">u64</a>, $f: |<a href="../std/u64.md#std_u64">u64</a>| -> $T): <a href="../std/vector.md#std_vector">vector</a><$T> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_tabulate">tabulate</a><$T>($n: <a href="../std/u64.md#std_u64">u64</a>, $f: |<a href="../std/u64.md#std_u64">u64</a>| -> $T): <a href="../std/vector.md#std_vector">vector</a><$T> { <b>let</b> <b>mut</b> v = <a href="../std/vector.md#std_vector">vector</a>[]; <b>let</b> n = $n; n.<a href="../std/vector.md#std_vector_do">do</a>!(|i| v.<a href="../std/vector.md#std_vector_push_back">push_back</a>($f(i))); v } </code></pre> </details><a name="std_vector_destroy"></a>
destroyDestroy the vector <code>v</code> by calling <code>f</code> 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).
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_destroy">destroy</a><$T, $R: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |$T| -> $R) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_destroy">destroy</a><$T, $R: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |$T| -> $R) { <b>let</b> <b>mut</b> v = $v; v.<a href="../std/vector.md#std_vector_length">length</a>().<a href="../std/vector.md#std_vector_do">do</a>!(|_| $f(v.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>())); v.<a href="../std/vector.md#std_vector_destroy_empty">destroy_empty</a>(); } </code></pre> </details><a name="std_vector_do"></a>
doDestroy the vector <code>v</code> by calling <code>f</code> on each element and then destroying the vector. Preserves the order of elements in the vector.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_do">do</a><$T, $R: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |$T| -> $R) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_do">do</a><$T, $R: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |$T| -> $R) { <b>let</b> <b>mut</b> v = $v; v.<a href="../std/vector.md#std_vector_reverse">reverse</a>(); v.<a href="../std/vector.md#std_vector_length">length</a>().<a href="../std/vector.md#std_vector_do">do</a>!(|_| $f(v.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>())); v.<a href="../std/vector.md#std_vector_destroy_empty">destroy_empty</a>(); } </code></pre> </details><a name="std_vector_do_ref"></a>
do_refPerform an action <code>f</code> on each element of the vector <code>v</code>. The vector is not modified.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_do_ref">do_ref</a><$T, $R: drop>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> $R) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_do_ref">do_ref</a><$T, $R: drop>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> $R) { <b>let</b> v = $v; v.<a href="../std/vector.md#std_vector_length">length</a>().<a href="../std/vector.md#std_vector_do">do</a>!(|i| $f(&v[i])) } </code></pre> </details><a name="std_vector_do_mut"></a>
do_mutPerform an action <code>f</code> on each element of the vector <code>v</code>. The function <code>f</code> takes a mutable reference to the element.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_do_mut">do_mut</a><$T, $R: drop>($v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&<b>mut</b> $T| -> $R) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_do_mut">do_mut</a><$T, $R: drop>($v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&<b>mut</b> $T| -> $R) { <b>let</b> v = $v; v.<a href="../std/vector.md#std_vector_length">length</a>().<a href="../std/vector.md#std_vector_do">do</a>!(|i| $f(&<b>mut</b> v[i])) } </code></pre> </details><a name="std_vector_map"></a>
mapMap the vector <code>v</code> to a new vector by applying the function <code>f</code> to each element. Preserves the order of elements in the vector, first is called first.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_map">map</a><$T, $U>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |$T| -> $U): <a href="../std/vector.md#std_vector">vector</a><$U> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_map">map</a><$T, $U>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |$T| -> $U): <a href="../std/vector.md#std_vector">vector</a><$U> { <b>let</b> v = $v; <b>let</b> <b>mut</b> r = <a href="../std/vector.md#std_vector">vector</a>[]; v.<a href="../std/vector.md#std_vector_do">do</a>!(|e| r.<a href="../std/vector.md#std_vector_push_back">push_back</a>($f(e))); r } </code></pre> </details><a name="std_vector_map_ref"></a>
map_refMap the vector <code>v</code> to a new vector by applying the function <code>f</code> to each element. Preserves the order of elements in the vector, first is called first.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_map_ref">map_ref</a><$T, $U>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> $U): <a href="../std/vector.md#std_vector">vector</a><$U> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_map_ref">map_ref</a><$T, $U>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> $U): <a href="../std/vector.md#std_vector">vector</a><$U> { <b>let</b> v = $v; <b>let</b> <b>mut</b> r = <a href="../std/vector.md#std_vector">vector</a>[]; v.<a href="../std/vector.md#std_vector_do_ref">do_ref</a>!(|e| r.<a href="../std/vector.md#std_vector_push_back">push_back</a>($f(e))); r } </code></pre> </details><a name="std_vector_filter"></a>
filterFilter the vector <code>v</code> by applying the function <code>f</code> to each element. Return a new vector containing only the elements for which <code>f</code> returns <code><b>true</b></code>.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_filter">filter</a><$T: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/vector.md#std_vector">vector</a><$T> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_filter">filter</a><$T: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/vector.md#std_vector">vector</a><$T> { <b>let</b> v = $v; <b>let</b> <b>mut</b> r = <a href="../std/vector.md#std_vector">vector</a>[]; v.<a href="../std/vector.md#std_vector_do">do</a>!(|e| <b>if</b> ($f(&e)) r.<a href="../std/vector.md#std_vector_push_back">push_back</a>(e)); r } </code></pre> </details><a name="std_vector_partition"></a>
partitionSplit the vector <code>v</code> into two vectors by applying the function <code>f</code> to each element. Return a tuple containing two vectors: the first containing the elements for which <code>f</code> returns <code><b>true</b></code>, and the second containing the elements for which <code>f</code> returns <code><b>false</b></code>.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_partition">partition</a><$T>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): (<a href="../std/vector.md#std_vector">vector</a><$T>, <a href="../std/vector.md#std_vector">vector</a><$T>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_partition">partition</a><$T>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): (<a href="../std/vector.md#std_vector">vector</a><$T>, <a href="../std/vector.md#std_vector">vector</a><$T>) { <b>let</b> v = $v; <b>let</b> <b>mut</b> r1 = <a href="../std/vector.md#std_vector">vector</a>[]; <b>let</b> <b>mut</b> r2 = <a href="../std/vector.md#std_vector">vector</a>[]; v.<a href="../std/vector.md#std_vector_do">do</a>!(|e| <b>if</b> ($f(&e)) r1.<a href="../std/vector.md#std_vector_push_back">push_back</a>(e) <b>else</b> r2.<a href="../std/vector.md#std_vector_push_back">push_back</a>(e)); (r1, r2) } </code></pre> </details><a name="std_vector_find_index"></a>
find_indexFinds the index of first element in the vector <code>v</code> that satisfies the predicate <code>f</code>. Returns <code>some(index)</code> if such an element is found, otherwise <code>none()</code>.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_find_index">find_index</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/option.md#std_option_Option">std::option::Option</a><<a href="../std/u64.md#std_u64">u64</a>> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_find_index">find_index</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): Option<<a href="../std/u64.md#std_u64">u64</a>> { <b>let</b> v = $v; '<a href="../std/vector.md#std_vector_find_index">find_index</a>: { v.<a href="../std/vector.md#std_vector_length">length</a>().<a href="../std/vector.md#std_vector_do">do</a>!(|i| <b>if</b> ($f(&v[i])) <b>return</b> '<a href="../std/vector.md#std_vector_find_index">find_index</a> <a href="../std/option.md#std_option_some">option::some</a>(i)); <a href="../std/option.md#std_option_none">option::none</a>() } } </code></pre> </details><a name="std_vector_find_indices"></a>
find_indicesFinds all indices of elements in the vector <code>v</code> that satisfy the predicate <code>f</code>. Returns a vector of indices of all found elements.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_find_indices">find_indices</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/vector.md#std_vector">vector</a><<a href="../std/u64.md#std_u64">u64</a>> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_find_indices">find_indices</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/vector.md#std_vector">vector</a><<a href="../std/u64.md#std_u64">u64</a>> { <b>let</b> v = $v; <b>let</b> <b>mut</b> indices = <a href="../std/vector.md#std_vector">vector</a>[]; v.<a href="../std/vector.md#std_vector_length">length</a>().<a href="../std/vector.md#std_vector_do">do</a>!(|i| <b>if</b> ($f(&v[i])) indices.<a href="../std/vector.md#std_vector_push_back">push_back</a>(i)); indices } </code></pre> </details><a name="std_vector_count"></a>
countCount how many elements in the vector <code>v</code> satisfy the predicate <code>f</code>.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_count">count</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/u64.md#std_u64">u64</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_count">count</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/u64.md#std_u64">u64</a> { <b>let</b> v = $v; <b>let</b> <b>mut</b> <a href="../std/vector.md#std_vector_count">count</a> = 0; v.<a href="../std/vector.md#std_vector_do_ref">do_ref</a>!(|e| <b>if</b> ($f(e)) <a href="../std/vector.md#std_vector_count">count</a> = <a href="../std/vector.md#std_vector_count">count</a> + 1); <a href="../std/vector.md#std_vector_count">count</a> } </code></pre> </details><a name="std_vector_fold"></a>
foldReduce the vector <code>v</code> to a single value by applying the function <code>f</code> to each element. Similar to <code>fold_left</code> in Rust and <code>reduce</code> in Python and JavaScript.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_fold">fold</a><$T, $Acc>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $init: $Acc, $f: |$Acc, $T| -> $Acc): $Acc </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_fold">fold</a><$T, $Acc>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $init: $Acc, $f: |$Acc, $T| -> $Acc): $Acc { <b>let</b> v = $v; <b>let</b> <b>mut</b> acc = $init; v.<a href="../std/vector.md#std_vector_do">do</a>!(|e| acc = $f(acc, e)); acc } </code></pre> </details><a name="std_vector_flatten"></a>
flattenConcatenate the vectors of <code>v</code> into a single vector, keeping the order of the elements.
<pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_flatten">flatten</a><T>(v: <a href="../std/vector.md#std_vector">vector</a><<a href="../std/vector.md#std_vector">vector</a><T>>): <a href="../std/vector.md#std_vector">vector</a><T> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/vector.md#std_vector_flatten">flatten</a><T>(v: <a href="../std/vector.md#std_vector">vector</a><<a href="../std/vector.md#std_vector">vector</a><T>>): <a href="../std/vector.md#std_vector">vector</a><T> { <b>let</b> <b>mut</b> r = <a href="../std/vector.md#std_vector">vector</a>[]; v.<a href="../std/vector.md#std_vector_do">do</a>!(|u| r.<a href="../std/vector.md#std_vector_append">append</a>(u)); r } </code></pre> </details><a name="std_vector_any"></a>
anyWhether any element in the vector <code>v</code> satisfies the predicate <code>f</code>. If the vector is empty, returns <code><b>false</b></code>.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_any">any</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/bool.md#std_bool">bool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_any">any</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/bool.md#std_bool">bool</a> { <b>let</b> v = $v; '<a href="../std/vector.md#std_vector_any">any</a>: { v.<a href="../std/vector.md#std_vector_do_ref">do_ref</a>!(|e| <b>if</b> ($f(e)) <b>return</b> '<a href="../std/vector.md#std_vector_any">any</a> <b>true</b>); <b>false</b> } } </code></pre> </details><a name="std_vector_all"></a>
allWhether all elements in the vector <code>v</code> satisfy the predicate <code>f</code>. If the vector is empty, returns <code><b>true</b></code>.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_all">all</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/bool.md#std_bool">bool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_all">all</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $f: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/bool.md#std_bool">bool</a> { <b>let</b> v = $v; '<a href="../std/vector.md#std_vector_all">all</a>: { v.<a href="../std/vector.md#std_vector_do_ref">do_ref</a>!(|e| <b>if</b> (!$f(e)) <b>return</b> '<a href="../std/vector.md#std_vector_all">all</a> <b>false</b>); <b>true</b> } } </code></pre> </details><a name="std_vector_zip_do"></a>
zip_doDestroys two vectors <code>v1</code> and <code>v2</code> by calling <code>f</code> to each pair of elements. Aborts if the vectors are not of the same length. The order of elements in the vectors is preserved.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_do">zip_do</a><$T1, $T2, $R: drop>($v1: <a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: <a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |$T1, $T2| -> $R) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_do">zip_do</a><$T1, $T2, $R: drop>( $v1: <a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: <a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |$T1, $T2| -> $R, ) { <b>let</b> v1 = $v1; <b>let</b> <b>mut</b> v2 = $v2; v2.<a href="../std/vector.md#std_vector_reverse">reverse</a>(); <b>let</b> len = v1.<a href="../std/vector.md#std_vector_length">length</a>(); <b>assert</b>!(len == v2.<a href="../std/vector.md#std_vector_length">length</a>()); v1.<a href="../std/vector.md#std_vector_do">do</a>!(|el1| $f(el1, v2.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>())); v2.<a href="../std/vector.md#std_vector_destroy_empty">destroy_empty</a>(); } </code></pre> </details><a name="std_vector_zip_do_reverse"></a>
zip_do_reverseDestroys two vectors <code>v1</code> and <code>v2</code> by calling <code>f</code> to each pair of elements. Aborts if the vectors are not of the same length. Starts from the end of the vectors.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_do_reverse">zip_do_reverse</a><$T1, $T2, $R: drop>($v1: <a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: <a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |$T1, $T2| -> $R) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_do_reverse">zip_do_reverse</a><$T1, $T2, $R: drop>( $v1: <a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: <a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |$T1, $T2| -> $R, ) { <b>let</b> v1 = $v1; <b>let</b> <b>mut</b> v2 = $v2; <b>let</b> len = v1.<a href="../std/vector.md#std_vector_length">length</a>(); <b>assert</b>!(len == v2.<a href="../std/vector.md#std_vector_length">length</a>()); v1.<a href="../std/vector.md#std_vector_destroy">destroy</a>!(|el1| $f(el1, v2.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>())); } </code></pre> </details><a name="std_vector_zip_do_ref"></a>
zip_do_refIterate through <code>v1</code> and <code>v2</code> and apply the function <code>f</code> to references of each pair of elements. The vectors are not modified. Aborts if the vectors are not of the same length. The order of elements in the vectors is preserved.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_do_ref">zip_do_ref</a><$T1, $T2, $R: drop>($v1: &<a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: &<a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |&$T1, &$T2| -> $R) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_do_ref">zip_do_ref</a><$T1, $T2, $R: drop>( $v1: &<a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: &<a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |&$T1, &$T2| -> $R, ) { <b>let</b> v1 = $v1; <b>let</b> v2 = $v2; <b>let</b> len = v1.<a href="../std/vector.md#std_vector_length">length</a>(); <b>assert</b>!(len == v2.<a href="../std/vector.md#std_vector_length">length</a>()); len.<a href="../std/vector.md#std_vector_do">do</a>!(|i| $f(&v1[i], &v2[i])); } </code></pre> </details><a name="std_vector_zip_do_mut"></a>
zip_do_mutIterate through <code>v1</code> and <code>v2</code> and apply the function <code>f</code> to mutable references of each pair of elements. The vectors may be modified. Aborts if the vectors are not of the same length. The order of elements in the vectors is preserved.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_do_mut">zip_do_mut</a><$T1, $T2, $R: drop>($v1: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |&<b>mut</b> $T1, &<b>mut</b> $T2| -> $R) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_do_mut">zip_do_mut</a><$T1, $T2, $R: drop>( $v1: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |&<b>mut</b> $T1, &<b>mut</b> $T2| -> $R, ) { <b>let</b> v1 = $v1; <b>let</b> v2 = $v2; <b>let</b> len = v1.<a href="../std/vector.md#std_vector_length">length</a>(); <b>assert</b>!(len == v2.<a href="../std/vector.md#std_vector_length">length</a>()); len.<a href="../std/vector.md#std_vector_do">do</a>!(|i| $f(&<b>mut</b> v1[i], &<b>mut</b> v2[i])); } </code></pre> </details><a name="std_vector_zip_map"></a>
zip_mapDestroys two vectors <code>v1</code> and <code>v2</code> by applying the function <code>f</code> to each pair of elements. The returned values are collected into a new vector. Aborts if the vectors are not of the same length. The order of elements in the vectors is preserved.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_map">zip_map</a><$T1, $T2, $U>($v1: <a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: <a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |$T1, $T2| -> $U): <a href="../std/vector.md#std_vector">vector</a><$U> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_map">zip_map</a><$T1, $T2, $U>( $v1: <a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: <a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |$T1, $T2| -> $U, ): <a href="../std/vector.md#std_vector">vector</a><$U> { <b>let</b> <b>mut</b> r = <a href="../std/vector.md#std_vector">vector</a>[]; <a href="../std/vector.md#std_vector_zip_do">zip_do</a>!($v1, $v2, |el1, el2| r.<a href="../std/vector.md#std_vector_push_back">push_back</a>($f(el1, el2))); r } </code></pre> </details><a name="std_vector_zip_map_ref"></a>
zip_map_refIterate through <code>v1</code> and <code>v2</code> and apply the function <code>f</code> to references of each pair of elements. The returned values are collected into a new vector. Aborts if the vectors are not of the same length. The order of elements in the vectors is preserved.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_map_ref">zip_map_ref</a><$T1, $T2, $U>($v1: &<a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: &<a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |&$T1, &$T2| -> $U): <a href="../std/vector.md#std_vector">vector</a><$U> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_zip_map_ref">zip_map_ref</a><$T1, $T2, $U>( $v1: &<a href="../std/vector.md#std_vector">vector</a><$T1>, $v2: &<a href="../std/vector.md#std_vector">vector</a><$T2>, $f: |&$T1, &$T2| -> $U, ): <a href="../std/vector.md#std_vector">vector</a><$U> { <b>let</b> <b>mut</b> r = <a href="../std/vector.md#std_vector">vector</a>[]; <a href="../std/vector.md#std_vector_zip_do_ref">zip_do_ref</a>!($v1, $v2, |el1, el2| r.<a href="../std/vector.md#std_vector_push_back">push_back</a>($f(el1, el2))); r } </code></pre> </details><a name="std_vector_insertion_sort_by"></a>
insertion_sort_byPerforms an in-place insertion sort on the vector <code>v</code> using the comparison function <code>le</code>. The sort is stable, meaning that equal elements will maintain their relative order.
Please, note that the comparison function <code>le</code> expects less or equal, not less.
Example:
let mut v = vector[2, 1, 3];
v.insertion_sort_by(|a, b| a <= b);
assert!(v == vector[1, 2, 3]);
Insertion sort is efficient for small vectors (~30 or less elements), and can be faster than merge sort for almost sorted vectors (e.g. when the vector is already sorted or nearly sorted).
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_insertion_sort_by">insertion_sort_by</a><$T>($v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T>, $le: |&$T, &$T| -> <a href="../std/bool.md#std_bool">bool</a>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_insertion_sort_by">insertion_sort_by</a><$T>($v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T>, $le: |&$T, &$T| -> <a href="../std/bool.md#std_bool">bool</a>) { <b>let</b> v = $v; <b>let</b> n = v.<a href="../std/vector.md#std_vector_length">length</a>(); <b>if</b> (n < 2) <b>return</b>; // <a href="../std/vector.md#std_vector_do">do</a> insertion sort <b>let</b> <b>mut</b> i = 1; <b>while</b> (i < n) { <b>let</b> <b>mut</b> j = i; <b>while</b> (j > 0 && !$le(&v[j - 1], &v[j])) { v.<a href="../std/vector.md#std_vector_swap">swap</a>(j, j - 1); j = j - 1; }; i = i + 1; } } </code></pre> </details><a name="std_vector_merge_sort_by"></a>
merge_sort_byPerforms an in-place merge sort on the vector <code>v</code> using the comparison function <code>le</code>. Merge sort is efficient for large vectors, and is a stable sort.
Please, note that the comparison function <code>le</code> expects less or equal, not less.
Example:
let mut v = vector[2, 1, 3];
v.merge_sort_by(|a, b| a <= b);
assert!(v == vector[1, 2, 3]);
Merge sort performs better than insertion sort for large vectors (~30 elements or more).
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_merge_sort_by">merge_sort_by</a><$T>($v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T>, $le: |&$T, &$T| -> <a href="../std/bool.md#std_bool">bool</a>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_merge_sort_by">merge_sort_by</a><$T>($v: &<b>mut</b> <a href="../std/vector.md#std_vector">vector</a><$T>, $le: |&$T, &$T| -> <a href="../std/bool.md#std_bool">bool</a>) { <b>let</b> v = $v; <b>let</b> n = v.<a href="../std/vector.md#std_vector_length">length</a>(); <b>if</b> (n < 2) <b>return</b>; <b>let</b> <b>mut</b> flags = <a href="../std/vector.md#std_vector">vector</a>[<b>false</b>]; <b>let</b> <b>mut</b> starts = <a href="../std/vector.md#std_vector">vector</a>[0]; <b>let</b> <b>mut</b> ends = <a href="../std/vector.md#std_vector">vector</a>[n]; <b>while</b> (!flags.<a href="../std/vector.md#std_vector_is_empty">is_empty</a>()) { <b>let</b> (halves_sorted, start, end) = (flags.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>(), starts.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>(), ends.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>()); <b>let</b> mid = (start + end) / 2; <b>if</b> (halves_sorted) { <b>let</b> <b>mut</b> mid = mid; <b>let</b> <b>mut</b> l = start; <b>let</b> <b>mut</b> r = mid; <b>while</b> (l < mid && r < end) { <b>if</b> ($le(&v[l], &v[r])) { l = l + 1; } <b>else</b> { <b>let</b> <b>mut</b> i = r; <b>while</b> (i > l) { v.<a href="../std/vector.md#std_vector_swap">swap</a>(i, i - 1); i = i - 1; }; l = l + 1; mid = mid + 1; r = r + 1; } } } <b>else</b> { // set up the "merge" flags.<a href="../std/vector.md#std_vector_push_back">push_back</a>(<b>true</b>); starts.<a href="../std/vector.md#std_vector_push_back">push_back</a>(start); ends.<a href="../std/vector.md#std_vector_push_back">push_back</a>(end); // set up the recursive calls // v[start..mid] <b>if</b> (mid - start > 1) { flags.<a href="../std/vector.md#std_vector_push_back">push_back</a>(<b>false</b>); starts.<a href="../std/vector.md#std_vector_push_back">push_back</a>(start); ends.<a href="../std/vector.md#std_vector_push_back">push_back</a>(mid); }; // v[mid..end] <b>if</b> (end - mid > 1) { flags.<a href="../std/vector.md#std_vector_push_back">push_back</a>(<b>false</b>); starts.<a href="../std/vector.md#std_vector_push_back">push_back</a>(mid); ends.<a href="../std/vector.md#std_vector_push_back">push_back</a>(end); } } } } </code></pre> </details><a name="std_vector_is_sorted_by"></a>
is_sorted_byCheck if the vector <code>v</code> is sorted in non-decreasing order according to the comparison function <code>le</code> (les). Returns <code><b>true</b></code> if the vector is sorted, <code><b>false</b></code> otherwise.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_is_sorted_by">is_sorted_by</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $le: |&$T, &$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/bool.md#std_bool">bool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_is_sorted_by">is_sorted_by</a><$T>($v: &<a href="../std/vector.md#std_vector">vector</a><$T>, $le: |&$T, &$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/bool.md#std_bool">bool</a> { <b>let</b> v = $v; <b>let</b> n_minus_1 = v.<a href="../std/vector.md#std_vector_length">length</a>().max(1) - 1; '<a href="../std/vector.md#std_vector_is_sorted_by">is_sorted_by</a>: { n_minus_1.<a href="../std/vector.md#std_vector_do">do</a>!(|i| <b>if</b> (!$le(&v[i], &v[i + 1])) <b>return</b> '<a href="../std/vector.md#std_vector_is_sorted_by">is_sorted_by</a> <b>false</b>); <b>true</b> } } </code></pre> </details><a name="std_vector_take_while"></a>
take_whileReturn a new vector containing the elements of <code>v</code> except the first <code>n</code> elements that satisfy the predicate <code>p</code>. If all elements satisfy the predicate, returns an empty vector.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_take_while">take_while</a><$T: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $p: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/vector.md#std_vector">vector</a><$T> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_take_while">take_while</a><$T: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $p: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/vector.md#std_vector">vector</a><$T> { <b>let</b> v = $v; '<a href="../std/vector.md#std_vector_take">take</a>: { <b>let</b> <b>mut</b> r = <a href="../std/vector.md#std_vector">vector</a>[]; v.<a href="../std/vector.md#std_vector_do">do</a>!(|e| <b>if</b> ($p(&e)) r.<a href="../std/vector.md#std_vector_push_back">push_back</a>(e) <b>else</b> <b>return</b> '<a href="../std/vector.md#std_vector_take">take</a> r); r } } </code></pre> </details><a name="std_vector_skip_while"></a>
skip_whileTake all elements of the vector <code>v</code> except the first <code>n</code> elements that satisfy the predicate <code>p</code> and drop the rest, where <code>n <= v.<a href="../std/vector.md#std_vector_length">length</a>()</code>.
<pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_skip_while">skip_while</a><$T: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $p: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/vector.md#std_vector">vector</a><$T> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>macro</b> <b>fun</b> <a href="../std/vector.md#std_vector_skip_while">skip_while</a><$T: drop>($v: <a href="../std/vector.md#std_vector">vector</a><$T>, $p: |&$T| -> <a href="../std/bool.md#std_bool">bool</a>): <a href="../std/vector.md#std_vector">vector</a><$T> { <b>let</b> <b>mut</b> v = $v; v.<a href="../std/vector.md#std_vector_reverse">reverse</a>(); <b>let</b> <b>mut</b> i = v.<a href="../std/vector.md#std_vector_length">length</a>(); <b>while</b> (i > 0) { i = i - 1; <b>if</b> ($p(&v[i])) v.<a href="../std/vector.md#std_vector_pop_back">pop_back</a>() <b>else</b> <b>break</b>; }; v.<a href="../std/vector.md#std_vector_reverse">reverse</a>(); v } </code></pre> </details>