Serial-Impl.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_SERIAL_IMPL_HPP
12 #define CUBBYFLOW_SERIAL_IMPL_HPP
13 
14 #include <algorithm>
15 #include <functional>
16 
17 namespace CubbyFlow
18 {
19 template <typename RandomIterator, typename T>
20 void SerialFill(const RandomIterator& begin, const RandomIterator& end,
21  const T& value)
22 {
23  size_t size = static_cast<size_t>(end - begin);
24 
25  SerialFor(static_cast<size_t>(0), size,
26  [begin, value](size_t i) { begin[i] = value; });
27 }
28 
29 template <typename IndexType, typename Function>
30 void SerialFor(IndexType beginIndex, IndexType endIndex,
31  const Function& function)
32 {
33  for (IndexType i = beginIndex; i < endIndex; ++i)
34  {
35  function(i);
36  }
37 }
38 
39 template <typename IndexType, typename Function>
40 void SerialFor(IndexType beginIndexX, IndexType endIndexX,
41  IndexType beginIndexY, IndexType endIndexY,
42  const Function& function)
43 {
44  for (IndexType j = beginIndexY; j < endIndexY; ++j)
45  {
46  for (IndexType i = beginIndexX; i < endIndexX; ++i)
47  {
48  function(i, j);
49  }
50  }
51 }
52 
53 template <typename IndexType, typename Function>
54 void SerialFor(IndexType beginIndexX, IndexType endIndexX,
55  IndexType beginIndexY, IndexType endIndexY,
56  IndexType beginIndexZ, IndexType endIndexZ,
57  const Function& function)
58 {
59  for (IndexType k = beginIndexZ; k < endIndexZ; ++k)
60  {
61  for (IndexType j = beginIndexY; j < endIndexY; ++j)
62  {
63  for (IndexType i = beginIndexX; i < endIndexX; ++i)
64  {
65  function(i, j, k);
66  }
67  }
68  }
69 }
70 
71 template <typename RandomIterator>
72 void SerialSort(RandomIterator begin, RandomIterator end)
73 {
74  SerialSort(begin, end, std::less<typename RandomIterator::value_type>());
75 }
76 
77 template <typename RandomIterator, typename SortingFunction>
78 void SerialSort(RandomIterator begin, RandomIterator end,
79  const SortingFunction& sortingFunction)
80 {
81  std::sort(begin, end, sortingFunction);
82 }
83 } // namespace CubbyFlow
84 
85 #endif
void SerialFill(const RandomIterator &begin, const RandomIterator &end, const T &value)
Fills from begin to end with value.
Definition: Serial-Impl.hpp:20
void SerialFor(IndexType beginIndex, IndexType endIndex, const Function &function)
Makes a for-loop from beginIndex to endIndex.
Definition: Serial-Impl.hpp:30
Definition: pybind11Utils.hpp:20
void SerialSort(RandomIterator begin, RandomIterator end)
Sorts a container.
Definition: Serial-Impl.hpp:72