Vector¶
| Spice | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
Construct a vector as a deep copy of another vector
Parameters
| Name | Type | Description |
|---|---|---|
original |
const Vector<T>& |
Vector to copy |
Methods¶
isEmpty¶
| Spice | |
|---|---|
Checks if the vector contains any items at the moment
Returns: bool — Empty or not empty
isFull¶
| Spice | |
|---|---|
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 | |
|---|---|
Add an item at the end of the vector
Parameters
| Name | Type | Description |
|---|---|---|
item |
const T& |
get¶
| Spice | |
|---|---|
Get an item at a certain index
Parameters
| Name | Type | Description |
|---|---|---|
index |
unsigned long |
Returns: T& — item at index
get¶
| Spice | |
|---|---|
Get an item at a certain index
Parameters
| Name | Type | Description |
|---|---|---|
index |
unsigned int |
Returns: T& — item at index
insertAt¶
| Spice | |
|---|---|
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 | |
|---|---|
Remove an item at a certain index
Parameters
| Name | Type | Description |
|---|---|---|
index |
unsigned long |
Index of the item to remove |
removeAt¶
| Spice | |
|---|---|
Remove an item at a certain index
Parameters
| Name | Type | Description |
|---|---|---|
index |
unsigned int |
Index of the item to remove |
front¶
| Spice | |
|---|---|
Get the first item in the vector
Returns: T& — item at index 0
back¶
| Spice | |
|---|---|
Get the last item in the vector
Returns: T& — item at index size - 1
clear¶
| Spice | |
|---|---|
Removes all items from the vector
reserve¶
| Spice | |
|---|---|
Reserves itemCount items
Parameters
| Name | Type | Description |
|---|---|---|
itemCount |
unsigned long |
reserve¶
| Spice | |
|---|---|
Reserves itemCount items
Parameters
| Name | Type | Description |
|---|---|---|
itemCount |
unsigned int |
getSize¶
| Spice | |
|---|---|
Retrieve the current size of the vector
Returns: long — Current size of the vector
getCapacity¶
| Spice | |
|---|---|
Retrieve the current capacity of the vector
Returns: long — Current capacity of the vector
getDataPtr¶
| Spice | |
|---|---|
Retrieve a pointer to the data of the vector
Returns: T*
pack¶
| Spice | |
|---|---|
Frees allocated memory that is not used by the queue
getIterator¶
| Spice | |
|---|---|
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 | |
|---|---|
Construct a vector iterator over the given vector
Parameters
| Name | Type | Description |
|---|---|---|
vector |
Vector<T>& |
Vector to iterate over |
Methods¶
get¶
| Spice | |
|---|---|
Returns the current item of the vector
Returns: T& — Reference to the current item
getIdx¶
| Spice | |
|---|---|
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 | |
|---|---|
Check if the iterator is valid
Returns: bool — true or false
next¶
| Spice | |
|---|---|
Returns the current item of the vector iterator and moves the cursor to the next item
Operators¶
operator=¶
| Spice | |
|---|---|
Copy-assign the contents of another vector into this one
Parameters
| Name | Type | Description |
|---|---|---|
newValue |
const Vector<T>& |
Vector to copy from |
operator[]¶
| Spice | |
|---|---|
Get an item at a certain index
Parameters
| Name | Type | Description |
|---|---|---|
v |
Vector<T>& |
|
index |
UIntOrULong |
Returns: T& — item at index
operator==¶
| Spice | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
Advances the cursor by one
Parameters
| Name | Type | Description |
|---|---|---|
it |
VectorIterator<T>& |
VectorIterator |
operator--¶
| Spice | |
|---|---|
Move the cursor back by one
Parameters
| Name | Type | Description |
|---|---|---|
it |
VectorIterator<T>& |
VectorIterator |
operator+=¶
| Spice | |
|---|---|
Advances the cursor by the given offset
Parameters
| Name | Type | Description |
|---|---|---|
it |
VectorIterator<T>& |
VectorIterator |
offset |
Numeric |
Offset |
operator-=¶
| Spice | |
|---|---|
Move the cursor back by the given offset
Parameters
| Name | Type | Description |
|---|---|---|
it |
VectorIterator<T>& |
VectorIterator |
offset |
Numeric |
Offset |