Module sui::table_vec
A basic scalable vector library implemented using Table.
- Struct TableVec
 - Constants
 - Function empty
 - 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
 
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::vector;
use sui::address;
use sui::dynamic_field;
use sui::hex;
use sui::object;
use sui::table;
use sui::tx_context;
Struct TableVec
public struct TableVec<phantom Element: store> has store
Fields
- 
contents: sui::table::Table<u64, Element> - The contents of the table vector.
 
Constants
const EIndexOutOfBound: u64 = 0;
const ETableNonEmpty: u64 = 1;
Function empty
Create an empty TableVec.
public fun empty<Element: store>(ctx: &mut sui::tx_context::TxContext): sui::table_vec::TableVec<Element>
Implementation
public fun empty<Element: store>(ctx: &mut TxContext): TableVec<Element> {
    TableVec {
        contents: table::new(ctx),
    }
}
Function singleton
Return a TableVec of size one containing element e.
public fun singleton<Element: store>(e: Element, ctx: &mut sui::tx_context::TxContext): sui::table_vec::TableVec<Element>
Function length
Return the length of the TableVec.
public fun length<Element: store>(t: &sui::table_vec::TableVec<Element>): u64
Function is_empty
Return if the TableVec is empty or not.
public fun is_empty<Element: store>(t: &sui::table_vec::TableVec<Element>): bool
Function borrow
Acquire an immutable reference to the ith element of the TableVec t.
Aborts if i is out of bounds.
public fun borrow<Element: store>(t: &sui::table_vec::TableVec<Element>, i: u64): &Element
Implementation
public fun borrow<Element: store>(t: &TableVec<Element>, i: u64): &Element {
    assert!(t.length() > i, EIndexOutOfBound);
    &t.contents[i]
}
Function push_back
Add element e to the end of the TableVec t.
public fun push_back<Element: store>(t: &mut sui::table_vec::TableVec<Element>, e: Element)
Function borrow_mut
Return a mutable reference to the ith element in the TableVec t.
Aborts if i is out of bounds.
public fun borrow_mut<Element: store>(t: &mut sui::table_vec::TableVec<Element>, i: u64): &mut Element
Implementation
public fun borrow_mut<Element: store>(t: &mut TableVec<Element>, i: u64): &mut Element {
    assert!(t.length() > i, EIndexOutOfBound);
    &mut t.contents[i]
}
Function pop_back
Pop an element from the end of TableVec t.
Aborts if t is empty.
public fun pop_back<Element: store>(t: &mut sui::table_vec::TableVec<Element>): Element
Function destroy_empty
Destroy the TableVec t.
Aborts if t is not empty.
public fun destroy_empty<Element: store>(t: sui::table_vec::TableVec<Element>)
Implementation
public fun destroy_empty<Element: store>(t: TableVec<Element>) {
    assert!(length(&t) == 0, ETableNonEmpty);
    let TableVec { contents } = t;
    contents.destroy_empty();
}
Function drop
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: sui::table_vec::TableVec<Element>)
Function swap
Swaps the elements at the ith and jth indices in the TableVec t.
Aborts if i or j is out of bounds.
public fun swap<Element: store>(t: &mut sui::table_vec::TableVec<Element>, i: u64, j: u64)
Implementation
public fun swap<Element: store>(t: &mut TableVec<Element>, i: u64, j: u64) {
    assert!(t.length() > i, EIndexOutOfBound);
    assert!(t.length() > j, EIndexOutOfBound);
    if (i == j) {
        return
    };
    let element_i = t.contents.remove(i);
    let element_j = t.contents.remove(j);
    t.contents.add(j, element_i);
    t.contents.add(i, element_j);
}
Function swap_remove
Swap the ith 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 sui::table_vec::TableVec<Element>, i: u64): Element
Implementation
public fun swap_remove<Element: store>(t: &mut TableVec<Element>, i: u64): Element {
    assert!(t.length() > i, EIndexOutOfBound);
    let last_idx = t.length() - 1;
    t.swap(i, last_idx);
    t.pop_back()
}