Skip to content

Latest commit

 

History

History
326 lines (159 loc) · 10.4 KB

big_vector.md

File metadata and controls

326 lines (159 loc) · 10.4 KB

Module 0x2::big_vector

Struct BigVector

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

Constants

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;

Function empty

Regular Vector API Create an empty vector.

public fun empty<T: store>(bucket_size: u64): big_vector::BigVector<T>

Function singleton

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>

Function destroy_empty

Destroy the vector v. Aborts if v is not empty.

public fun destroy_empty<T: store>(v: big_vector::BigVector<T>)

Function destroy

Destroy the vector v if T has drop

public fun destroy<T: drop, store>(v: big_vector::BigVector<T>)

Function borrow

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

Function borrow_mut

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

Function append

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>)

Function push_back

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)

Function pop_back

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

Function remove

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

Function swap_remove

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

Function swap

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)

Function reverse

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>)

Function index_of

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)

Function contains

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

Function to_vector

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>

Function length

Return the length of the vector.

public fun length<T: store>(v: &big_vector::BigVector<T>): u64

Function is_empty

Return true if the vector v has no elements and false otherwise.

public fun is_empty<T: store>(v: &big_vector::BigVector<T>): bool