4#include <initializer_list>
9#include "arrayLike.hpp"
12#include "typeChecks.hpp"
15#if __has_include(<cereal/cereal.hpp>)
16#include <cereal/archives/json.hpp>
17#include <cereal/archives/xml.hpp>
22#if __cplusplus < 201703L
27class vec_iter :
public std::vector<T>::iterator {};
31void sort(vec_iter<T> _begin, vec_iter<T> _end, std::function<
bool(
const T &,
const T &)> _lambda)
noexcept {
35 for (
auto i = _begin; i != _end - 1; ++i) {
36 if (_lambda(*i, *(i + 1))) {
37 std::swap(*i, *(i + 1));
45template <
class _RAIter,
class _UGenerator>
46void shuffle(_RAIter _begin, _RAIter _end, _UGenerator &&g) {
47 for (_RAIter i = _end - 1; i > _begin; --i) {
48 std::uniform_int_distribution<std::size_t> d(0, i - _begin);
49 std::iter_swap(i, _begin + d(g));
93 template <
typename...
Args>
169 template <
typename...
Args>
208 virtual int add(
const T &
object) {
242 inline int push(
const T &
object)
noexcept {
270 throw std::out_of_range(
"No more elements in array.");
363 virtual int find(
const T &
object)
const {
381 return find(
object) > -1;
400 void sort(std::function<
bool(
const T &,
const T &)> lambda)
noexcept {
425 array sorted(std::function<
bool(
const T &,
const T &)> lambda)
const noexcept {
439 std::random_device device;
466 for (
int i = 0;
i < (
len / 2); ++
i) {
536 template <
typename U>
574 throw std::out_of_range(
"Array is empty");
577 std::random_device device;
598 std::random_device device;
638#if __has_include(<cereal/cereal.hpp>)
643 void save(cereal::JSONOutputArchive &
ar)
const {
654 void save(cereal::XMLOutputArchive &
ar)
const {
664 template <
typename archive>
676 void load(cereal::JSONInputArchive &
ar) {
682 for (
int i = 0;
i <
sz;
i++) {
692 void load(cereal::XMLInputArchive &
ar) {
708 template <
class archive>
737template <
typename...
Args>
1002 end = index + count;
1004 start = index + count + 1;
1023template <
typename T>
1034template <
typename T>
1051template <
typename T>
1068template <
typename T>
1084template <
typename T>
1098 end = index + count;
1100 start = index + count + 1;
1131template <
typename T>
1141 end = index + count;
1143 start = index + count + 1;
1165template <
typename T>
1181 end = index + count;
1183 start = index + count + 1;
1197 if (
end - start > 0) {
1200 for (
int i = start;
i <
end;
i++) {
1207template <
typename T>
1212template <
typename T>
1220template <
typename T>
1228template <
typename T>
1229template <
typename U>
1241template <
typename T>
1255template <
typename T>
1263 for (
int i = 1;
i <
len; ++
i) {
1273#define zarray z::core::array
An interface for all objects that can be both iterated over and directly indexed.
Definition arrayLike.hpp:13
A wrapper for std::vector.
Definition array.hpp:75
bool isValid(int index) const
Check if an index is within the bounds of the array.
Definition array.hpp:1213
void increase(int newSize) noexcept
Increase the space allocated for this array.
Definition array.hpp:192
int push(const T &object) noexcept
Add an object to the array.
Definition array.hpp:242
virtual bool eq(const T &arg1, const T &arg2) const
Check if two objects are equal.
Definition array.hpp:111
bool contains(const T &object) const noexcept
Check if a given object is in the array.
Definition array.hpp:380
bool operator>(const array &other) const
Array greater-than operator.
Definition array.hpp:816
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 t...
Definition array.hpp:1242
array(const array &other)
Copy constructor.
Definition array.hpp:727
virtual void shuffle() noexcept
Shuffle the elements of the array into a random order.
Definition array.hpp:437
void sort(std::function< bool(const T &, const T &)> lambda) noexcept
Sort the array based on an arbitrary function.
Definition array.hpp:400
array sorted() const noexcept
Sort the array based on default comparison operator.
Definition array.hpp:411
void init(const T &arg1)
Helper function for single object initialization.
Definition array.hpp:84
virtual int find(const T &object) const
Find the index of a given object in the array.
Definition array.hpp:363
void add(const array &other) noexcept
Add another array to this array.
Definition array.hpp:224
array sorted(std::function< bool(const T &, const T &)> lambda) const noexcept
Sort the array based on an arbitrary function.
Definition array.hpp:425
std::vector< T > array_data
The data in the array.
Definition array.hpp:78
array & operator=(const std::initializer_list< T > &other)
Initializer list assignment operator.
Definition array.hpp:772
array(const T &arg1, const Args &...args)
List-initialized constructor.
Definition array.hpp:738
void sort() noexcept
Sort the array based on default comparison operator.
Definition array.hpp:389
bool operator<=(const array &other) const
Array less-than-or-equal operator.
Definition array.hpp:900
array & operator=(const array &other)
Array assignment operator.
Definition array.hpp:754
bool operator!=(const array &other) const
Check whether two arrays' contents are different.
Definition array.hpp:872
T & operator[](int index)
Function to get the object at the given index.
Definition array.hpp:348
size_t size() const noexcept override
Get the size of the array.
Definition array.hpp:1024
array()
Default constructor.
Definition array.hpp:149
int length() const noexcept override
Get the length of the array.
Definition array.hpp:1035
array shuffled() const noexcept
Shuffle the elements of the array into a random order.
Definition array.hpp:452
array & insert(const T &, int)
Insert an object into the array.
Definition array.hpp:923
virtual bool operator()(const T &arg1, const T &arg2) const
Callable operator.
Definition array.hpp:1208
bool operator<(const array &other) const
Array less-than operator.
Definition array.hpp:844
array randomElements(int count) const noexcept
Get N random elements from the array.
Definition array.hpp:593
array reversed() const noexcept
Reverse the order of all elements in the array.
Definition array.hpp:479
T & at(int)
Function to get the object at the given index.
Definition array.hpp:1052
void init(const T &arg1, const Args &...args)
Helper function for brace-enclosed list initialization.
Definition array.hpp:94
T randomElement() const
Get a random element from the array.
Definition array.hpp:572
void push(const array &other) noexcept
Add another array to this array.
Definition array.hpp:256
array & replace(int, int, const T &)
Replace all objects in the given range with an object.
Definition array.hpp:1085
array(const std::vector< T > &other)
Copy from std::vector.
Definition array.hpp:732
void append(const T &)
Append an object to the end of the array.
Definition array.hpp:955
array & remove(int)
Remove an object from the array.
Definition array.hpp:967
virtual ~array()
Destructor.
Definition array.hpp:180
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.
Definition array.hpp:1256
bool operator>=(const array &other) const
Array greater-than-or-equal operator.
Definition array.hpp:886
virtual void reverse() noexcept
Reverse the order of all elements in the array.
Definition array.hpp:464
array & replace(int, int, const array< T > &)
Replace all objects in the given range with an array of objects.
Definition array.hpp:1132
virtual int add(const T &object)
Add an object to the array.
Definition array.hpp:208
virtual bool gt(const T &arg1, const T &arg2) const
Check if one object is greater than another.
Definition array.hpp:127
array(const std::initializer_list< T > &other)
Construct from a generic initializer list.
Definition array.hpp:177
array subset(int index, int count) const
Get a contiguous subset of the elements in the array.
Definition array.hpp:1166
array & remove(int, int)
Remove all elements in a subset of the array.
Definition array.hpp:989
T * begin() const noexcept override
Get pointer to the beginning of the array.
Definition array.hpp:621
virtual bool lt(const T &arg1, const T &arg2) const
Check if one object is less than another.
Definition array.hpp:143
T * end() const noexcept override
Get pointer to the end of the array.
Definition array.hpp:633
bool operator==(const array &other) const
Check whether two arrays' contents are the same.
Definition array.hpp:792
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 resul...
Definition array.hpp:1230
array & swap(int index1, int index2)
Swap two elements in an array.
Definition array.hpp:1221
void clear()
Clear the data in the array.
Definition array.hpp:906
T pop()
Remove the last element from this array.
Definition array.hpp:268
An arbitrary generator for producing sequential results on-the-fly.
Definition generator.hpp:71
An interface for getting an object's size.
Definition sizable.hpp:13
std::enable_if< std::is_base_of< z::core::sizable, T >::value >::type size(const T &object, size_t &bytes) noexcept
Get the size of a sizable object.
Definition sizable.hpp:34