5#include <initializer_list>
10#include "arrayLike.hpp"
13#include "typeChecks.hpp"
16#if __has_include(<cereal/cereal.hpp>)
17#include <cereal/archives/json.hpp>
18#include <cereal/archives/xml.hpp>
47 inline void init(
const T &arg1) {
56 template <
typename... Args>
57 inline void init(
const T &arg1,
const Args &...args) {
74 virtual bool eq(
const T &arg1,
const T &arg2)
const {
75 return equals(arg1, arg2);
90 virtual bool gt(
const T &arg1,
const T &arg2)
const {
91 return greater(arg1, arg2);
106 virtual bool lt(
const T &arg1,
const T &arg2)
const {
107 return lesser(arg1, arg2);
132 template <
typename... Args>
133 array(
const T &arg1,
const Args &...args);
171 virtual int add(
const T &
object) {
188 for (
auto i : other.array_data) {
205 inline int push(
const T &
object)
noexcept {
233 throw std::out_of_range(
"No more elements in array.");
271 size_t size() const noexcept override;
281 const T &
at(
int) const override;
295 const T &operator[](
int index)
const override {
326 virtual int find(
const T &
object)
const {
327 for (
int i = 0; i < (int)
array_data.size(); i++) {
347 int find(std::function<
bool(
const T &)> predicate)
const {
348 for (
int i = 0; i < (int)
array_data.size(); i++) {
368 int find(std::function<
bool(
int,
const T &)> predicate)
const {
369 for (
int i = 0; i < (int)
array_data.size(); i++) {
386 return find(
object) > -1;
399 bool contains(std::function<
bool(
const T &)> predicate)
const {
400 return find(predicate) > -1;
413 bool contains(std::function<
bool(
int,
const T &)> predicate)
const {
414 return find(predicate) > -1;
433 void sort(std::function<
bool(
const T &,
const T &)> lambda)
noexcept {
445 auto new_array = *
this;
458 array sorted(std::function<
bool(
const T &,
const T &)> lambda)
const noexcept {
459 auto new_array = *
this;
460 new_array.sort(lambda);
471 std::random_device device;
485 auto new_array = *
this;
497 const auto len =
length();
498 for (
int i = 0; i < (len / 2); ++i) {
512 auto new_array = *
this;
568 template <
typename U>
593 T
reduce(std::function<T(
const T &,
const T &)> lambda,
const T &defaultValue = {})
const;
600 template <
typename U>
610 template <
typename U>
635 inline T
operator>>(std::function<T(
const T &,
const T &)> lambda) {
650 throw std::out_of_range(
"Array is empty");
653 std::random_device device;
655 std::uniform_int_distribution<int> distribution(0,
array_data.size() - 1);
674 std::random_device device;
676 std::uniform_int_distribution<int> distribution(0,
array_data.size() - 1);
678 const int numElements = std::min(count,
length());
682 for (
int i = 0; i < numElements; ++i) {
697 T *
begin() const noexcept
override {
709 T *
end() const noexcept
override {
714#if __has_include(<cereal/cereal.hpp>)
719 void save(cereal::JSONOutputArchive &ar)
const {
721 for (
int i = 0; i < (int)
array_data.size(); i++) {
730 void save(cereal::XMLOutputArchive &ar)
const {
731 for (
int i = 0; i < (int)
array_data.size(); i++) {
732 ar(cereal::make_nvp(std::to_string(i),
array_data[i]));
740 template <
typename archive>
741 void save(archive &ar)
const {
743 for (
int i = 0; i < (int)
array_data.size(); i++) {
752 void load(cereal::JSONInputArchive &ar) {
758 for (
int i = 0; i < sz; i++) {
768 void load(cereal::XMLInputArchive &ar) {
774 for (CEREAL_SIZE_TYPE i = 0; i < sz; i++) {
784 template <
class archive>
785 void load(archive &ar) {
792 for (CEREAL_SIZE_TYPE i = 0; i < sz; i++) {
813template <
typename... Args>
851 for (
auto &item : other) {
873 for (
int i = 0; i < (int)
array_data.size(); i++) {
899 for (
int i = 0; i < (int)
array_data.size(); i++) {
927 for (
int i = 0; i < (int)
array_data.size(); i++) {
1030template <
typename T>
1042template <
typename T>
1047 if ((index >=
array_data.size()) || (index < 0)) {
1064template <
typename T>
1078 end = index + count;
1080 start = index + count + 1;
1099template <
typename T>
1110template <
typename T>
1127template <
typename T>
1144template <
typename T>
1160template <
typename T>
1174 end = index + count;
1176 start = index + count + 1;
1207template <
typename T>
1217 end = index + count;
1219 start = index + count + 1;
1241template <
typename T>
1257 end = index + count;
1259 start = index + count + 1;
1273 if (
end - start > 0) {
1276 for (
int i = start; i <
end; i++) {
1283template <
typename T>
1285 return greater(arg1, arg2);
1288template <
typename T>
1293 return (index < (
int)
array_data.size()) && (index >= 0);
1296template <
typename T>
1298 auto temp =
at(index1);
1304template <
typename T>
1305template <
typename U>
1311 result.
add(lambda(i));
1317template <
typename T>
1331template <
typename T>
1332T
array<T>::reduce(std::function<T(
const T &,
const T &)> lambda,
const T &defaultValue)
const {
1335 return defaultValue;
1339 for (
int i = 1; i < len; ++i) {
1349#define zarray z::core::array
An interface for all objects that can be both iterated over and directly indexed.
Definition arrayLike.hpp:13
bool isValid(int index) const
Check if an index is within the bounds of the array.
Definition array.hpp:1289
void increase(int newSize) noexcept
Increase the space allocated for this array.
Definition array.hpp:155
int push(const T &object) noexcept
Add an object to the array.
Definition array.hpp:205
virtual bool eq(const T &arg1, const T &arg2) const
Check if two objects are equal.
Definition array.hpp:74
bool contains(const T &object) const noexcept
Check if a given object is in the array.
Definition array.hpp:385
bool operator>(const array &other) const
Array greater-than operator.
Definition array.hpp:892
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:1318
array(const array &other)
Copy constructor.
Definition array.hpp:803
virtual void shuffle() noexcept
Shuffle the elements of the array into a random order.
Definition array.hpp:470
int find(std::function< bool(int, const T &)> predicate) const
Find the index of the first element in the array that satisfies a given predicate.
Definition array.hpp:368
void sort(std::function< bool(const T &, const T &)> lambda) noexcept
Sort the array based on an arbitrary function.
Definition array.hpp:433
array< U > operator|(U(*lambda)(T)) noexcept
Definition array.hpp:611
array sorted() const noexcept
Sort the array based on default comparison operator.
Definition array.hpp:444
void init(const T &arg1)
Helper function for single object initialization.
Definition array.hpp:47
virtual int find(const T &object) const
Find the index of a given object in the array.
Definition array.hpp:326
void add(const array &other) noexcept
Add another array to this array.
Definition array.hpp:187
array sorted(std::function< bool(const T &, const T &)> lambda) const noexcept
Sort the array based on an arbitrary function.
Definition array.hpp:458
std::vector< generic > array_data
Definition array.hpp:41
array & operator=(const std::initializer_list< T > &other)
Initializer list assignment operator.
Definition array.hpp:848
array(const T &arg1, const Args &...args)
List-initialized constructor.
Definition array.hpp:814
void sort() noexcept
Sort the array based on default comparison operator.
Definition array.hpp:422
bool operator<=(const array &other) const
Array less-than-or-equal operator.
Definition array.hpp:976
array & operator=(const array &other)
Array assignment operator.
Definition array.hpp:830
bool operator!=(const array &other) const
Check whether two arrays' contents are different.
Definition array.hpp:948
T & operator[](int index)
Function to get the object at the given index.
Definition array.hpp:311
size_t size() const noexcept override
Get the size of the array.
Definition array.hpp:1100
array()
Default constructor.
Definition array.hpp:112
int length() const noexcept override
Get the length of the array.
Definition array.hpp:1111
array shuffled() const noexcept
Shuffle the elements of the array into a random order.
Definition array.hpp:484
array & insert(const T &, int)
Insert an object into the array.
Definition array.hpp:999
virtual bool operator()(const T &arg1, const T &arg2) const
Callable operator.
Definition array.hpp:1284
bool operator<(const array &other) const
Array less-than operator.
Definition array.hpp:920
array randomElements(int count) const noexcept
Get N random elements from the array.
Definition array.hpp:669
array reversed() const noexcept
Reverse the order of all elements in the array.
Definition array.hpp:511
int find(std::function< bool(const T &)> predicate) const
Find the index of the first element in the array that satisfies a given predicate.
Definition array.hpp:347
T & at(int)
Function to get the object at the given index.
Definition array.hpp:1128
void init(const T &arg1, const Args &...args)
Helper function for brace-enclosed list initialization.
Definition array.hpp:57
T randomElement() const
Get a random element from the array.
Definition array.hpp:648
void push(const array &other) noexcept
Add another array to this array.
Definition array.hpp:219
T operator>>(std::function< T(const T &, const T &)> lambda)
Reduces the array to a single value by applying a binary operation cumulatively to the elements.
Definition array.hpp:635
array & replace(int, int, const T &)
Replace all objects in the given range with an object.
Definition array.hpp:1161
bool contains(std::function< bool(const T &)> predicate) const
Check if an element in the array satisfies a given predicate.
Definition array.hpp:399
array(const std::vector< T > &other)
Copy from std::vector.
Definition array.hpp:808
void append(const T &)
Append an object to the end of the array.
Definition array.hpp:1031
array & remove(int)
Remove an object from the array.
Definition array.hpp:1043
virtual ~array()
Destructor.
Definition array.hpp:143
bool operator>=(const array &other) const
Array greater-than-or-equal operator.
Definition array.hpp:962
virtual void reverse() noexcept
Reverse the order of all elements in the array.
Definition array.hpp:496
array & replace(int, int, const array< T > &)
Replace all objects in the given range with an array of objects.
Definition array.hpp:1208
virtual int add(const T &object)
Add an object to the array.
Definition array.hpp:171
array operator&&(std::function< T(const T &)> lambda) noexcept
Definition array.hpp:620
virtual bool gt(const T &arg1, const T &arg2) const
Check if one object is greater than another.
Definition array.hpp:90
array< U > operator|(std::function< U(T)> lambda) noexcept
Definition array.hpp:601
bool contains(std::function< bool(int, const T &)> predicate) const
Check if an element in the array satisfies a given predicate.
Definition array.hpp:413
array(const std::initializer_list< T > &other)
Construct from a generic initializer list.
Definition array.hpp:140
array subset(int index, int count) const
Get a contiguous subset of the elements in the array.
Definition array.hpp:1242
T reduce(std::function< T(const T &, const T &)> lambda, const T &defaultValue={}) const
Reduces the array to a single value by applying a binary operation cumulatively to the elements.
Definition array.hpp:1332
array & remove(int, int)
Remove all elements in a subset of the array.
Definition array.hpp:1065
T * begin() const noexcept override
Get pointer to the beginning of the array.
Definition array.hpp:697
virtual bool lt(const T &arg1, const T &arg2) const
Check if one object is less than another.
Definition array.hpp:106
T * end() const noexcept override
Get pointer to the end of the array.
Definition array.hpp:709
bool operator==(const array &other) const
Check whether two arrays' contents are the same.
Definition array.hpp:868
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:1306
array & swap(int index1, int index2)
Swap two elements in an array.
Definition array.hpp:1297
void clear()
Clear the data in the array.
Definition array.hpp:982
T pop()
Remove the last element from this array.
Definition array.hpp:231
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
Section containing core functions and classes.
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:31