0x1::SimpleMap
This module provides a solution for sorted 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
SimpleMap
Element
length
create
borrow
borrow_mut
contains_key
destroy_empty
add
upsert
remove
find
use 0x1::Errors;
use 0x1::Option;
use 0x1::Vector;
SimpleMap
struct SimpleMap<Key, Value> has copy, drop, store
data: vector<SimpleMap::Element<Key, Value>>
Element
struct Element<Key, Value> has copy, drop, store
key: Key
value: Value
Map key already exists
const EKEY_ALREADY_EXISTS: u64 = 1;
Map key is not found
const EKEY_NOT_FOUND: u64 = 2;
length
public fun length<Key: store, Value: store>(map: &SimpleMap::SimpleMap<Key, Value>): u64
public fun length<Key: store, Value: store>(map: &SimpleMap<Key, Value>): u64 {
Vector::length(&map.data)
}
pragma intrinsic = true;
create
public fun create<Key: store, Value: store>(): SimpleMap::SimpleMap<Key, Value>
public fun create<Key: store, Value: store>(): SimpleMap<Key, Value> {
SimpleMap {
data: Vector::empty(),
}
}
pragma intrinsic = true;
borrow
public fun borrow<Key: store, Value: store>(map: &SimpleMap::SimpleMap<Key, Value>, key: &Key): &Value
public fun borrow<Key: store, Value: store>(
map: &SimpleMap<Key, Value>,
key: &Key,
): &Value {
let maybe_idx = find(map, key);
assert!(Option::is_some(&maybe_idx), Errors::invalid_argument(EKEY_NOT_FOUND));
let idx = Option::extract(&mut maybe_idx);
&Vector::borrow(&map.data, idx).value
}
pragma intrinsic = true;
borrow_mut
public fun borrow_mut<Key: store, Value: store>(map: &mut SimpleMap::SimpleMap<Key, Value>, key: &Key): &mut Value
public fun borrow_mut<Key: store, Value: store>(
map: &mut SimpleMap<Key, Value>,
key: &Key,
): &mut Value {
let maybe_idx = find(map, key);
assert!(Option::is_some(&maybe_idx), Errors::invalid_argument(EKEY_NOT_FOUND));
let idx = Option::extract(&mut maybe_idx);
&mut Vector::borrow_mut(&mut map.data, idx).value
}
pragma intrinsic = true;
contains_key
public fun contains_key<Key: store, Value: store>(map: &SimpleMap::SimpleMap<Key, Value>, key: &Key): bool
public fun contains_key<Key: store, Value: store>(
map: &SimpleMap<Key, Value>,
key: &Key,
): bool {
let maybe_idx = find(map, key);
Option::is_some(&maybe_idx)
}
pragma intrinsic = true;
destroy_empty
public fun destroy_empty<Key: store, Value: store>(map: SimpleMap::SimpleMap<Key, Value>)
public fun destroy_empty<Key: store, Value: store>(map: SimpleMap<Key, Value>) {
let SimpleMap { data } = map;
Vector::destroy_empty(data);
}
pragma intrinsic = true;
add
public fun add<Key: store, Value: store>(map: &mut SimpleMap::SimpleMap<Key, Value>, key: Key, value: Value)
public fun add<Key: store, Value: store>(
map: &mut SimpleMap<Key, Value>,
key: Key,
value: Value,
) {
let maybe_idx = find(map, &key);
assert!(Option::is_none(&maybe_idx), Errors::invalid_argument(EKEY_ALREADY_EXISTS));
Vector::push_back(&mut map.data, Element { key, value });
}
pragma intrinsic = true;
upsert
Insert key/value pair or update an existing key to a new value
public fun upsert<Key: store, Value: store>(map: &mut SimpleMap::SimpleMap<Key, Value>, key: Key, value: Value): (Option::Option<Key>, Option::Option<Value>)
public fun upsert<Key: store, Value: store>(
map: &mut SimpleMap<Key, Value>,
key: Key,
value: Value
): (Option::Option<Key>, Option::Option<Value>) {
let data = &mut map.data;
let len = Vector::length(data);
let i = 0;
while (i < len) {
let element = Vector::borrow(data, i);
if (&element.key == &key) {
Vector::push_back(data, Element { key, value });
Vector::swap(data, i, len);
let Element { key, value } = Vector::pop_back(data);
return (Option::some(key), Option::some(value))
};
i = i + 1;
};
Vector::push_back(&mut map.data, Element { key, value });
(Option::none(), Option::none())
}
pragma verify=false;
remove
public fun remove<Key: store, Value: store>(map: &mut SimpleMap::SimpleMap<Key, Value>, key: &Key): (Key, Value)
public fun remove<Key: store, Value: store>(
map: &mut SimpleMap<Key, Value>,
key: &Key,
): (Key, Value) {
let maybe_idx = find(map, key);
assert!(Option::is_some(&maybe_idx), Errors::invalid_argument(EKEY_NOT_FOUND));
let placement = Option::extract(&mut maybe_idx);
let Element { key, value } = Vector::swap_remove(&mut map.data, placement);
(key, value)
}
pragma intrinsic = true;
find
fun find<Key: store, Value: store>(map: &SimpleMap::SimpleMap<Key, Value>, key: &Key): Option::Option<u64>
fun find<Key: store, Value: store>(
map: &SimpleMap<Key, Value>,
key: &Key,
): Option::Option<u64> {
let leng = Vector::length(&map.data);
let i = 0;
while (i < leng) {
let element = Vector::borrow(&map.data, i);
if (&element.key == key) {
return Option::some(i)
};
i = i + 1;
};
Option::none<u64>()
}
pragma verify=false;