Skip to content

Vector

Spice
import "std/data/vector";

Vector<T> struct

Implements: IIterable<T>

A vector in Spice is a commonly used data structure, which can be used to represent a list of items.

Time complexity:
Insert: O(1)
Delete: O(n * m); n = deleted elements, m = moved elements
Search: O(n)

Vectors pre-allocate space using an initial size and a resize factor to not have to re-allocate with every item pushed.

Constructors

ctor

Spice
public p Vector.ctor(unsigned long initialCapacity = INITIAL_CAPACITY)

Construct a vector, pre-allocating space for the given number of items

Parameters

Name Type Description
initialCapacity unsigned long Number of items to pre-allocate space for (default: INITIAL_CAPACITY)

ctor

Spice
public p Vector.ctor(unsigned int initialCapacity)

Construct a vector, pre-allocating space for the given number of items

Parameters

Name Type Description
initialCapacity unsigned int Number of items to pre-allocate space for

ctor

Spice
public p Vector.ctor(unsigned long initAllocItems, const T& defaultValue)

Construct a vector pre-filled with a number of copies of a default value

Parameters

Name Type Description
initAllocItems unsigned long Number of items to insert
defaultValue const T& Value to fill the vector with

ctor

Spice
public p Vector.ctor(const Vector<T>& original)

Construct a vector as a deep copy of another vector

Parameters

Name Type Description
original const Vector<T>& Vector to copy

Methods

isEmpty

Spice
public f<bool> Vector.isEmpty()

Checks if the vector contains any items at the moment

Returns: bool — Empty or not empty

isFull

Spice
public f<bool> Vector.isFull()

Checks if the vector exhausts its capacity and needs to resize at the next call of push

Returns: bool — Full or not full

pushBack

Spice
public p Vector.pushBack<T>(const T& item)

Add an item at the end of the vector

Parameters

Name Type Description
item const T&

get

Spice
public f<T&> Vector.get(unsigned long index)

Get an item at a certain index

Parameters

Name Type Description
index unsigned long

Returns: T& — item at index

get

Spice
public f<T&> Vector.get(unsigned int index)

Get an item at a certain index

Parameters

Name Type Description
index unsigned int

Returns: T& — item at index

insertAt

Spice
public p Vector.insertAt(unsigned long index, const T& item)

Insert an item at a certain index

Parameters

Name Type Description
index unsigned long Index to insert the item at
item const T& Item to insert

removeAt

Spice
public p Vector.removeAt(unsigned long index)

Remove an item at a certain index

Parameters

Name Type Description
index unsigned long Index of the item to remove

removeAt

Spice
public p Vector.removeAt(unsigned int index)

Remove an item at a certain index

Parameters

Name Type Description
index unsigned int Index of the item to remove

front

Spice
public f<T&> Vector.front()

Get the first item in the vector

Returns: T& — item at index 0

back

Spice
public f<T&> Vector.back()

Get the last item in the vector

Returns: T& — item at index size - 1

clear

Spice
public p Vector.clear()

Removes all items from the vector

reserve

Spice
public p Vector.reserve(unsigned long itemCount)

Reserves itemCount items

Parameters

Name Type Description
itemCount unsigned long

reserve

Spice
public p Vector.reserve(unsigned int itemCount)

Reserves itemCount items

Parameters

Name Type Description
itemCount unsigned int

getSize

Spice
public f<long> Vector.getSize()

Retrieve the current size of the vector

Returns: long — Current size of the vector

getCapacity

Spice
public f<long> Vector.getCapacity()

Retrieve the current capacity of the vector

Returns: long — Current capacity of the vector

getDataPtr

Spice
public f<T*> Vector.getDataPtr()

Retrieve a pointer to the data of the vector

Returns: T*

pack

Spice
public p Vector.pack()

Frees allocated memory that is not used by the queue

getIterator

Spice
public f<VectorIterator<T>> Vector.getIterator()

Retrieve a forward iterator for the vector

Returns: VectorIterator<T>

VectorIterator<T> struct

Implements: IIterator<T>

Iterator to iterate over a vector data structure

Constructors

ctor

Spice
public p VectorIterator.ctor<T>(Vector<T>& vector)

Construct a vector iterator over the given vector

Parameters

Name Type Description
vector Vector<T>& Vector to iterate over

Methods

get

Spice
public inline f<T&> VectorIterator.get()

Returns the current item of the vector

Returns: T& — Reference to the current item

getIdx

Spice
public inline f<Pair<unsigned long, T&>> VectorIterator.getIdx()

Returns the current index and the current item of the vector

Returns: Pair<unsigned long, T&> — Pair of current index and reference to current item

isValid

Spice
public inline f<bool> VectorIterator.isValid()

Check if the iterator is valid

Returns: bool — true or false

next

Spice
public inline p VectorIterator.next()

Returns the current item of the vector iterator and moves the cursor to the next item

Operators

operator=

Spice
public p operator=<T>(Vector<T>& this, const Vector<T>& newValue)

Copy-assign the contents of another vector into this one

Parameters

Name Type Description
newValue const Vector<T>& Vector to copy from

operator[]

Spice
public f<T&> operator[]<T, UIntOrULong>(Vector<T>& v, UIntOrULong index)

Get an item at a certain index

Parameters

Name Type Description
v Vector<T>&
index UIntOrULong

Returns: T& — item at index

operator==

Spice
public f<bool> operator==<T>(const Vector<T>& lhs, const Vector<T>& rhs)

Check if two vectors are equal, i.e. they have the same size and equal contents

Parameters

Name Type Description
lhs const Vector<T>&
rhs const Vector<T>&

Returns: bool — Equal or not equal

operator!=

Spice
public f<bool> operator!=<T>(const Vector<T>& lhs, const Vector<T>& rhs)

Check if two vectors are not equal

Parameters

Name Type Description
lhs const Vector<T>&
rhs const Vector<T>&

Returns: bool — Not equal or equal

operator++

Spice
public inline p operator++<T>(VectorIterator<T>& it)

Advances the cursor by one

Parameters

Name Type Description
it VectorIterator<T>& VectorIterator

operator--

Spice
public inline p operator--<T>(VectorIterator<T>& it)

Move the cursor back by one

Parameters

Name Type Description
it VectorIterator<T>& VectorIterator

operator+=

Spice
public inline p operator+=<T, Numeric>(VectorIterator<T>& it, Numeric offset)

Advances the cursor by the given offset

Parameters

Name Type Description
it VectorIterator<T>& VectorIterator
offset Numeric Offset

operator-=

Spice
public inline p operator-=<T, Numeric>(VectorIterator<T>& it, Numeric offset)

Move the cursor back by the given offset

Parameters

Name Type Description
it VectorIterator<T>& VectorIterator
offset Numeric Offset