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