Loading...
Searching...
No Matches
IterationUtils.hpp
Go to the documentation of this file.
1// This code is based on Jet framework.
2// Copyright (c) 2018 Doyub Kim
3// CubbyFlow is voxel-based fluid simulation engine for computer games.
4// Copyright (c) 2020 CubbyFlow Team
5// Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6// AI Part: Dongheon Cho, Minseo Kim
7// We are making my contributions/submissions to this project solely in our
8// personal capacity and are not conveying any rights to any intellectual
9// property of any third parties.
10
11#ifndef CUBBYFLOW_ITERATION_UTILS_HPP
12#define CUBBYFLOW_ITERATION_UTILS_HPP
13
16
17namespace CubbyFlow
18{
19template <typename IndexType, size_t N, typename Func>
20void ForEachIndex(const Vector<IndexType, N>& begin,
21 const Vector<IndexType, N>& end, const Func& func);
22
23template <typename IndexType, typename Func>
24void ForEachIndex(const Vector<IndexType, 1>& begin,
25 const Vector<IndexType, 1>& end, const Func& func);
26
27template <typename IndexType, typename Func>
28void ForEachIndex(IndexType begin, IndexType end, const Func& func);
29
30template <typename IndexType, size_t N, typename Func>
32
33template <typename IndexType, typename Func>
35
36template <typename IndexType, typename Func>
37void ForEachIndex(IndexType size, const Func& func);
38
39template <typename IndexType, size_t N, typename Func>
41 const Vector<IndexType, N>& end, const Func& func,
43
44template <typename IndexType, typename Func>
46 const Vector<IndexType, 1>& end, const Func& func,
48
49template <typename IndexType, typename Func>
50void ParallelForEachIndex(IndexType begin, IndexType end, const Func& func,
52
53template <typename IndexType, size_t N, typename Func>
56
57template <typename IndexType, typename Func>
60
61template <typename IndexType, typename Func>
64
66template <typename ReturnType>
67std::function<ReturnType(size_t)> Unroll1(
68 const std::function<ReturnType(const Vector1UZ&)>& func)
69{
70 return [func](size_t i) { return func(Vector1UZ{ i }); };
71}
72
74template <typename ReturnType>
75std::function<ReturnType(size_t, size_t)> Unroll2(
76 const std::function<ReturnType(const Vector2UZ&)>& func)
77{
78 return [func](size_t i, size_t j) { return func(Vector2UZ{ i, j }); };
79}
80
82template <typename ReturnType>
83std::function<ReturnType(size_t, size_t, size_t)> Unroll3(
84 const std::function<ReturnType(const Vector3UZ&)>& func)
85{
86 return [func](size_t i, size_t j, size_t k) {
87 return func(Vector3UZ{ i, j, k });
88 };
89}
90
91template <typename ReturnType, size_t N>
93{
94 // Do nothing
95};
96
97template <typename ReturnType>
99{
100 static std::function<ReturnType(size_t)> Unroll(
101 const std::function<ReturnType(const Vector1UZ&)>& func)
102 {
103 return [func](size_t i) { return func(Vector1UZ{ i }); };
104 }
105};
106
107template <>
108struct GetUnroll<void, 1>
109{
110 static std::function<void(size_t)> Unroll(
111 const std::function<void(const Vector1UZ&)>& func)
112 {
113 return [func](size_t i) { func(Vector1UZ{ i }); };
114 }
115};
116
117template <typename ReturnType>
119{
120 static std::function<ReturnType(size_t, size_t)> Unroll(
121 const std::function<ReturnType(const Vector2UZ&)>& func)
122 {
123 return [func](size_t i, size_t j) { return func(Vector2UZ{ i, j }); };
124 }
125};
126
127template <>
128struct GetUnroll<void, 2>
129{
130 static std::function<void(size_t, size_t)> Unroll(
131 const std::function<void(const Vector2UZ&)>& func)
132 {
133 return [func](size_t i, size_t j) { func(Vector2UZ{ i, j }); };
134 }
135};
136
137template <typename ReturnType>
139{
140 static std::function<ReturnType(size_t, size_t, size_t)> Unroll(
141 const std::function<ReturnType(const Vector3UZ&)>& func)
142 {
143 return [func](size_t i, size_t j, size_t k) {
144 return func(Vector3UZ{ i, j, k });
145 };
146 }
147};
148
149template <>
150struct GetUnroll<void, 3>
151{
152 static std::function<void(size_t, size_t, size_t)> Unroll(
153 const std::function<void(const Vector3UZ&)>& func)
154 {
155 return [func](size_t i, size_t j, size_t k) {
156 return func(Vector3UZ{ i, j, k });
157 };
158 }
159};
160} // namespace CubbyFlow
161
163
164#endif
Definition Matrix.hpp:30
Definition pybind11Utils.hpp:21
void ForEachIndex(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func)
Definition IterationUtils-Impl.hpp:51
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:738
void ParallelForEachIndex(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func, ExecutionPolicy policy)
Definition IterationUtils-Impl.hpp:98
std::function< ReturnType(size_t, size_t, size_t)> Unroll3(const std::function< ReturnType(const Vector3UZ &)> &func)
Unrolls vector-based DataPositionFunc indexing to size_t-based function.
Definition IterationUtils.hpp:83
std::function< ReturnType(size_t)> Unroll1(const std::function< ReturnType(const Vector1UZ &)> &func)
Unrolls vector-based indexing to size_t-based function.
Definition IterationUtils.hpp:67
ExecutionPolicy
Execution policy tag.
Definition Parallel.hpp:18
std::function< ReturnType(size_t, size_t)> Unroll2(const std::function< ReturnType(const Vector2UZ &)> &func)
Unrolls vector-based indexing to size_t-based function.
Definition IterationUtils.hpp:75
static std::function< ReturnType(size_t)> Unroll(const std::function< ReturnType(const Vector1UZ &)> &func)
Definition IterationUtils.hpp:100
static std::function< ReturnType(size_t, size_t)> Unroll(const std::function< ReturnType(const Vector2UZ &)> &func)
Definition IterationUtils.hpp:120
static std::function< ReturnType(size_t, size_t, size_t)> Unroll(const std::function< ReturnType(const Vector3UZ &)> &func)
Definition IterationUtils.hpp:140
static std::function< void(size_t)> Unroll(const std::function< void(const Vector1UZ &)> &func)
Definition IterationUtils.hpp:110
static std::function< void(size_t, size_t)> Unroll(const std::function< void(const Vector2UZ &)> &func)
Definition IterationUtils.hpp:130
static std::function< void(size_t, size_t, size_t)> Unroll(const std::function< void(const Vector3UZ &)> &func)
Definition IterationUtils.hpp:152
Definition IterationUtils.hpp:93