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 {
359 virtual int find(
const T &
object)
const {
377 return find(
object) > -1;
396 void sort(std::function<
bool(
const T &,
const T &)> lambda)
noexcept {
421 array sorted(std::function<
bool(
const T &,
const T &)> lambda)
const noexcept {
435 std::random_device device;
462 for (
int i = 0;
i < (
len / 2); ++
i) {
531 template <
typename U>
569 throw std::out_of_range(
"Array is empty");
572 std::random_device device;
593 std::random_device device;
633#if __has_include(<cereal/cereal.hpp>)
638 void save(cereal::JSONOutputArchive &
ar)
const {
649 void save(cereal::XMLOutputArchive &
ar)
const {
659 template <
typename archive>
671 void load(cereal::JSONInputArchive &
ar) {
677 for (
int i = 0;
i <
sz;
i++) {
703 template <
class archive>
732template <
typename...
Args>
983 start = index + count + 1;
1002template <
typename T>
1013template <
typename T>
1030template <
typename T>
1047template <
typename T>
1063template <
typename T>
1077 end = index + count;
1079 start = index + count + 1;
1110template <
typename T>
1120 end = index + count;
1122 start = index + count + 1;
1144template <
typename T>
1160 end = index + count;
1162 start = index + count + 1;
1176 if (
end - start > 0) {
1179 for (
int i = start;
i <
end;
i++) {
1186template <
typename T>
1191template <
typename T>
1199template <
typename T>
1207template <
typename T>
1208template <
typename U>
1220template <
typename T>
1234template <
typename T>
1242 for (
int i = 1;
i <
len; ++
i) {
1252#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:1192
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:376
bool operator>(const array &other) const
Array greater-than operator.
Definition array.hpp:811
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:1221
void load(archive &ar)
Binary specialization of serialization input.
Definition array.hpp:704
array(const array &other)
Copy constructor.
Definition array.hpp:722
virtual void shuffle() noexcept
Shuffle the elements of the array into a random order.
Definition array.hpp:433
void save(cereal::JSONOutputArchive &ar) const
JSON specialization of serialization output.
Definition array.hpp:638
void sort(std::function< bool(const T &, const T &)> lambda) noexcept
Sort the array based on an arbitrary function.
Definition array.hpp:396
array sorted() const noexcept
Sort the array based on default comparison operator.
Definition array.hpp:407
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:359
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:421
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:767
void load(cereal::XMLInputArchive &ar)
XML specialization of serialization input.
Definition array.hpp:687
void save(cereal::XMLOutputArchive &ar) const
XML specialization of serialization output.
Definition array.hpp:649
array(const T &arg1, const Args &...args)
List-initialized constructor.
Definition array.hpp:733
void sort() noexcept
Sort the array based on default comparison operator.
Definition array.hpp:385
bool operator<=(const array &other) const
Array less-than-or-equal operator.
Definition array.hpp:881
array & operator=(const array &other)
Array assignment operator.
Definition array.hpp:749
T & operator[](int index)
Function to get the object at the given index.
Definition array.hpp:344
size_t size() const noexcept override
Get the size of the array.
Definition array.hpp:1003
array()
Default constructor.
Definition array.hpp:149
int length() const noexcept override
Get the length of the array.
Definition array.hpp:1014
array shuffled() const noexcept
Shuffle the elements of the array into a random order.
Definition array.hpp:448
array & insert(const T &, int)
Insert an object into the array.
Definition array.hpp:902
virtual bool operator()(const T &arg1, const T &arg2) const
Callable operator.
Definition array.hpp:1187
bool operator<(const array &other) const
Array less-than operator.
Definition array.hpp:839
array randomElements(int count) const noexcept
Get N random elements from the array.
Definition array.hpp:588
array reversed() const noexcept
Reverse the order of all elements in the array.
Definition array.hpp:475
T & at(int)
Function to get the object at the given index.
Definition array.hpp:1031
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:567
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:1064
array(const std::vector< T > &other)
Copy from std::vector.
Definition array.hpp:727
void append(const T &)
Append an object to the end of the array.
Definition array.hpp:934
array & remove(int)
Remove an object from the array.
Definition array.hpp:946
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:1235
bool operator>=(const array &other) const
Array greater-than-or-equal operator.
Definition array.hpp:867
virtual void reverse() noexcept
Reverse the order of all elements in the array.
Definition array.hpp:460
void save(archive &ar) const
Binary specialization of serialization output.
Definition array.hpp:660
array & replace(int, int, const array< T > &)
Replace all objects in the given range with an array of objects.
Definition array.hpp:1111
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:1145
void load(cereal::JSONInputArchive &ar)
JSON specialization of serialization input.
Definition array.hpp:671
array & remove(int, int)
Remove all elements in a subset of the array.
Definition array.hpp:968
T * begin() const noexcept override
Get pointer to the beginning of the array.
Definition array.hpp:616
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:628
bool operator==(const array &other) const
Check whether two arrays' contents are the same.
Definition array.hpp:787
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:1209
array & swap(int index1, int index2)
Swap two elements in an array.
Definition array.hpp:1200
void clear()
Clear the data in the array.
Definition array.hpp:887
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: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