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));
90 template <
typename...
Args>
166 template <
typename...
Args>
205 virtual int add(
const T &
object) {
239 inline int push(
const T &
object)
noexcept {
356 virtual int find(
const T &
object)
const {
374 return find(
object) > -1;
393 void sort(std::function<
bool(
const T &,
const T &)> lambda)
noexcept {
418 array sorted(std::function<
bool(
const T &,
const T &)> lambda)
const noexcept {
432 std::random_device device;
459 for (
int i = 0;
i < (
len / 2); ++
i) {
528 template <
typename U>
566 throw std::out_of_range(
"Array is empty");
569 std::random_device device;
590 std::random_device device;
630#if __has_include(<cereal/cereal.hpp>)
635 void save(cereal::JSONOutputArchive &
ar)
const {
646 void save(cereal::XMLOutputArchive &
ar)
const {
656 template <
typename archive>
668 void load(cereal::JSONInputArchive &
ar) {
674 for (
int i = 0;
i <
sz;
i++) {
700 template <
class archive>
729template <
typename...
Args>
980 start = index + count + 1;
1010template <
typename T>
1027template <
typename T>
1044template <
typename T>
1060template <
typename T>
1074 end = index + count;
1076 start = index + count + 1;
1107template <
typename T>
1117 end = index + count;
1119 start = index + count + 1;
1141template <
typename T>
1157 end = index + count;
1159 start = index + count + 1;
1173 if (
end - start > 0) {
1176 for (
int i = start;
i <
end;
i++) {
1183template <
typename T>
1188template <
typename T>
1196template <
typename T>
1204template <
typename T>
1205template <
typename U>
1217template <
typename T>
1231template <
typename T>
1239 for (
int i = 1;
i <
len; ++
i) {
1249#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:72
bool isValid(int index) const
Check if an index is within the bounds of the array.
Definition array.hpp:1189
void increase(int newSize) noexcept
Increase the space allocated for this array.
Definition array.hpp:189
int push(const T &object) noexcept
Add an object to the array.
Definition array.hpp:239
virtual bool eq(const T &arg1, const T &arg2) const
Check if two objects are equal.
Definition array.hpp:108
bool contains(const T &object) const noexcept
Check if a given object is in the array.
Definition array.hpp:373
bool operator>(const array &other) const
Array greater-than operator.
Definition array.hpp:808
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:1218
void load(archive &ar)
Binary specialization of serialization input.
Definition array.hpp:701
array(const array &other)
Copy constructor.
Definition array.hpp:719
virtual void shuffle() noexcept
Shuffle the elements of the array into a random order.
Definition array.hpp:430
void save(cereal::JSONOutputArchive &ar) const
JSON specialization of serialization output.
Definition array.hpp:635
void sort(std::function< bool(const T &, const T &)> lambda) noexcept
Sort the array based on an arbitrary function.
Definition array.hpp:393
array sorted() const noexcept
Sort the array based on default comparison operator.
Definition array.hpp:404
void init(const T &arg1)
Helper function for single object initialization.
Definition array.hpp:81
virtual int find(const T &object) const
Find the index of a given object in the array.
Definition array.hpp:356
void add(const array &other) noexcept
Add another array to this array.
Definition array.hpp:221
array sorted(std::function< bool(const T &, const T &)> lambda) const noexcept
Sort the array based on an arbitrary function.
Definition array.hpp:418
std::vector< T > array_data
The data in the array.
Definition array.hpp:75
array & operator=(const std::initializer_list< T > &other)
Initializer list assignment operator.
Definition array.hpp:764
void load(cereal::XMLInputArchive &ar)
XML specialization of serialization input.
Definition array.hpp:684
void save(cereal::XMLOutputArchive &ar) const
XML specialization of serialization output.
Definition array.hpp:646
array(const T &arg1, const Args &...args)
List-initialized constructor.
Definition array.hpp:730
void sort() noexcept
Sort the array based on default comparison operator.
Definition array.hpp:382
bool operator<=(const array &other) const
Array less-than-or-equal operator.
Definition array.hpp:878
array & operator=(const array &other)
Array assignment operator.
Definition array.hpp:746
T & operator[](int index)
Function to get the object at the given index.
Definition array.hpp:341
size_t size() const noexcept override
Get the size of the array.
Definition array.hpp:1000
array()
Default constructor.
Definition array.hpp:146
int length() const noexcept override
Get the length of the array.
Definition array.hpp:1011
array shuffled() const noexcept
Shuffle the elements of the array into a random order.
Definition array.hpp:445
array & insert(const T &, int)
Insert an object into the array.
Definition array.hpp:899
virtual bool operator()(const T &arg1, const T &arg2) const
Callable operator.
Definition array.hpp:1184
bool operator<(const array &other) const
Array less-than operator.
Definition array.hpp:836
array randomElements(int count) const noexcept
Get N random elements from the array.
Definition array.hpp:585
array reversed() const noexcept
Reverse the order of all elements in the array.
Definition array.hpp:472
T & at(int)
Function to get the object at the given index.
Definition array.hpp:1028
void init(const T &arg1, const Args &...args)
Helper function for brace-enclosed list initialization.
Definition array.hpp:91
T randomElement() const
Get a random element from the array.
Definition array.hpp:564
void push(const array &other) noexcept
Add another array to this array.
Definition array.hpp:253
array & replace(int, int, const T &)
Replace all objects in the given range with an object.
Definition array.hpp:1061
array(const std::vector< T > &other)
Copy from std::vector.
Definition array.hpp:724
void append(const T &)
Append an object to the end of the array.
Definition array.hpp:931
array & remove(int)
Remove an object from the array.
Definition array.hpp:943
virtual ~array()
Destructor.
Definition array.hpp:177
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:1232
bool operator>=(const array &other) const
Array greater-than-or-equal operator.
Definition array.hpp:864
virtual void reverse() noexcept
Reverse the order of all elements in the array.
Definition array.hpp:457
void save(archive &ar) const
Binary specialization of serialization output.
Definition array.hpp:657
array & replace(int, int, const array< T > &)
Replace all objects in the given range with an array of objects.
Definition array.hpp:1108
virtual int add(const T &object)
Add an object to the array.
Definition array.hpp:205
virtual bool gt(const T &arg1, const T &arg2) const
Check if one object is greater than another.
Definition array.hpp:124
array(const std::initializer_list< T > &other)
Construct from a generic initializer list.
Definition array.hpp:174
array subset(int index, int count) const
Get a contiguous subset of the elements in the array.
Definition array.hpp:1142
void load(cereal::JSONInputArchive &ar)
JSON specialization of serialization input.
Definition array.hpp:668
array & remove(int, int)
Remove all elements in a subset of the array.
Definition array.hpp:965
T * begin() const noexcept override
Get pointer to the beginning of the array.
Definition array.hpp:613
virtual bool lt(const T &arg1, const T &arg2) const
Check if one object is less than another.
Definition array.hpp:140
T * end() const noexcept override
Get pointer to the end of the array.
Definition array.hpp:625
bool operator==(const array &other) const
Check whether two arrays' contents are the same.
Definition array.hpp:784
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:1206
array & swap(int index1, int index2)
Swap two elements in an array.
Definition array.hpp:1197
void clear()
Clear the data in the array.
Definition array.hpp:884
T pop()
Remove the last element from this array.
Definition array.hpp:265
An arbitrary generator for producing sequential results on-the-fly.
Definition generator.hpp:85
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