Skip to content

Vectors

Apoorv Singal edited this page Aug 30, 2020 · 3 revisions

A vector is a linear data structure used for storing a group of data with dynamic length.

Type signature

Vectors have type vec type.

Declaring a vector

vector: vec type;

Initializing a vector

Vectors can be initialized using compound literals.

vector: vec type;
vector = (vec type){el1, el2, el3, el4};

Vector properties and methods

  • vector.length returns the number of elements in the vector.
  • vector.capacity returns the maximum number of elements that the vector can have. Appending more elements than the capacity of the vector reallocates the vector with a higher capacity.
  • vector.push(value) appends value to the end of the vector.
  • vector.pop() removes and returns the last value of the vector.
  • vector.clone() creates and returns a copy of the vector.
  • vector.concat(anotherVector) appends all elements of anotherVector to the end of vector. Both vectors must have the same type.
  • vector.free() frees the vector from the heap. It is not necessary to manually free vectors because Volant also has garbage collection.
Clone this wiki locally