opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
functors.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <algorithm>
7#include <concepts>
8#include <cstddef>
9#include <functional>
10
11namespace osc
12{
13 // returns a vector containing `op(xv)` for each `xv` in `x`
14 template<typename T, size_t N, std::invocable<const T&> UnaryOperation>
15 constexpr auto map(const Vector<T, N>& x, UnaryOperation op) -> Vector<decltype(std::invoke(op, x[0])), N>
16 {
17 Vector<decltype(std::invoke(op, x[0])), N> rv{};
18 for (size_t i = 0; i < N; ++i) {
19 rv[i] = std::invoke(op, x[i]);
20 }
21 return rv;
22 }
23
24 // returns a vector containing `op(xv, yv)` for each `(xv, yv)` in `x` and `y`
25 template<typename T, size_t N, std::invocable<const T&, const T&> BinaryOperation>
26 constexpr auto map(const Vector<T, N>& x, const Vector<T, N>& y, BinaryOperation op) -> Vector<decltype(std::invoke(op, x[0], y[0])), N>
27 {
28 Vector<decltype(std::invoke(op, x[0], y[0])), N> rv{};
29 for (size_t i = 0; i < N; ++i) {
30 rv[i] = std::invoke(op, x[i], y[i]);
31 }
32 return rv;
33 }
34
35 // returns a vector containing `op(xv, yv, zv)` for each `(xv, yv, zv)` in `x`, `y`, and `z`
36 template<typename T, size_t N, std::invocable<const T&, const T&, const T&> TernaryOperation>
37 constexpr auto map(const Vector<T, N>& x, const Vector<T, N>& y, const Vector<T, N>& z, TernaryOperation op)
38 -> Vector<decltype(std::invoke(op, x[0], y[0], z[0])), N>
39 {
40 Vector<decltype(std::invoke(op, x[0], y[0], z[0])), N> rv{};
41 for (size_t i = 0; i < N; ++i) {
42 rv[i] = std::invoke(op, x[i], y[i], z[i]);
43 }
44 return rv;
45 }
46
47 // tests if all elements in `v` are `true`
48 template<size_t N>
49 constexpr bool all_of(const Vector<bool, N>& v)
50 {
51 return std::ranges::all_of(v, std::identity{});
52 }
53
54 // tests if any element in `v` is `true`
55 template<size_t N>
56 constexpr bool any_of(const Vector<bool, N>& v)
57 {
58 return std::ranges::any_of(v, std::identity{});
59 }
60
61 // tests if no elements in `v` are `true`
62 template<size_t N>
63 constexpr bool none_of(const Vector<bool, N>& v)
64 {
65 return std::ranges::none_of(v, std::identity{});
66 }
67}
Definition vector.h:63
Definition custom_decoration_generator.h:5
constexpr auto map(const Rgba< T > &x, UnaryOperation op) -> Rgba< decltype(std::invoke(op, x[0]))>
Definition rgba.h:208
constexpr U to(T &&value)
Definition conversion.h:56
constexpr bool all_of(const Vector< bool, N > &v)
Definition functors.h:49
constexpr bool any_of(const Vector< bool, N > &v)
Definition functors.h:56
constexpr bool none_of(const Vector< bool, N > &v)
Definition functors.h:63