A wrapper for std::vector. More...
#include <array.hpp>
Public Member Functions | |
array () | |
Default constructor. | |
array (const array &other) | |
Copy constructor. | |
array (const std::vector< T > &other) | |
Copy from std::vector. | |
template<typename... Args> | |
array (const T &arg1, const Args &...args) | |
List-initialized constructor. | |
array (const std::initializer_list< T > &other) | |
Construct from a generic initializer list. | |
virtual | ~array () |
Destructor. | |
void | clear () |
Clear the data in the array. | |
void | increase (int newSize) noexcept |
Increase the space allocated for this array. | |
virtual int | add (const T &object) |
Add an object to the array. | |
void | add (const array &other) noexcept |
Add another array to this array. | |
int | push (const T &object) noexcept |
Add an object to the array. | |
void | push (const array &other) noexcept |
Add another array to this array. | |
T | pop () |
Remove the last element from this array. | |
array & | insert (const T &, int) |
Insert an object into the array. | |
void | append (const T &) |
Append an object to the end of the array. | |
array & | remove (int) |
Remove an object from the array. | |
array & | remove (int, int) |
Remove all elements in a subset of the array. | |
array & | replace (int, int, const T &) |
Replace all objects in the given range with an object. | |
array & | replace (int, int, const array< T > &) |
Replace all objects in the given range with an array of objects. | |
array | subset (int index, int count) const |
Get a contiguous subset of the elements in the array. | |
size_t | size () const noexcept override |
Get the size of the array. | |
int | length () const noexcept override |
Get the length of the array. | |
T & | at (int) |
Function to get the object at the given index. | |
const T & | at (int) const override |
Const function to get the object at the given index. | |
const T & | operator[] (int index) const override |
Function to get the object at the given index. | |
T & | operator[] (int index) |
Function to get the object at the given index. | |
virtual int | find (const T &object) const |
Find the index of a given object in the array. | |
bool | contains (const T &object) const noexcept |
Check if a given object is in the array. | |
void | sort () noexcept |
Sort the array based on default comparison operator. | |
void | sort (std::function< bool(const T &, const T &)> lambda) noexcept |
Sort the array based on an arbitrary function. | |
array | sorted () const noexcept |
Sort the array based on default comparison operator. | |
array | sorted (std::function< bool(const T &, const T &)> lambda) const noexcept |
Sort the array based on an arbitrary function. | |
virtual void | shuffle () noexcept |
Shuffle the elements of the array into a random order. | |
array | shuffled () const noexcept |
Shuffle the elements of the array into a random order. | |
virtual void | reverse () noexcept |
Reverse the order of all elements in the array. | |
array | reversed () const noexcept |
Reverse the order of all elements in the array. | |
array & | operator= (const array &other) |
Array assignment operator. | |
array & | operator= (const std::initializer_list< T > &other) |
Initializer list assignment operator. | |
bool | operator== (const array &other) const |
Check whether two arrays' contents are the same. | |
bool | operator> (const array &other) const |
Array greater-than operator. | |
bool | operator< (const array &other) const |
Array less-than operator. | |
bool | operator>= (const array &other) const |
Array greater-than-or-equal operator. | |
bool | operator<= (const array &other) const |
Array less-than-or-equal operator. | |
virtual bool | operator() (const T &arg1, const T &arg2) const |
Callable operator. | |
bool | isValid (int index) const |
Check if an index is within the bounds of the array. | |
array & | swap (int index1, int index2) |
Swap two elements in an array. | |
template<typename U > | |
array< U > | map (std::function< U(const T &)> lambda) const |
Applies a transformation function to each element of the array and returns a new array with the results. | |
array | filter (std::function< bool(const T &)> lambda) const |
Filters the array based on a predicate and returns a new array containing the elements that satisfy the predicate. | |
T | reduce (const T &defaultValue, std::function< T(const T &, const T &)> lambda) const |
Reduces the array to a single value by applying a binary operation cumulatively to the elements. | |
T | randomElement () const |
Get a random element from the array. | |
array | randomElements (int count) const noexcept |
Get N random elements from the array. | |
T * | begin () const noexcept override |
Get pointer to the beginning of the array. | |
T * | end () const noexcept override |
Get pointer to the end of the array. | |
void | save (cereal::JSONOutputArchive &ar) const |
JSON specialization of serialization output. | |
void | save (cereal::XMLOutputArchive &ar) const |
XML specialization of serialization output. | |
template<typename archive > | |
void | save (archive &ar) const |
Binary specialization of serialization output. | |
void | load (cereal::JSONInputArchive &ar) |
JSON specialization of serialization input. | |
void | load (cereal::XMLInputArchive &ar) |
XML specialization of serialization input. | |
template<class archive > | |
void | load (archive &ar) |
Binary specialization of serialization input. | |
![]() | |
virtual | ~sizable () noexcept |
Virtual destructor. | |
![]() | |
virtual | ~indexable () noexcept |
Virtual destructor. | |
![]() | |
virtual | ~iterable () noexcept |
Virtual destructor. | |
Protected Member Functions | |
void | init (const T &arg1) |
Helper function for single object initialization. | |
template<typename... Args> | |
void | init (const T &arg1, const Args &...args) |
Helper function for brace-enclosed list initialization. | |
virtual bool | eq (const T &arg1, const T &arg2) const |
Check if two objects are equal. | |
virtual bool | gt (const T &arg1, const T &arg2) const |
Check if one object is greater than another. | |
virtual bool | lt (const T &arg1, const T &arg2) const |
Check if one object is less than another. | |
Protected Attributes | |
std::vector< T > | array_data |
The data in the array. | |
A wrapper for std::vector.
This class is a wrapper for the std::vector class that adds ease of use and hides implementation from the user.
/note This will only compile for objects that are copyable. Use std::vector for non-copyable objects.
List-initialized constructor.
Constructs the array with an arbitrary number of elements already contained.
Syntax: array<T> X {arg1, arg2, ...}; array<T> X = {arg1, arg2, ...};
arg1 | initializing data. |
args | cont. initializing data. |
Construct from a generic initializer list.
other | The list to initialize this array with. |
Add an object to the array.
Adds the given data to a position in the array. That position is up to implementation.
object | the data to add to the array. |
Reimplemented in z::core::sortedArray< T >, and z::core::sortedArray< zstring * >.
Append an object to the end of the array.
Appends the given data to the end of the array.
object | the data to aappend to the array. |
Const function to get the object at the given index.
Identical behavior to at(int), but allows indexing with square brackets.
index | the index of the desired object. |
std::out_of_range | if index is an invalid index. |
Implements z::core::indexable< T >.
|
inlineoverridevirtualnoexcept |
Get pointer to the beginning of the array.
This member function should not be used directly. It is meant for C++11's range-based for loop syntax.
Implements z::core::iterable< ITER >.
Check if a given object is in the array.
object | the object to search for. |
|
inlineoverridevirtualnoexcept |
Get pointer to the end of the array.
This member function should not be used directly. It is meant for C++11's range-based for loop syntax.
Implements z::core::iterable< ITER >.
|
inlineprotectedvirtual |
Check if two objects are equal.
This member function allows object comparison to be different for different array types (e.g. reference arrays will need to sort by value after dereference).
arg1 | First object to compare. |
arg2 | Second object to compare. |
Reimplemented in z::core::refArray< T >, and z::core::sortedRefArray< T >.
Filters the array based on a predicate and returns a new array containing the elements that satisfy the predicate.
This function iterates through the array, applies the given lambda function as a predicate to each element, and adds elements that satisfy the predicate to the resulting array.
lambda | A function that takes a constant reference to an element of type T and returns a boolean indicating whether the element should be included. |
Find the index of a given object in the array.
Locates the desired index using a linear search, as the array is expected to be unsorted.
object | the object to search for. |
Reimplemented in z::core::sortedArray< T >, and z::core::sortedArray< zstring * >.
|
inlineprotectedvirtual |
Check if one object is greater than another.
This member function allows object comparison to be different for different array types (e.g. reference arrays will need to sort by value after dereference).
arg1 | First object to compare. |
arg2 | Second object to compare. |
Reimplemented in z::core::refArray< T >, and z::core::sortedRefArray< T >.
Increase the space allocated for this array.
If this array currently has fewer than newSize elements allocated, enough space is reallocated to hold at least that many characters.
newSize | The minimum number of elements this array should be able to contain. |
Helper function for single object initialization.
arg1 | The object to initialize the array with. |
|
inlineprotected |
Helper function for brace-enclosed list initialization.
arg1 | The first object to add to the array. |
args | Any following objects to add to the array. |
Insert an object into the array.
Inserts an object into the given index in the array, if possible.
object | the data to add to the array. |
index | the index in the array to insert the object. |
Check if an index is within the bounds of the array.
index | the index to check. |
|
overridevirtualnoexcept |
Get the length of the array.
Implements z::core::arrayLike< const T &, T * >.
|
inline |
Binary specialization of serialization input.
ar | The input archive. |
|
inline |
JSON specialization of serialization input.
ar | The input archive. |
|
inline |
XML specialization of serialization input.
ar | The input archive. |
|
inlineprotectedvirtual |
Check if one object is less than another.
This member function allows object comparison to be different for different array types (e.g. reference arrays will need to sort by value after dereference).
arg1 | First object to compare. |
arg2 | Second object to compare. |
Reimplemented in z::core::refArray< T >, and z::core::sortedRefArray< T >.
Applies a transformation function to each element of the array and returns a new array with the results.
This function iterates through each element of the array, applies the provided lambda function, and stores the result in a new array of type U
. The new array is returned after all elements have been processed.
U | The type of elements in the resulting array. |
lambda | A function that takes a constant reference to an element of type T and returns an element of type U . |
|
virtual |
Callable operator.
This is used to compare array elements for sorting.
arg1 | The first element to compare. |
arg2 | The second element to compare. |
Reimplemented in z::core::refArray< T >, and z::core::sortedRefArray< T >.
bool z::core::array< T >::operator< | ( | const array< T > & | other | ) | const |
Array less-than operator.
other | the array to compare with this one. |
|
inline |
Array less-than-or-equal operator.
other | the array to compare with this one. |
array< T > & z::core::array< T >::operator= | ( | const array< T > & | other | ) |
Array assignment operator.
Clear the contents of this array and create a copy of another array's contents into this one.
other | the array to copy from. |
array< T > & z::core::array< T >::operator= | ( | const std::initializer_list< T > & | other | ) |
Initializer list assignment operator.
Clear the contents of this array and copy the contents of an initializer list into this array.
other | The initializer list to copy from. |
bool z::core::array< T >::operator== | ( | const array< T > & | other | ) | const |
Check whether two arrays' contents are the same.
other | the array to compare with this one. |
bool z::core::array< T >::operator> | ( | const array< T > & | other | ) | const |
Array greater-than operator.
other | the array to compare with this one. |
|
inline |
Array greater-than-or-equal operator.
other | the array to compare with this one. |
|
inline |
Function to get the object at the given index.
Identical behavior to at(int), but allows indexing with square brackets.
index | the index of the desired object. |
|
inlineoverridevirtual |
Function to get the object at the given index.
Identical behavior to at(int), but allows indexing with square brackets.
index | the index of the desired object. |
Implements z::core::indexable< T >.
|
inline |
Remove the last element from this array.
Pops the last element, returning it.
std::out_of_range | if there are no elements in the array. |
|
inline |
Get a random element from the array.
Selects a random element from the array, if one exists.
std::out_of_range | if the array is empty. |
Get N random elements from the array.
Selects up to N random elements from the array. If N is greater than the size of the array, all elements are returned in a random order.
count | The number of random elements to select. |
T z::core::array< T >::reduce | ( | const T & | defaultValue, |
std::function< T(const T &, const T &)> | lambda | ||
) | const |
Reduces the array to a single value by applying a binary operation cumulatively to the elements.
This function applies a binary operation (provided as a lambda) to combine the elements of the array into a single value. If the array is empty, the provided default value is returned.
defaultValue | The value to return if the array is empty. |
lambda | A function that takes two elements of type T and returns their combined result of type T . |
Remove an object from the array.
index | the index of the object to be removed. |
Remove all elements in a subset of the array.
index | the index of the first object to be removed. |
count | the number of objects to be removed. |
Replace all objects in the given range with an array of objects.
index | the index of the first object to replace. |
count | the number of objects to replace. |
other | the array to copy from. |
Replace all objects in the given range with an object.
index | the index of the first object to replace. |
count | the number of objects to replace. |
object | the object to insert into the gap. |
Reverse the order of all elements in the array.
Reverses the array in-place, mutating existing values.
Reimplemented in z::core::sortedArray< T >, and z::core::sortedArray< zstring * >.
|
inlinenoexcept |
Reverse the order of all elements in the array.
Creates a new, reversed version of the array, without mutating existing values.
|
inline |
Binary specialization of serialization output.
ar | The output archive. |
|
inline |
JSON specialization of serialization output.
ar | The output archive. |
|
inline |
XML specialization of serialization output.
ar | The output archive. |
Shuffle the elements of the array into a random order.
Shuffles the array in-place, mutating existing values.
Reimplemented in z::core::sortedArray< T >, and z::core::sortedArray< zstring * >.
|
inlinenoexcept |
Shuffle the elements of the array into a random order.
Creates a new, shuffled version of the array, without mutating existing values.
|
overridevirtualnoexcept |
Get the size of the array.
Implements z::core::sizable.
|
inlinenoexcept |
Sort the array based on default comparison operator.
Sorts the array in-place, mutating existing values.
Sort the array based on an arbitrary function.
Sorts the array in-place, mutating existing values.
lambda | A function that takes two elements, and returns true if the first item comes after the second (e.g. A greater than B). |
|
inlinenoexcept |
Sort the array based on default comparison operator.
Creates a new, sorted version of the array, without mutating existing values.
|
inlinenoexcept |
Sort the array based on an arbitrary function.
Creates a new, sorted version of the array, without mutating existing values.
lambda | A function that takes two elements, and returns true if the first item comes after the second (e.g. A greater than B). |
Get a contiguous subset of the elements in the array.
Copies all elements in the given range, inclusive. If either of the parameters is -1, gives an empty array. If the stop parameter is less than start, then the subset is copied in reverse order.
index | the index of the first object to copy. |
count | the number of objects to copy. |
Swap two elements in an array.
index1 | The index of the first element to swap. |
index2 | The index of the second element to swap. |
std::out_of_range | if either of the indexes is an invalid index. |