A basic scalable vector library implemented using Table
.
- Struct
TableVec
- Constants
- Function
new
- Function
singleton
- Function
length
- Function
is_empty
- Function
borrow
- Function
push_back
- Function
borrow_mut
- Function
pop_back
- Function
destroy_empty
- Function
drop
- Function
swap
- Function
swap_remove
- Function
contains
use 0x2::table;
struct TableVec<Element: store> has store
const ErrorIndexOutOfBound: u64 = 1;
const ErrorTableNonEmpty: u64 = 2;
Create an empty TableVec.
public fun new<Element: store>(): table_vec::TableVec<Element>
Return a TableVec of size one containing element e
.
public fun singleton<Element: store>(e: Element): table_vec::TableVec<Element>
Return the length of the TableVec.
public fun length<Element: store>(t: &table_vec::TableVec<Element>): u64
Return if the TableVec is empty or not.
public fun is_empty<Element: store>(t: &table_vec::TableVec<Element>): bool
Acquire an immutable reference to the i
th element of the TableVec t
.
Aborts if i
is out of bounds.
public fun borrow<Element: store>(t: &table_vec::TableVec<Element>, i: u64): &Element
Add element e
to the end of the TableVec t
.
public fun push_back<Element: store>(t: &mut table_vec::TableVec<Element>, e: Element)
Return a mutable reference to the i
th element in the TableVec t
.
Aborts if i
is out of bounds.
public fun borrow_mut<Element: store>(t: &mut table_vec::TableVec<Element>, i: u64): &mut Element
Pop an element from the end of TableVec t
.
Aborts if t
is empty.
public fun pop_back<Element: store>(t: &mut table_vec::TableVec<Element>): Element
Destroy the TableVec t
.
Aborts if t
is not empty.
public fun destroy_empty<Element: store>(t: table_vec::TableVec<Element>)
Drop a possibly non-empty TableVec t
.
Usable only if the value type Element
has the drop
ability
public fun drop<Element: drop, store>(t: table_vec::TableVec<Element>)
Swaps the elements at the i
th and j
th indices in the TableVec t
.
Aborts if i
or j
is out of bounds.
public fun swap<Element: store>(t: &mut table_vec::TableVec<Element>, i: u64, j: u64)
Swap the i
th element of the TableVec t
with the last element and then pop the TableVec.
This is O(1), but does not preserve ordering of elements in the TableVec.
Aborts if i
is out of bounds.
public fun swap_remove<Element: store>(t: &mut table_vec::TableVec<Element>, i: u64): Element
Return if the TableVec t
contains the element at index i
.
public fun contains<Element: store>(t: &table_vec::TableVec<Element>, i: u64): bool