27 #include <Reaktoro/Common/TraitsUtils.hpp>
33 #define RKT_LAMBDA(x, expr) [&](const auto& x) { return expr; }
36 template<
typename Container,
typename T>
37 auto index(
const Container& c,
const T& x) -> std::size_t
39 return std::find(c.begin(), c.end(), x) - c.begin();
43 template<
typename Container,
typename Predicate>
44 auto indexfn(
const Container& c,
const Predicate& pred) -> std::size_t
46 return std::find_if(c.begin(), c.end(), pred) - c.begin();
50 template<
typename Container,
typename Predicate>
51 auto filter(
const Container& c,
const Predicate& pred)
54 std::copy_if(c.begin(), c.end(), std::back_inserter(res), pred);
59 template<
typename Container,
typename T>
60 auto remove(
const Container& c,
const T& x)
62 return filter(c, [&](
auto&& y) {
return x != y; });
66 template<
typename Container,
typename Predicate>
67 auto removefn(
const Container& c,
const Predicate& pred)
69 return filter(c, [&](
auto&& x) {
return !pred(x); });
73 template<
typename Container>
76 std::sort(c.begin(), c.end());
80 template<
typename Container,
typename Predicate>
81 auto sortfn(Container& c,
const Predicate& pred)
83 std::sort(c.begin(), c.end(), pred);
87 template<
typename Container>
91 std::sort(res.begin(), res.end());
96 template<
typename Container,
typename Predicate>
97 auto sortedfn(
const Container& c,
const Predicate& pred)
100 std::sort(res.begin(), res.end(), pred);
105 template<
typename Container>
109 std::sort(res.begin(), res.end());
110 res.erase(std::unique(res.begin(), res.end()), res.end());
115 template<
typename Container,
typename Result,
typename Function>
116 auto transform(
const Container& c, Result& res,
const Function& f)
118 assert(c.size() == res.size());
119 std::transform(c.begin(), c.end(), res.begin(), f);
123 template<
typename Container,
typename Indices>
127 res.reserve(indices.size());
128 for(
auto i : indices)
134 template<
typename Container,
typename Function>
137 using X =
typename Container::value_type;
138 using T = std::invoke_result_t<Function, X>;
140 res.resize(c.size());
146 template<
typename Container,
typename T>
149 return c.size() && std::find(c.begin(), c.end(), x) != c.end();
153 template<
typename Container,
typename Predicate>
156 return c.size() && std::find_if(c.begin(), c.end(), pred) != c.end();
160 template<
typename ContainerA,
typename ContainerB>
161 auto contained(
const ContainerA& a,
const ContainerB& b)
163 for(
auto const& x : a)
170 template<
typename Container>
174 res.insert(res.end(), b.begin(), b.end());
179 template<
typename Container>
180 auto merge(
const Container& a,
const Container& b)
183 res.insert(res.end(), b.begin(), b.end());
184 std::sort(res.begin(), res.end());
185 res.erase(std::unique(res.begin(), res.end()), res.end());
190 template<
typename Container>
197 template<
typename Container>
204 template<
typename ContainerA,
typename ContainerB>
205 auto disjoint(
const ContainerA& a,
const ContainerB& b)
214 template<
typename ContainerA,
typename ContainerB>
215 auto identical(
const ContainerA& a,
const ContainerB& b)
224 if(last <= first)
return std::vector<T>{};
225 auto size = std::size_t((last - first - 1)/step) + 1;
226 std::vector<T> res(size);
227 for(
auto i = 0; i < size; ++i)
228 res[i] = first + i*step;
236 return range(first, last,
static_cast<T
>(1));
243 return range(
static_cast<T
>(0), last,
static_cast<T
>(1));
247 template<
typename X,
typename X0,
typename... XS>
248 auto oneof(
const X x,
const X0& x0,
const XS&... xs)
250 if constexpr(
sizeof...(XS) > 0)
251 return x == x0 ||
oneof(x, xs...);
256 template<
typename Function>
257 auto sum(std::size_t ibegin, std::size_t iend,
const Function& f)
259 using T = std::invoke_result_t<Function, std::size_t>;
260 T res =
static_cast<T
>(0);
261 for(
auto i = ibegin; i < iend; ++i)
267 template<
typename Function>
268 auto sum(std::size_t iend,
const Function& f)
270 return sum(0, iend, f);
274 template<
typename Indices,
typename Function, Requires<!isArithmetic<Indices>> = true>
277 using T = std::invoke_result_t<Function, std::size_t>;
278 T res =
static_cast<T
>(0);
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto containsfn(const Container &c, const Predicate &pred)
Return true if container a contains item x for which pred(x) evaluates to true.
Definition: Algorithms.hpp:154
auto indexfn(const Container &c, const Predicate &pred) -> std::size_t
Return the index of item x in container c for which pred(x) evaluates to true or the number of items ...
Definition: Algorithms.hpp:44
auto sortfn(Container &c, const Predicate &pred)
Sort a container in-place with predicate controlling order.
Definition: Algorithms.hpp:81
auto remove(const Container &c, const T &x)
Return a container without items x.
Definition: Algorithms.hpp:60
auto vectorize(const Container &c, const Function &f)
Return a vector by applying function f on every item in container c.
Definition: Algorithms.hpp:135
auto filter(const Container &c, const Predicate &pred)
Return a container with items x for which pred(x) evaluates to true.
Definition: Algorithms.hpp:51
auto intersect(const Container &a, const Container &b)
Return the intersection of two containers.
Definition: Algorithms.hpp:191
auto sorted(const Container &c)
Return a sorted container.
Definition: Algorithms.hpp:88
auto disjoint(const ContainerA &a, const ContainerB &b)
Return true if containers a and b have distinct items.
Definition: Algorithms.hpp:205
auto removefn(const Container &c, const Predicate &pred)
Return a container without items x for which pred(x) evaluates to true.
Definition: Algorithms.hpp:67
auto identical(const ContainerA &a, const ContainerB &b)
Return true if containers a and b have identical items.
Definition: Algorithms.hpp:215
auto sum(std::size_t ibegin, std::size_t iend, const Function &f)
Return the sum f(ibegin) + ... + f(iend-1) for a given function f.
Definition: Algorithms.hpp:257
auto contained(const ContainerA &a, const ContainerB &b)
Return true if items in container a are also in container b.
Definition: Algorithms.hpp:161
auto range(T first, T last, T step)
Return a vector with given range of values and step between them.
Definition: Algorithms.hpp:222
auto index(const Container &c, const T &x) -> std::size_t
Return the index of item x in container c or the number of items if not found.
Definition: Algorithms.hpp:37
auto transform(const Container &c, Result &res, const Function &f)
Apply a function f on every item in container c and store in res.
Definition: Algorithms.hpp:116
std::vector< Index > Indices
Define a type that represents a collection of indices.
Definition: Index.hpp:29
auto concatenate(const Container &a, const Container &b)
Return a container with items from both a and b.
Definition: Algorithms.hpp:171
auto sortedfn(const Container &c, const Predicate &pred)
Return a sorted container with predicate controlling order.
Definition: Algorithms.hpp:97
auto merge(const Container &a, const Container &b)
Return a container with items from both a and b without duplicates.
Definition: Algorithms.hpp:180
auto extract(const Container &a, const Indices &indices)
Return the items in a container at given indices.
Definition: Algorithms.hpp:124
auto oneof(const X x, const X0 &x0, const XS &... xs)
Return true if x is equal to at least one of the other arguments.
Definition: Algorithms.hpp:248
auto sort(Container &c)
Sort a container in-place.
Definition: Algorithms.hpp:74
auto contains(const Container &c, const T &x)
Return true if container a contains item x.
Definition: Algorithms.hpp:147
auto difference(const Container &a, const Container &b)
Return the difference of two containers.
Definition: Algorithms.hpp:198
auto unique(const Container &c)
Return a container without duplicates.
Definition: Algorithms.hpp:106