0x1::vector
A variable-sized container that can hold any type. Indexing is 0-based, and vectors are growable. This module has many native functions.
empty
length
borrow
push_back
borrow_mut
pop_back
destroy_empty
swap
singleton
reverse
append
is_empty
contains
index_of
remove
insert
swap_remove
flatten
The index into the vector is out of bounds
const EINDEX_OUT_OF_BOUNDS: u64 = 131072;
empty
Create an empty vector.
public fun empty<Element>(): vector<Element>
length
Return the length of the vector.
public fun length<Element>(v: &vector<Element>): u64
borrow
Acquire an immutable reference to the i
th element of the vector v
.
Aborts if i
is out of bounds.
public fun borrow<Element>(v: &vector<Element>, i: u64): &Element
push_back
Add element e
to the end of the vector v
.
public fun push_back<Element>(v: &mut vector<Element>, e: Element)
borrow_mut
Return a mutable reference to the i
th element in the vector v
.
Aborts if i
is out of bounds.
public fun borrow_mut<Element>(v: &mut vector<Element>, i: u64): &mut Element
public native fun borrow_mut<Element>(v: &mut vector<Element>, i: u64): &mut Element;
pop_back
Pop an element from the end of vector v
.
Aborts if v
is empty.
public fun pop_back<Element>(v: &mut vector<Element>): Element
destroy_empty
Destroy the vector v
.
Aborts if v
is not empty.
public fun destroy_empty<Element>(v: vector<Element>)
public native fun destroy_empty<Element>(v: vector<Element>);
swap
Swaps the elements at the i
th and j
th indices in the vector v
.
Aborts if i
or j
is out of bounds.
public fun swap<Element>(v: &mut vector<Element>, i: u64, j: u64)
singleton
Return an vector of size one containing element e
.
public fun singleton<Element>(e: Element): vector<Element>
public fun singleton<Element>(e: Element): vector<Element> {
let mut v = empty();
v.push_back(e);
v
}
reverse
Reverses the order of the elements in the vector v
in place.
public fun reverse<Element>(v: &mut vector<Element>)
public fun reverse<Element>(v: &mut vector<Element>) {
let len = v.length();
if (len == 0) return ();
let mut front_index = 0;
let mut back_index = len - 1;
while (front_index < back_index) {
v.swap(front_index, back_index);
front_index = front_index + 1;
back_index = back_index - 1;
}
}
append
Pushes all of the elements of the other
vector into the lhs
vector.
public fun append<Element>(lhs: &mut vector<Element>, other: vector<Element>)
public fun append<Element>(lhs: &mut vector<Element>, mut other: vector<Element>) {
other.reverse();
while (other.length() != 0) lhs.push_back(other.pop_back());
other.destroy_empty();
}
is_empty
Return true
if the vector v
has no elements and false
otherwise.
public fun is_empty<Element>(v: &vector<Element>): bool
contains
Return true if e
is in the vector v
.
Otherwise, returns false.
public fun contains<Element>(v: &vector<Element>, e: &Element): bool
public fun contains<Element>(v: &vector<Element>, e: &Element): bool {
let mut i = 0;
let len = v.length();
while (i < len) {
if (&v[i] == e) return true;
i = i + 1;
};
false
}
index_of
Return (true, i)
if e
is in the vector v
at index i
.
Otherwise, returns (false, 0)
.
public fun index_of<Element>(v: &vector<Element>, e: &Element): (bool, u64)
public fun index_of<Element>(v: &vector<Element>, e: &Element): (bool, u64) {
let mut i = 0;
let len = v.length();
while (i < len) {
if (&v[i] == e) return (true, i);
i = i + 1;
};
(false, 0)
}
remove
Remove the i
th element of the vector v
, shifting all subsequent elements.
This is O(n) and preserves ordering of elements in the vector.
Aborts if i
is out of bounds.
public fun remove<Element>(v: &mut vector<Element>, i: u64): Element
public fun remove<Element>(v: &mut vector<Element>, mut i: u64): Element {
let mut len = v.length();
// i out of bounds; abort
if (i >= len) abort EINDEX_OUT_OF_BOUNDS;
len = len - 1;
while (i < len) v.swap(i, {
i = i + 1;
i
});
v.pop_back()
}
insert
Insert e
at position i
in the vector v
.
If i
is in bounds, this shifts the old v[i]
and all subsequent elements to the right.
If i == v.length()
, this adds e
to the end of the vector.
This is O(n) and preserves ordering of elements in the vector.
Aborts if i > v.length()
public fun insert<Element>(v: &mut vector<Element>, e: Element, i: u64)
public fun insert<Element>(v: &mut vector<Element>, e: Element, mut i: u64) {
let len = v.length();
// i too big abort
if (i > len) abort EINDEX_OUT_OF_BOUNDS;
v.push_back(e);
while (i < len) {
v.swap(i, len);
i = i + 1
}
}
swap_remove
Swap the i
th 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<Element>(v: &mut vector<Element>, i: u64): Element
public fun swap_remove<Element>(v: &mut vector<Element>, i: u64): Element {
assert!(v.length() != 0, EINDEX_OUT_OF_BOUNDS);
let last_idx = v.length() - 1;
v.swap(i, last_idx);
v.pop_back()
}
flatten
Concatenate the vectors of v
into a single vector, keeping the order of the elements.
public fun flatten<T>(v: vector<vector<T>>): vector<T>