Module 0x2::vec_map
- Struct
VecMap
- Struct
Entry
- Constants
- Function
empty
- Function
insert
- Function
remove
- Function
pop
- Function
get_mut
- Function
get
- Function
try_get
- Function
contains
- Function
size
- Function
is_empty
- Function
destroy_empty
- Function
into_keys_values
- Function
keys
- Function
get_idx_opt
- Function
get_idx
- Function
get_entry_by_idx
- Function
get_entry_by_idx_mut
- Function
remove_entry_by_idx
use 0x1::option;
use 0x1::vector;
Struct VecMap
A map data structure backed by a vector. The map is guaranteed not to contain duplicate keys, but entries are not sorted by key--entries are included in insertion order. All operations are O(N) in the size of the map--the intention of this data structure is only to provide the convenience of programming against a map API. Large maps should use handwritten parent/child relationships instead. Maps that need sorted iteration rather than insertion order iteration should also be handwritten.
struct VecMap<K: copy, V> has copy, drop, store
Fields
- contents: vector<vec_map::Entry<K, V>>
Struct Entry
An entry in the map
struct Entry<K: copy, V> has copy, drop, store
Fields
- key: K
- value: V
Constants
Trying to access an element of the map at an invalid index
const EIndexOutOfBounds: u64 = 3;
This key already exists in the map
const EKeyAlreadyExists: u64 = 0;
This key does not exist in the map
const EKeyDoesNotExist: u64 = 1;
Trying to pop from a map that is empty
const EMapEmpty: u64 = 4;
Trying to destroy a map that is not empty
const EMapNotEmpty: u64 = 2;
Function empty
Create an empty VecMap
public fun empty<K: copy, V>(): vec_map::VecMap<K, V>
Function insert
Insert the entry key |-> value into self. Aborts if key is already bound in self.
public fun insert<K: copy, V>(self: &mut vec_map::VecMap<K, V>, key: K, value: V)
Implementation
public fun insert<K: copy, V>(self: &mut VecMap<K,V>, key: K, value: V) {
assert!(!self.contains(&key), EKeyAlreadyExists);
self.contents.push_back(Entry { key, value })
}
Function remove
Remove the entry key |-> value from self. Aborts if key is not bound in self.
public fun remove<K: copy, V>(self: &mut vec_map::VecMap<K, V>, key: &K): (K, V)
Implementation
Function pop
Pop the most recently inserted entry from the map. Aborts if the map is empty.
public fun pop<K: copy, V>(self: &mut vec_map::VecMap<K, V>): (K, V)
Implementation
Function get_mut
Get a mutable reference to the value bound to key in self. Aborts if key is not bound in self.
public fun get_mut<K: copy, V>(self: &mut vec_map::VecMap<K, V>, key: &K): &mut V