Source from https://github.com/aptos-labs/aptos-core/blob/d50af4db34a6929642603c3896a0af17984b3054/aptos-move/framework/aptos-stdlib/sources/simple_map.move Do some refator because we do not support inline and lambda yet. This module provides a solution for unsorted maps, that is it has the properties that
- Keys point to Values
- Each Key must be unique
- A Key can be found within O(N) time
- The keys are unsorted.
- Adds and removals take O(N) time
- Struct
SimpleMap
- Struct
Element
- Constants
- Function
length
- Function
new
- Function
clone
- Function
borrow
- Function
borrow_with_default
- Function
borrow_mut
- Function
contains_key
- Function
destroy_empty
- Function
add
- Function
upsert
- Function
keys
- Function
values
- Function
to_vec_pair
- Function
remove
use 0x1::option;
use 0x1::vector;
struct SimpleMap<Key, Value> has copy, drop, store
struct Element<Key, Value> has copy, drop, store
Map key already exists
const ErrorKeyAlreadyExists: u64 = 1;
Map key is not found
const ErrorKeyNotFound: u64 = 2;
public fun length<Key, Value>(map: &simple_map::SimpleMap<Key, Value>): u64
Create an empty SimpleMap.
public fun new<Key, Value>(): simple_map::SimpleMap<Key, Value>
public fun clone<Key: copy, store, Value: copy, store>(map: &simple_map::SimpleMap<Key, Value>): simple_map::SimpleMap<Key, Value>
public fun borrow<Key, Value>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): &Value
public fun borrow_with_default<Key, Value>(map: &simple_map::SimpleMap<Key, Value>, key: &Key, default: &Value): &Value
public fun borrow_mut<Key, Value>(map: &mut simple_map::SimpleMap<Key, Value>, key: &Key): &mut Value
public fun contains_key<Key, Value>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): bool
public fun destroy_empty<Key, Value>(map: simple_map::SimpleMap<Key, Value>)
public fun add<Key, Value>(map: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value)
Insert key/value pair or update an existing key to a new value
public fun upsert<Key, Value>(map: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value): (option::Option<Key>, option::Option<Value>)
Return all keys in the map. This requires keys to be copyable.
public fun keys<Key: copy, Value>(map: &simple_map::SimpleMap<Key, Value>): vector<Key>
Return all values in the map. This requires values to be copyable.
public fun values<Key, Value: copy>(map: &simple_map::SimpleMap<Key, Value>): vector<Value>
Transform the map into two vectors with the keys and values respectively Primarily used to destroy a map
public fun to_vec_pair<Key, Value>(map: simple_map::SimpleMap<Key, Value>): (vector<Key>, vector<Value>)
public fun remove<Key, Value>(map: &mut simple_map::SimpleMap<Key, Value>, key: &Key): (Key, Value)