Similar to moveos_std::table
but the values are linked together, allowing for ordered insertion and
removal
- Resource
TablePlaceholder
- Resource
LinkedTable
- Struct
Node
- Constants
- Function
new
- Function
front
- Function
back
- Function
push_front
- Function
push_back
- Function
borrow
- Function
borrow_mut
- Function
prev
- Function
next
- Function
remove
- Function
pop_front
- Function
pop_back
- Function
contains
- Function
length
- Function
is_empty
- Function
destroy_empty
- Function
drop
use 0x1::option;
use 0x2::object;
struct TablePlaceholder has key
struct LinkedTable<K: copy, drop, store, V: store> has store, key
struct Node<K: copy, drop, store, V: store> has store
const ErrorTableIsEmpty: u64 = 1;
const ErrorTableNotEmpty: u64 = 0;
Creates a new, empty table
public fun new<K: copy, drop, store, V: store>(): linked_table::LinkedTable<K, V>
Returns the key for the first element in the table, or None if the table is empty
public fun front<K: copy, drop, store, V: store>(table: &linked_table::LinkedTable<K, V>): &option::Option<K>
Returns the key for the last element in the table, or None if the table is empty
public fun back<K: copy, drop, store, V: store>(table: &linked_table::LinkedTable<K, V>): &option::Option<K>
Inserts a key-value pair at the front of the table, i.e. the newly inserted pair will be
the first element in the table
Aborts with if the table already has an entry with
that key k: K
.
public fun push_front<K: copy, drop, store, V: store>(table: &mut linked_table::LinkedTable<K, V>, k: K, value: V)
Inserts a key-value pair at the back of the table, i.e. the newly inserted pair will be
the last element in the table
Aborts if the table already has an entry with
that key k: K
.
public fun push_back<K: copy, drop, store, V: store>(table: &mut linked_table::LinkedTable<K, V>, k: K, value: V)
Immutable borrows the value associated with the key in the table table: &LinkedTable<K, V>
.
Aborts if the table does not have an entry with
that key k: K
.
public fun borrow<K: copy, drop, store, V: store>(table: &linked_table::LinkedTable<K, V>, k: K): &V
Mutably borrows the value associated with the key in the table table: &mut LinkedTable<K, V>
.
Aborts if the table does not have an entry with
that key k: K
.
public fun borrow_mut<K: copy, drop, store, V: store>(table: &mut linked_table::LinkedTable<K, V>, k: K): &mut V
Borrows the key for the previous entry of the specified key k: K
in the table
table: &LinkedTable<K, V>
. Returns None if the entry does not have a predecessor.
Aborts if the table does not have an entry with
that key k: K
public fun prev<K: copy, drop, store, V: store>(table: &linked_table::LinkedTable<K, V>, k: K): &option::Option<K>
Borrows the key for the next entry of the specified key k: K
in the table
table: &LinkedTable<K, V>
. Returns None if the entry does not have a predecessor.
Aborts if the table does not have an entry with
that key k: K
public fun next<K: copy, drop, store, V: store>(table: &linked_table::LinkedTable<K, V>, k: K): &option::Option<K>
Removes the key-value pair in the table table: &mut LinkedTable<K, V>
and returns the value.
This splices the element out of the ordering.
Aborts if the table does not have an entry with
that key k: K
. Note: this is also what happens when the table is empty.
public fun remove<K: copy, drop, store, V: store>(table: &mut linked_table::LinkedTable<K, V>, k: K): V
Removes the front of the table table: &mut LinkedTable<K, V>
and returns the value.
Aborts with ETableIsEmpty
if the table is empty
public fun pop_front<K: copy, drop, store, V: store>(table: &mut linked_table::LinkedTable<K, V>): (K, V)
Removes the back of the table table: &mut LinkedTable<K, V>
and returns the value.
Aborts with ETableIsEmpty
if the table is empty
public fun pop_back<K: copy, drop, store, V: store>(table: &mut linked_table::LinkedTable<K, V>): (K, V)
Returns true iff there is a value associated with the key k: K
in table
table: &LinkedTable<K, V>
public fun contains<K: copy, drop, store, V: store>(table: &linked_table::LinkedTable<K, V>, k: K): bool
Returns the size of the table, the number of key-value pairs
public fun length<K: copy, drop, store, V: store>(table: &linked_table::LinkedTable<K, V>): u64
Returns true if the table is empty (if length
returns 0
)
public fun is_empty<K: copy, drop, store, V: store>(table: &linked_table::LinkedTable<K, V>): bool
Destroys an empty table
Aborts with ETableNotEmpty
if the table still contains values
public fun destroy_empty<K: copy, drop, store, V: store>(table: linked_table::LinkedTable<K, V>)
Drop a possibly non-empty table.
Usable only if the value type V
has the drop
ability
public fun drop<K: copy, drop, store, V: drop, store>(table_obj: object::Object<linked_table::LinkedTable<K, V>>)