libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
algorithm.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <algorithm>
14#include <numeric>
15#include <functional>
16#include <promeki/namespace.h>
17#include <promeki/list.h>
18
19PROMEKI_NAMESPACE_BEGIN
20
29template <typename Container> Container sorted(Container c) {
30 std::sort(c.begin(), c.end());
31 return c;
32}
33
42template <typename Container, typename Compare> Container sorted(Container c, Compare comp) {
43 std::sort(c.begin(), c.end(), comp);
44 return c;
45}
46
55template <typename Container, typename Predicate> Container 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> auto mapped(const Container &c, Transform fn) {
72 using ResultType = decltype(fn(*c.begin()));
73 List<ResultType> result;
74 result.reserve(c.size());
75 for (auto it = c.begin(); it != c.end(); ++it) {
76 result += fn(*it);
77 }
78 return result;
79}
80
89template <typename Container, typename Predicate> bool allOf(const Container &c, Predicate pred) {
90 return std::all_of(c.begin(), c.end(), pred);
91}
92
101template <typename Container, typename Predicate> bool anyOf(const Container &c, Predicate pred) {
102 return std::any_of(c.begin(), c.end(), pred);
103}
104
113template <typename Container, typename Predicate> bool noneOf(const Container &c, Predicate pred) {
114 return std::none_of(c.begin(), c.end(), pred);
115}
116
124template <typename Container, typename Callable> void forEach(const Container &c, Callable fn) {
125 std::for_each(c.begin(), c.end(), fn);
126}
127
138template <typename Container, typename Init, typename BinaryOp>
139Init accumulate(const Container &c, Init init, BinaryOp op) {
140 return std::accumulate(c.begin(), c.end(), init, op);
141}
142
149template <typename Container> auto minElement(const Container &c) {
150 return std::min_element(c.begin(), c.end());
151}
152
159template <typename Container> auto maxElement(const Container &c) {
160 return std::max_element(c.begin(), c.end());
161}
162
171template <typename Container, typename Value> bool contains(const Container &c, const Value &val) {
172 return std::find(c.begin(), c.end(), val) != c.end();
173}
174
175PROMEKI_NAMESPACE_END
176
177#endif // PROMEKI_ENABLE_CORE