26template <
typename Container>
28 std::sort(c.begin(), c.end());
40template <
typename Container,
typename Compare>
41Container
sorted(Container c, Compare comp) {
42 std::sort(c.begin(), c.end(), comp);
54template <
typename Container,
typename Predicate>
55Container
filtered(
const Container &c, Predicate pred) {
57 for(
auto it = c.begin(); it != c.end(); ++it) {
58 if(pred(*it)) result += *it;
71template <
typename Container,
typename Transform>
72auto mapped(
const Container &c, Transform fn) {
73 using ResultType =
decltype(fn(*c.begin()));
76 for(
auto it = c.begin(); it != c.end(); ++it) {
90template <
typename Container,
typename Predicate>
91bool allOf(
const Container &c, Predicate pred) {
92 return std::all_of(c.begin(), c.end(), pred);
103template <
typename Container,
typename Predicate>
104bool anyOf(
const Container &c, Predicate pred) {
105 return std::any_of(c.begin(), c.end(), pred);
116template <
typename Container,
typename Predicate>
117bool noneOf(
const Container &c, Predicate pred) {
118 return std::none_of(c.begin(), c.end(), pred);
128template <
typename Container,
typename Callable>
129void forEach(
const Container &c, Callable fn) {
130 std::for_each(c.begin(), c.end(), fn);
143template <
typename Container,
typename Init,
typename BinaryOp>
145 return std::accumulate(c.begin(), c.end(), init, op);
154template <
typename Container>
156 return std::min_element(c.begin(), c.end());
165template <
typename Container>
167 return std::max_element(c.begin(), c.end());
178template <
typename Container,
typename Value>
179bool contains(
const Container &c,
const Value &val) {
180 return std::find(c.begin(), c.end(), val) != c.end();
Container filtered(const Container &c, Predicate pred)
Returns a copy of the container with only elements matching the predicate.
Definition algorithm.h:55
Init accumulate(const Container &c, Init init, BinaryOp op)
Folds/reduces the container with a binary operation.
Definition algorithm.h:144
bool anyOf(const Container &c, Predicate pred)
Returns true if any element satisfies the predicate.
Definition algorithm.h:104
auto maxElement(const Container &c)
Returns an iterator to the maximum element.
Definition algorithm.h:166
bool allOf(const Container &c, Predicate pred)
Returns true if all elements satisfy the predicate.
Definition algorithm.h:91
auto minElement(const Container &c)
Returns an iterator to the minimum element.
Definition algorithm.h:155
bool contains(const Container &c, const Value &val)
Returns true if the container contains the given value.
Definition algorithm.h:179
bool noneOf(const Container &c, Predicate pred)
Returns true if no elements satisfy the predicate.
Definition algorithm.h:117
void forEach(const Container &c, Callable fn)
Applies a callable to each element.
Definition algorithm.h:129
auto mapped(const Container &c, Transform fn)
Returns a container of transformed elements.
Definition algorithm.h:72
Dynamic array container wrapping std::vector.
Definition list.h:40
void reserve(size_t newCapacity)
Pre-allocates storage for at least newCapacity elements.
Definition list.h:314
PROMEKI_NAMESPACE_BEGIN Container sorted(Container c)
Returns a sorted copy of the container.
Definition algorithm.h:27
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19