- Struct
BigVector - Constants
- Function
empty - Function
singleton - Function
destroy_empty - Function
destroy - Function
borrow - Function
borrow_mut - Function
append - Function
push_back - Function
pop_back - Function
remove - Function
swap_remove - Function
swap - Function
reverse - Function
index_of - Function
contains - Function
to_vector - Function
length - Function
is_empty
use 0x1::vector;
use 0x2::table;
A scalable vector implementation based on tables where elements are grouped into buckets.
Each bucket has a capacity of bucket_size elements.
struct BigVector<T: store> has store
bucket_size cannot be 0
const ErrorBucketSizeIllegal: u64 = 4;
Vector index is out of bounds
const ErrorIndexOutOfBound: u64 = 1;
Cannot pop back from an empty vector
const ErrorVectorEmpty: u64 = 3;
Cannot destroy a non-empty vector
const ErrorVectorNotEmpty: u64 = 2;
Regular Vector API Create an empty vector.
public fun empty<T: store>(bucket_size: u64): big_vector::BigVector<T>
Create a vector of length 1 containing the passed in element.
public fun singleton<T: store>(element: T, bucket_size: u64): big_vector::BigVector<T>
Destroy the vector v.
Aborts if v is not empty.
public fun destroy_empty<T: store>(v: big_vector::BigVector<T>)
Destroy the vector v if T has drop
public fun destroy<T: drop, store>(v: big_vector::BigVector<T>)
Acquire an immutable reference to the ith element of the vector v.
Aborts if i is out of bounds.
public fun borrow<T: store>(v: &big_vector::BigVector<T>, i: u64): &T
Return a mutable reference to the ith element in the vector v.
Aborts if i is out of bounds.
public fun borrow_mut<T: store>(v: &mut big_vector::BigVector<T>, i: u64): &mut T
Empty and destroy the other vector, and push each of the elements in the other vector onto the lhs vector in the same order as they occurred in other. Disclaimer: This function is costly. Use it at your own discretion.
public fun append<T: store>(lhs: &mut big_vector::BigVector<T>, other: big_vector::BigVector<T>)
Add element val to the end of the vector v. It grows the buckets when the current buckets are full.
This operation will cost more gas when it adds new bucket.
public fun push_back<T: store>(v: &mut big_vector::BigVector<T>, val: T)
Pop an element from the end of vector v. It doesn't shrink the buckets even if they're empty.
Call shrink_to_fit explicitly to deallocate empty buckets.
Aborts if v is empty.
public fun pop_back<T: store>(v: &mut big_vector::BigVector<T>): T
Remove the element at index i in the vector v and return the owned value that was previously stored at i in v. All elements occurring at indices greater than i will be shifted down by 1. Will abort if i is out of bounds. Disclaimer: This function is costly. Use it at your own discretion.
public fun remove<T: store>(v: &mut big_vector::BigVector<T>, i: u64): T
Swap the ith element of the vector v with the last element and then pop the vector.
This is O(1), but does not preserve ordering of elements in the vector.
Aborts if i is out of bounds.
public fun swap_remove<T: store>(v: &mut big_vector::BigVector<T>, i: u64): T
Swap the elements at the i'th and j'th indices in the vector v. Will abort if either of i or j are out of bounds for v.
public fun swap<T: store>(v: &mut big_vector::BigVector<T>, i: u64, j: u64)
Reverse the order of the elements in the vector v in-place. Disclaimer: This function is costly. Use it at your own discretion.
public fun reverse<T: store>(v: &mut big_vector::BigVector<T>)
Return the index of the first occurrence of an element in v that is equal to e. Returns (true, index) if such an element was found, and (false, 0) otherwise. Disclaimer: This function is costly. Use it at your own discretion.
public fun index_of<T: store>(v: &big_vector::BigVector<T>, val: &T): (bool, u64)
Return if an element equal to e exists in the vector v. Disclaimer: This function is costly. Use it at your own discretion.
public fun contains<T: store>(v: &big_vector::BigVector<T>, val: &T): bool
Convert a big vector to a native vector, which is supposed to be called mostly by view functions to get an atomic view of the whole vector. Disclaimer: This function may be costly as the big vector may be huge in size. Use it at your own discretion.
public fun to_vector<T: copy, store>(v: &big_vector::BigVector<T>): vector<T>
Return the length of the vector.
public fun length<T: store>(v: &big_vector::BigVector<T>): u64
Return true if the vector v has no elements and false otherwise.
public fun is_empty<T: store>(v: &big_vector::BigVector<T>): bool