0x1::Table
Type of large-scale storage tables.
Table
Box
new
destroy_empty
add
borrow
borrow_with_default
borrow_mut
length
empty
borrow_mut_with_default
upsert
remove
contains
new_table_handle
add_box
borrow_box
borrow_box_mut
contains_box
remove_box
destroy_empty_box
drop_unchecked_box
use 0x1::Errors;
Table
Type of tables
struct Table<K: copy, drop, V> has store
handle: address
length: u64
pragma intrinsic = map,
map_new = new,
map_destroy_empty = destroy_empty,
map_len = length,
map_is_empty = empty,
map_has_key = contains,
map_add_no_override = add,
map_del_must_exist = remove,
map_borrow = borrow,
map_borrow_mut = borrow_mut,
map_spec_get = spec_get,
map_spec_set = spec_set,
map_spec_del = spec_remove,
map_spec_len = spec_len,
map_spec_has_key = spec_contains;
Box
Wrapper for values. Required for making values appear as resources in the implementation.
struct Box<V> has drop, store, key
val: V
const EALREADY_EXISTS: u64 = 100;
const ENOT_EMPTY: u64 = 102;
const ENOT_FOUND: u64 = 101;
new
Create a new Table.
public fun new<K: copy, drop, V: store>(): Table::Table<K, V>
public fun new<K: copy + drop, V: store>(): Table<K, V> {
Table{
handle: new_table_handle<K, V>(),
length: 0,
}
}
pragma intrinsic;
destroy_empty
Destroy a table. The table must be empty to succeed.
public fun destroy_empty<K: copy, drop, V>(table: Table::Table<K, V>)
public fun destroy_empty<K: copy + drop, V>(table: Table<K, V>) {
assert!(table.length == 0, Errors::invalid_state(ENOT_EMPTY));
destroy_empty_box<K, V, Box<V>>(&table);
drop_unchecked_box<K, V, Box<V>>(table)
}
pragma intrinsic;
add
Add a new entry to the table. Aborts if an entry for this key already exists. The entry itself is not stored in the table, and cannot be discovered from it.
public fun add<K: copy, drop, V>(table: &mut Table::Table<K, V>, key: K, val: V)
public fun add<K: copy + drop, V>(table: &mut Table<K, V>, key: K, val: V) {
add_box<K, V, Box<V>>(table, key, Box{val});
table.length = table.length + 1
}
pragma intrinsic;
borrow
Acquire an immutable reference to the value which key
maps to.
Aborts if there is no entry for key
.
public fun borrow<K: copy, drop, V>(table: &Table::Table<K, V>, key: K): &V
public fun borrow<K: copy + drop, V>(table: &Table<K, V>, key: K): &V {
&borrow_box<K, V, Box<V>>(table, key).val
}
pragma intrinsic;
borrow_with_default
Acquire an immutable reference to the value which key
maps to.
Returns specified default value if there is no entry for key
.
public fun borrow_with_default<K: copy, drop, V>(table: &Table::Table<K, V>, key: K, default: &V): &V
public fun borrow_with_default<K: copy + drop, V>(table: &Table<K, V>, key: K, default: &V): &V {
if (!contains(table, copy key)) {
default
} else {
borrow(table, copy key)
}
}
borrow_mut
Acquire a mutable reference to the value which key
maps to.
Aborts if there is no entry for key
.
public fun borrow_mut<K: copy, drop, V>(table: &mut Table::Table<K, V>, key: K): &mut V
public fun borrow_mut<K: copy + drop, V>(table: &mut Table<K, V>, key: K): &mut V {
&mut borrow_box_mut<K, V, Box<V>>(table, key).val
}
pragma intrinsic;
length
Returns the length of the table, i.e. the number of entries.
public fun length<K: copy, drop, V>(table: &Table::Table<K, V>): u64
pragma intrinsic;
empty
Returns true if this table is empty.
public fun empty<K: copy, drop, V>(table: &Table::Table<K, V>): bool
pragma intrinsic;
borrow_mut_with_default
Acquire a mutable reference to the value which key
maps to.
Insert the pair (key
, default
) first if there is no entry for key
.
public fun borrow_mut_with_default<K: copy, drop, V: drop>(table: &mut Table::Table<K, V>, key: K, default: V): &mut V
public fun borrow_mut_with_default<K: copy + drop, V: drop>(table: &mut Table<K, V>, key: K, default: V): &mut V {
if (!contains(table, copy key)) {
add(table, copy key, default)
};
borrow_mut(table, key)
}
pragma opaque, verify=false;
aborts_if false;
upsert
Insert the pair (key
, value
) if there is no entry for key
.
update the value of the entry for key
to value
otherwise
public fun upsert<K: copy, drop, V: drop>(table: &mut Table::Table<K, V>, key: K, value: V)
public fun upsert<K: copy + drop, V: drop>(table: &mut Table<K, V>, key: K, value: V) {
if (!contains(table, copy key)) {
add(table, copy key, value)
} else {
let ref = borrow_mut(table, key);
*ref = value;
};
}
remove
Remove from table
and return the value which key
maps to.
Aborts if there is no entry for key
.
public fun remove<K: copy, drop, V>(table: &mut Table::Table<K, V>, key: K): V
public fun remove<K: copy + drop, V>(table: &mut Table<K, V>, key: K): V {
let Box{val} = remove_box<K, V, Box<V>>(table, key);
table.length = table.length - 1;
val
}
pragma intrinsic;
contains
Returns true iff table
contains an entry for key
.
public fun contains<K: copy, drop, V>(table: &Table::Table<K, V>, key: K): bool
public fun contains<K: copy + drop, V>(table: &Table<K, V>, key: K): bool {
contains_box<K, V, Box<V>>(table, key)
}
pragma intrinsic;
new_table_handle
fun new_table_handle<K, V>(): address
native fun new_table_handle<K, V>(): address;
add_box
fun add_box<K: copy, drop, V, B>(table: &mut Table::Table<K, V>, key: K, val: Table::Box<V>)
native fun add_box<K: copy + drop, V, B>(table: &mut Table<K, V>, key: K, val: Box<V>);
borrow_box
fun borrow_box<K: copy, drop, V, B>(table: &Table::Table<K, V>, key: K): &Table::Box<V>
native fun borrow_box<K: copy + drop, V, B>(table: &Table<K, V>, key: K): &Box<V>;
borrow_box_mut
fun borrow_box_mut<K: copy, drop, V, B>(table: &mut Table::Table<K, V>, key: K): &mut Table::Box<V>
native fun borrow_box_mut<K: copy + drop, V, B>(table: &mut Table<K, V>, key: K): &mut Box<V>;
contains_box
fun contains_box<K: copy, drop, V, B>(table: &Table::Table<K, V>, key: K): bool
native fun contains_box<K: copy + drop, V, B>(table: &Table<K, V>, key: K): bool;
remove_box
fun remove_box<K: copy, drop, V, B>(table: &mut Table::Table<K, V>, key: K): Table::Box<V>
native fun remove_box<K: copy + drop, V, B>(table: &mut Table<K, V>, key: K): Box<V>;
destroy_empty_box
fun destroy_empty_box<K: copy, drop, V, B>(table: &Table::Table<K, V>)
native fun destroy_empty_box<K: copy + drop, V, B>(table: &Table<K, V>);
drop_unchecked_box
fun drop_unchecked_box<K: copy, drop, V, B>(table: Table::Table<K, V>)
native fun drop_unchecked_box<K: copy + drop, V, B>(table: Table<K, V>);
native fun spec_len<K, V>(t: Table<K, V>): num;
native fun spec_contains<K, V>(t: Table<K, V>, k: K): bool;
native fun spec_set<K, V>(t: Table<K, V>, k: K, v: V): Table<K, V>;
native fun spec_remove<K, V>(t: Table<K, V>, k: K): Table<K, V>;
native fun spec_get<K, V>(t: Table<K, V>, k: K): V;