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

[framework][simple] Add remove_if_present to simple_map #15573

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions aptos-move/framework/aptos-stdlib/doc/simple_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This module provides a solution for unsorted maps, that is it has the properties
- [Function `to_vec_pair`](#0x1_simple_map_to_vec_pair)
- [Function `destroy`](#0x1_simple_map_destroy)
- [Function `remove`](#0x1_simple_map_remove)
- [Function `remove_if_present`](#0x1_simple_map_remove_if_present)
- [Function `find`](#0x1_simple_map_find)
- [Specification](#@Specification_1)
- [Struct `SimpleMap`](#@Specification_1_SimpleMap)
Expand Down Expand Up @@ -623,6 +624,42 @@ Remove a key/value pair from the map. The key must exist.



</details>

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

## Function `remove_if_present`

Remove a key/value pair from the map, if present.
Otherwise returns none.


<pre><code><b>public</b> <b>fun</b> <a href="simple_map.md#0x1_simple_map_remove_if_present">remove_if_present</a>&lt;Key: drop, store, Value: store&gt;(map: &<b>mut</b> <a href="simple_map.md#0x1_simple_map_SimpleMap">simple_map::SimpleMap</a>&lt;Key, Value&gt;, key: &Key): <a href="../../move-stdlib/doc/option.md#0x1_option_Option">option::Option</a>&lt;Value&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="simple_map.md#0x1_simple_map_remove_if_present">remove_if_present</a>&lt;Key: store + drop, Value: store&gt;(
map: &<b>mut</b> <a href="simple_map.md#0x1_simple_map_SimpleMap">SimpleMap</a>&lt;Key, Value&gt;,
key: &Key,
): <a href="../../move-stdlib/doc/option.md#0x1_option_Option">option::Option</a>&lt;Value&gt; {
<b>let</b> maybe_idx = <a href="simple_map.md#0x1_simple_map_find">find</a>(map, key);
<b>if</b> (<a href="../../move-stdlib/doc/option.md#0x1_option_is_some">option::is_some</a>(&maybe_idx)) {
<b>let</b> placement = <a href="../../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> maybe_idx);
<b>let</b> <a href="simple_map.md#0x1_simple_map_Element">Element</a> { key: _, value } = <a href="../../move-stdlib/doc/vector.md#0x1_vector_swap_remove">vector::swap_remove</a>(&<b>mut</b> map.data, placement);
<a href="../../move-stdlib/doc/option.md#0x1_option_some">option::some</a>(value)
} <b>else</b> {
<a href="../../move-stdlib/doc/option.md#0x1_option_none">option::none</a>()
}
}
</code></pre>



</details>

<a id="0x1_simple_map_find"></a>
Expand Down
16 changes: 16 additions & 0 deletions aptos-move/framework/aptos-stdlib/sources/simple_map.move
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ module aptos_std::simple_map {
(key, value)
}

/// Remove a key/value pair from the map, if present.
/// Otherwise returns none.
public fun remove_if_present<Key: store + drop, Value: store>(
map: &mut SimpleMap<Key, Value>,
key: &Key,
): option::Option<Value> {
let maybe_idx = find(map, key);
if (option::is_some(&maybe_idx)) {
let placement = option::extract(&mut maybe_idx);
let Element { key: _, value } = vector::swap_remove(&mut map.data, placement);
option::some(value)
} else {
option::none()
}
}

fun find<Key: store, Value: store>(
self: &SimpleMap<Key, Value>,
key: &Key,
Expand Down
Loading