Skip to content

Latest commit

 

History

History
244 lines (110 loc) · 7.3 KB

simple_map.md

File metadata and controls

244 lines (110 loc) · 7.3 KB

Module 0x2::simple_map

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

  1. Keys point to Values
  2. Each Key must be unique
  3. A Key can be found within O(N) time
  4. The keys are unsorted.
  5. Adds and removals take O(N) time

Struct SimpleMap

struct SimpleMap<Key, Value> has copy, drop, store

Struct Element

struct Element<Key, Value> has copy, drop, store

Constants

Map key already exists

const ErrorKeyAlreadyExists: u64 = 1;

Map key is not found

const ErrorKeyNotFound: u64 = 2;

Function length

public fun length<Key, Value>(map: &simple_map::SimpleMap<Key, Value>): u64

Function new

Create an empty SimpleMap.

public fun new<Key, Value>(): simple_map::SimpleMap<Key, Value>

Function clone

public fun clone<Key: copy, store, Value: copy, store>(map: &simple_map::SimpleMap<Key, Value>): simple_map::SimpleMap<Key, Value>

Function borrow

public fun borrow<Key, Value>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): &Value

Function borrow_with_default

public fun borrow_with_default<Key, Value>(map: &simple_map::SimpleMap<Key, Value>, key: &Key, default: &Value): &Value

Function borrow_mut

public fun borrow_mut<Key, Value>(map: &mut simple_map::SimpleMap<Key, Value>, key: &Key): &mut Value

Function contains_key

public fun contains_key<Key, Value>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): bool

Function destroy_empty

public fun destroy_empty<Key, Value>(map: simple_map::SimpleMap<Key, Value>)

Function add

public fun add<Key, Value>(map: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value)

Function upsert

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>)

Function keys

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>

Function values

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>

Function to_vec_pair

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>)

Function remove

public fun remove<Key, Value>(map: &mut simple_map::SimpleMap<Key, Value>, key: &Key): (Key, Value)