libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
algorithm.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <algorithm>
11#include <numeric>
12#include <functional>
14#include <promeki/core/list.h>
15
17
26template <typename Container>
27Container sorted(Container c) {
28 std::sort(c.begin(), c.end());
29 return c;
30}
31
40template <typename Container, typename Compare>
41Container sorted(Container c, Compare comp) {
42 std::sort(c.begin(), c.end(), comp);
43 return c;
44}
45
54template <typename Container, typename Predicate>
55Container filtered(const Container &c, Predicate pred) {
56 Container result;
57 for(auto it = c.begin(); it != c.end(); ++it) {
58 if(pred(*it)) result += *it;
59 }
60 return result;
61}
62
71template <typename Container, typename Transform>
72auto mapped(const Container &c, Transform fn) {
73 using ResultType = decltype(fn(*c.begin()));
74 List<ResultType> result;
75 result.reserve(c.size());
76 for(auto it = c.begin(); it != c.end(); ++it) {
77 result += fn(*it);
78 }
79 return result;
80}
81
90template <typename Container, typename Predicate>
91bool allOf(const Container &c, Predicate pred) {
92 return std::all_of(c.begin(), c.end(), pred);
93}
94
103template <typename Container, typename Predicate>
104bool anyOf(const Container &c, Predicate pred) {
105 return std::any_of(c.begin(), c.end(), pred);
106}
107
116template <typename Container, typename Predicate>
117bool noneOf(const Container &c, Predicate pred) {
118 return std::none_of(c.begin(), c.end(), pred);
119}
120
128template <typename Container, typename Callable>
129void forEach(const Container &c, Callable fn) {
130 std::for_each(c.begin(), c.end(), fn);
131}
132
143template <typename Container, typename Init, typename BinaryOp>
144Init accumulate(const Container &c, Init init, BinaryOp op) {
145 return std::accumulate(c.begin(), c.end(), init, op);
146}
147
154template <typename Container>
155auto minElement(const Container &c) {
156 return std::min_element(c.begin(), c.end());
157}
158
165template <typename Container>
166auto maxElement(const Container &c) {
167 return std::max_element(c.begin(), c.end());
168}
169
178template <typename Container, typename Value>
179bool contains(const Container &c, const Value &val) {
180 return std::find(c.begin(), c.end(), val) != c.end();
181}
182
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