Module iota::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
from_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 std::option;
use std::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.
public struct VecMap<K: copy, V> has copy, drop, store
Fields
contents: vector<iota::vec_map::Entry<K, V>>
Struct Entry
An entry in the map
public 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;
Trying to construct a map from keys and values of different lengths
const EUnequalLengths: u64 = 5;
Function empty
Create an empty VecMap
public fun empty<K: copy, V>(): iota::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 iota::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 iota::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 iota::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 iota::vec_map::VecMap<K, V>, key: &K): &mut V
Implementation
Function get
Get a reference to the value bound to key
in self
.
Aborts if key
is not bound in self
.
public fun get<K: copy, V>(self: &iota::vec_map::VecMap<K, V>, key: &K): &V
Implementation
Function try_get
Safely try borrow a value bound to key
in self
.
Return Some(V) if the value exists, None otherwise.
Only works for a "copyable" value as references cannot be stored in vector
.
public fun try_get<K: copy, V: copy>(self: &iota::vec_map::VecMap<K, V>, key: &K): std::option::Option<V>
Implementation
Function contains
Return true if self
contains an entry for key
, false otherwise
public fun contains<K: copy, V>(self: &iota::vec_map::VecMap<K, V>, key: &K): bool
Implementation
public fun contains<K: copy, V>(self: &VecMap<K, V>, key: &K): bool {
get_idx_opt(self, key).is_some()
}
Function size
Return the number of entries in self
public fun size<K: copy, V>(self: &iota::vec_map::VecMap<K, V>): u64
Function is_empty
Return true if self
has 0 elements, false otherwise
public fun is_empty<K: copy, V>(self: &iota::vec_map::VecMap<K, V>): bool