table_with_length - [mainnet]
Extends Table and provides functions such as length and the ability to be destroyed
use 0x1::error;use 0x1::table;
Constants
const EALREADY_EXISTS: u64 = 100;
const ENOT_EMPTY: u64 = 102;
const ENOT_FOUND: u64 = 101;
Structs
TableWithLength
Type of tables
struct TableWithLength<K: copy, drop, V> has store
Fields
-
inner: table::Table<K, V>
-
length: u64
Functions
new
Create a new Table.
public fun new<K: copy, drop, V: store>(): table_with_length::TableWithLength<K, V>
Implementation
public fun new<K: copy + drop, V: store>(): TableWithLength<K, V> { TableWithLength { inner: table::new<K, V>(), length: 0, }}
destroy_empty
Destroy a table. The table must be empty to succeed.
public fun destroy_empty<K: copy, drop, V>(self: table_with_length::TableWithLength<K, V>)
Implementation
public fun destroy_empty<K: copy + drop, V>(self: TableWithLength<K, V>) { assert!(self.length == 0, error::invalid_state(ENOT_EMPTY)); let TableWithLength { inner, length: _ } = self; inner.destroy_known_empty_unsafe()}
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>(self: &mut table_with_length::TableWithLength<K, V>, key: K, val: V)
Implementation
public fun add<K: copy + drop, V>(self: &mut TableWithLength<K, V>, key: K, val: V) { self.inner.add(key, val); self.length += 1;}
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>(self: &table_with_length::TableWithLength<K, V>, key: K): &V
Implementation
public fun borrow<K: copy + drop, V>(self: &TableWithLength<K, V>, key: K): &V { self.inner.borrow(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>(self: &mut table_with_length::TableWithLength<K, V>, key: K): &mut V
Implementation
public fun borrow_mut<K: copy + drop, V>(self: &mut TableWithLength<K, V>, key: K): &mut V { self.inner.borrow_mut(key)}
length
Returns the length of the table, i.e. the number of entries.
public fun length<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>): u64
Implementation
public fun length<K: copy + drop, V>(self: &TableWithLength<K, V>): u64 { self.length}
empty
Returns true if this table is empty.
public fun empty<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>): bool
Implementation
public fun empty<K: copy + drop, V>(self: &TableWithLength<K, V>): bool { self.length == 0}
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>(self: &mut table_with_length::TableWithLength<K, V>, key: K, default: V): &mut V
Implementation
public fun borrow_mut_with_default<K: copy + drop, V: drop>(self: &mut TableWithLength<K, V>, key: K, default: V): &mut V { if (self.inner.contains(key)) { self.inner.borrow_mut(key) } else { self.inner.add(key, default); self.length += 1; self.inner.borrow_mut(key) }}
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>(self: &mut table_with_length::TableWithLength<K, V>, key: K, value: V)
Implementation
public fun upsert<K: copy + drop, V: drop>(self: &mut TableWithLength<K, V>, key: K, value: V) { if (!self.inner.contains(key)) { self.add(copy key, value) } else { let ref = self.inner.borrow_mut(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>(self: &mut table_with_length::TableWithLength<K, V>, key: K): V
Implementation
public fun remove<K: copy + drop, V>(self: &mut TableWithLength<K, V>, key: K): V { let val = self.inner.remove(key); self.length -= 1; val}
contains
Returns true iff table
contains an entry for key
.
public fun contains<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>, key: K): bool
Implementation
public fun contains<K: copy + drop, V>(self: &TableWithLength<K, V>, key: K): bool { self.inner.contains(key)}
Specification
native fun spec_len<K, V>(t: TableWithLength<K, V>): num;
native fun spec_contains<K, V>(t: TableWithLength<K, V>, k: K): bool;
native fun spec_set<K, V>(t: TableWithLength<K, V>, k: K, v: V): TableWithLength<K, V>;
native fun spec_remove<K, V>(t: TableWithLength<K, V>, k: K): TableWithLength<K, V>;
native fun spec_get<K, V>(t: TableWithLength<K, V>, k: K): V;
TableWithLength
struct TableWithLength<K: copy, drop, V> has store
-
inner: table::Table<K, V>
-
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_add_override_if_exists = upsert, map_del_must_exist = remove, map_borrow = borrow, map_borrow_mut = borrow_mut, map_borrow_mut_with_default = borrow_mut_with_default, 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;
new
public fun new<K: copy, drop, V: store>(): table_with_length::TableWithLength<K, V>
pragma intrinsic;
destroy_empty
public fun destroy_empty<K: copy, drop, V>(self: table_with_length::TableWithLength<K, V>)
pragma intrinsic;
add
public fun add<K: copy, drop, V>(self: &mut table_with_length::TableWithLength<K, V>, key: K, val: V)
pragma intrinsic;
borrow
public fun borrow<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>, key: K): &V
pragma intrinsic;
borrow_mut
public fun borrow_mut<K: copy, drop, V>(self: &mut table_with_length::TableWithLength<K, V>, key: K): &mut V
pragma intrinsic;
length
public fun length<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>): u64
pragma intrinsic;
empty
public fun empty<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>): bool
pragma intrinsic;
borrow_mut_with_default
public fun borrow_mut_with_default<K: copy, drop, V: drop>(self: &mut table_with_length::TableWithLength<K, V>, key: K, default: V): &mut V
aborts_if false;pragma intrinsic;
upsert
public fun upsert<K: copy, drop, V: drop>(self: &mut table_with_length::TableWithLength<K, V>, key: K, value: V)
pragma intrinsic;
remove
public fun remove<K: copy, drop, V>(self: &mut table_with_length::TableWithLength<K, V>, key: K): V
pragma intrinsic;
contains
public fun contains<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>, key: K): bool
pragma intrinsic;