Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Automation] Enumerable map enhancements #148

Open
wants to merge 7 commits into
base: feature/automation-networks
Choose a base branch
from
129 changes: 112 additions & 17 deletions aptos-move/framework/supra-stdlib/doc/enumerable_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ The module includes error handling and a suite of test functions for validation.
- [Function `get_map_list`](#0x1_enumerable_map_get_map_list)
- [Function `contains`](#0x1_enumerable_map_contains)
- [Function `length`](#0x1_enumerable_map_length)
- [Function `for_each_value`](#0x1_enumerable_map_for_each_value)
- [Function `for_each_value_ref`](#0x1_enumerable_map_for_each_value_ref)
- [Function `filter`](#0x1_enumerable_map_filter)


<pre><code><b>use</b> <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error">0x1::error</a>;
Expand Down Expand Up @@ -188,7 +191,7 @@ To create an empty enum map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_new_map">new_map</a>&lt;K : <b>copy</b> + drop, V : store+drop+<b>copy</b>&gt;(): <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt; {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_new_map">new_map</a>&lt;K: <b>copy</b> + drop, V: store+drop+<b>copy</b>&gt;(): <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt; {
<b>return</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt; { list: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_empty">vector::empty</a>&lt;K&gt;(), map: <a href="../../aptos-stdlib/doc/table.md#0x1_table_new">table::new</a>&lt;K, <a href="enumerable_map.md#0x1_enumerable_map_Tuple">Tuple</a>&lt;V&gt;&gt;() }
}
</code></pre>
Expand All @@ -213,7 +216,7 @@ Add Single Key in the Enumerable Map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_add_value">add_value</a>&lt;K : <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K, value: V) {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_add_value">add_value</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K, value: V) {
<b>assert</b>!(!<a href="enumerable_map.md#0x1_enumerable_map_contains">contains</a>(map, key), <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_already_exists">error::already_exists</a>(<a href="enumerable_map.md#0x1_enumerable_map_EKEY_ALREADY_ADDED">EKEY_ALREADY_ADDED</a>));
<a href="../../aptos-stdlib/doc/table.md#0x1_table_add">table::add</a>(&<b>mut</b> map.map, key, <a href="enumerable_map.md#0x1_enumerable_map_Tuple">Tuple</a>&lt;V&gt; { position: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_length">vector::length</a>(&map.list), value });
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_push_back">vector::push_back</a>(&<b>mut</b> map.list, key);
Expand All @@ -240,7 +243,7 @@ Add Multiple Keys in the Enumerable Map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_add_value_bulk">add_value_bulk</a>&lt;K: <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_add_value_bulk">add_value_bulk</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(
map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;,
keys: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;K&gt;,
values: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;V&gt;
Expand Down Expand Up @@ -271,10 +274,10 @@ Add Multiple Keys in the Enumerable Map

## Function `update_value`

Update the value of a key thats already present in the Enumerable Map
Update the value of a key thats already present in the Enumerable Map and return old value


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_update_value">update_value</a>&lt;K: <b>copy</b>, drop, V: <b>copy</b>, drop, store&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">enumerable_map::EnumerableMap</a>&lt;K, V&gt;, key: K, new_value: V): <a href="enumerable_map.md#0x1_enumerable_map_KeyValue">enumerable_map::KeyValue</a>&lt;K, V&gt;
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_update_value">update_value</a>&lt;K: <b>copy</b>, drop, V: <b>copy</b>, drop, store&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">enumerable_map::EnumerableMap</a>&lt;K, V&gt;, key: K, new_value: V): V
</code></pre>


Expand All @@ -283,14 +286,15 @@ Update the value of a key thats already present in the Enumerable Map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_update_value">update_value</a>&lt;K: <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_update_value">update_value</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(
map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;,
key: K,
new_value: V
): <a href="enumerable_map.md#0x1_enumerable_map_KeyValue">KeyValue</a>&lt;K, V&gt; {
): V {
<b>assert</b>!(<a href="enumerable_map.md#0x1_enumerable_map_contains">contains</a>(map, key), <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_not_found">error::not_found</a>(<a href="enumerable_map.md#0x1_enumerable_map_EKEY_ABSENT">EKEY_ABSENT</a>));
<b>let</b> old_value = <a href="../../aptos-stdlib/doc/table.md#0x1_table_borrow">table::borrow</a>(&<b>mut</b> map.map, key).value;
<a href="../../aptos-stdlib/doc/table.md#0x1_table_borrow_mut">table::borrow_mut</a>(&<b>mut</b> map.map, key).value = new_value;
<a href="enumerable_map.md#0x1_enumerable_map_KeyValue">KeyValue</a> { key, value: new_value }
old_value
}
</code></pre>

Expand All @@ -314,7 +318,7 @@ Remove single Key from the Enumerable Map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_remove_value">remove_value</a>&lt;K : <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K) {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_remove_value">remove_value</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K) {
<b>assert</b>!(<a href="enumerable_map.md#0x1_enumerable_map_contains">contains</a>(map, key), <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_not_found">error::not_found</a>(<a href="enumerable_map.md#0x1_enumerable_map_EKEY_ABSENT">EKEY_ABSENT</a>));

<b>let</b> map_last_index = <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_length">vector::length</a>(&map.list) - 1;
Expand Down Expand Up @@ -348,7 +352,7 @@ Remove Multiple Keys from the Enumerable Map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_remove_value_bulk">remove_value_bulk</a>&lt;K : <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_remove_value_bulk">remove_value_bulk</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(
map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;,
keys: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;K&gt;
): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;K&gt; {
Expand Down Expand Up @@ -395,7 +399,7 @@ Will clear the entire data from the Enumerable Map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_clear">clear</a>&lt;K : <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;) {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_clear">clear</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;) {
<b>let</b> list = <a href="enumerable_map.md#0x1_enumerable_map_get_map_list">get_map_list</a>(map);
<a href="enumerable_map.md#0x1_enumerable_map_remove_value_bulk">remove_value_bulk</a>(map, list);
}
Expand All @@ -421,7 +425,7 @@ Returns the value of a key that is present in Enumerable Map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_get_value">get_value</a>&lt;K : <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(map: & <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K): V {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_get_value">get_value</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(map: & <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K): V {
<a href="../../aptos-stdlib/doc/table.md#0x1_table_borrow">table::borrow</a>(&map.map, key).value
}
</code></pre>
Expand All @@ -446,7 +450,7 @@ Returns reference to the value of a key that is present in Enumerable Map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_get_value_ref">get_value_ref</a>&lt;K : <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(map: & <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K): &V {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_get_value_ref">get_value_ref</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(map: & <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K): &V {
&<a href="../../aptos-stdlib/doc/table.md#0x1_table_borrow">table::borrow</a>(&map.map, key).value
}
</code></pre>
Expand All @@ -471,7 +475,7 @@ Returns the value of a key that is present in Enumerable Map
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_get_value_mut">get_value_mut</a>&lt;K : <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K): &<b>mut</b> V {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_get_value_mut">get_value_mut</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(map: &<b>mut</b> <a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K): &<b>mut</b> V {
&<b>mut</b> <a href="../../aptos-stdlib/doc/table.md#0x1_table_borrow_mut">table::borrow_mut</a>(&<b>mut</b> map.map, key).value
}
</code></pre>
Expand All @@ -496,7 +500,7 @@ Returns the list of keys that the Enumerable Map contains
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_get_map_list">get_map_list</a>&lt;K : <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(map: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;K&gt; {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_get_map_list">get_map_list</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(map: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;K&gt; {
<b>return</b> map.list
}
</code></pre>
Expand All @@ -521,7 +525,7 @@ Check whether Key is present into the Enumerable map or not
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_contains">contains</a>&lt;K: <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(map: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K): bool {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_contains">contains</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(map: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, key: K): bool {
<a href="../../aptos-stdlib/doc/table.md#0x1_table_contains">table::contains</a>(&map.map, key)
}
</code></pre>
Expand All @@ -546,13 +550,104 @@ Return current length of the EnumerableSetRing
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_length">length</a>&lt;K : <b>copy</b>+drop, V : store+drop+<b>copy</b>&gt;(set: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;): u64 {
<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_length">length</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(set: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;): u64 {
<b>return</b> <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_length">vector::length</a>(&set.list)
}
</code></pre>



</details>

<a id="0x1_enumerable_map_for_each_value"></a>

## Function `for_each_value`

Apply the function to each element in the EnumerableMap.


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_for_each_value">for_each_value</a>&lt;K: <b>copy</b>, drop, V: <b>copy</b>, drop, store&gt;(set: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">enumerable_map::EnumerableMap</a>&lt;K, V&gt;, f: |V|)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> inline <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_for_each_value">for_each_value</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(set: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, f: |V|) {
<b>let</b> i = 0;
<b>let</b> len = <a href="enumerable_map.md#0x1_enumerable_map_length">length</a>(set);
<b>while</b> (i &lt; len) {
<b>let</b> key = *<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_borrow">vector::borrow</a>(&set.list, i);
f(<a href="../../aptos-stdlib/doc/table.md#0x1_table_borrow">table::borrow</a>(&set.map, key).value);
i = i + 1
}
}
</code></pre>



</details>

<a id="0x1_enumerable_map_for_each_value_ref"></a>

## Function `for_each_value_ref`

Apply the function to a reference of each element in the EnumerableMap.


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_for_each_value_ref">for_each_value_ref</a>&lt;K: <b>copy</b>, drop, V: <b>copy</b>, drop, store&gt;(set: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">enumerable_map::EnumerableMap</a>&lt;K, V&gt;, f: |&V|)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> inline <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_for_each_value_ref">for_each_value_ref</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(set: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, f: |&V|) {
<b>let</b> i = 0;
<b>let</b> len = <a href="enumerable_map.md#0x1_enumerable_map_length">length</a>(set);
<b>while</b> (i &lt; len) {
<b>let</b> key = *<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_borrow">vector::borrow</a>(&set.list, i);
f(&<a href="../../aptos-stdlib/doc/table.md#0x1_table_borrow">table::borrow</a>(&set.map, key).value);
i = i + 1
}
}
</code></pre>



</details>

<a id="0x1_enumerable_map_filter"></a>

## Function `filter`

Filter the enumerableMap using the boolean function, removing all elements for which <code>p(e)</code> is not true.


<pre><code><b>public</b> <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_filter">filter</a>&lt;K: <b>copy</b>, drop, V: <b>copy</b>, drop, store&gt;(set: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">enumerable_map::EnumerableMap</a>&lt;K, V&gt;, p: |&V|bool): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;V&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> inline <b>fun</b> <a href="enumerable_map.md#0x1_enumerable_map_filter">filter</a>&lt;K: <b>copy</b>+drop, V: store+drop+<b>copy</b>&gt;(set: &<a href="enumerable_map.md#0x1_enumerable_map_EnumerableMap">EnumerableMap</a>&lt;K, V&gt;, p: |&V|bool): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;V&gt; {
<b>let</b> result = <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;V&gt;[];
<a href="enumerable_map.md#0x1_enumerable_map_for_each_value">for_each_value</a>(set, |v| {
<b>if</b> (p(&v)) <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_push_back">vector::push_back</a>(&<b>mut</b> result, v);
});
result
}
</code></pre>



</details>


Expand Down
Loading
Loading