Serial-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Serial-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Serial functions for CubbyFlow.
6 > Created Time: 2017/07/05
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_SERIAL_IMPL_H
10 #define CUBBYFLOW_SERIAL_IMPL_H
11 
12 #include <algorithm>
13 
14 namespace CubbyFlow
15 {
16  template <typename RandomIterator, typename T>
17  void SerialFill(const RandomIterator& begin, const RandomIterator& end, const T& value)
18  {
19  size_t size = static_cast<size_t>(end - begin);
20 
21  SerialFor(size_t(0), size, [begin, value](size_t i)
22  {
23  begin[i] = value;
24  });
25  }
26 
27  template <typename IndexType, typename Function>
28  void SerialFor(IndexType beginIndex, IndexType endIndex, const Function& function)
29  {
30  for (IndexType i = beginIndex; i < endIndex; ++i)
31  {
32  function(i);
33  }
34  }
35 
36  template <typename IndexType, typename Function>
37  void SerialFor(
38  IndexType beginIndexX, IndexType endIndexX,
39  IndexType beginIndexY, IndexType endIndexY,
40  const Function& function)
41  {
42  for (IndexType j = beginIndexY; j < endIndexY; ++j)
43  {
44  for (IndexType i = beginIndexX; i < endIndexX; ++i)
45  {
46  function(i, j);
47  }
48  }
49  }
50 
51  template <typename IndexType, typename Function>
52  void SerialFor(
53  IndexType beginIndexX, IndexType endIndexX,
54  IndexType beginIndexY, IndexType endIndexY,
55  IndexType beginIndexZ, IndexType endIndexZ,
56  const Function& function)
57  {
58  for (IndexType k = beginIndexZ; k < endIndexZ; ++k)
59  {
60  for (IndexType j = beginIndexY; j < endIndexY; ++j)
61  {
62  for (IndexType i = beginIndexX; i < endIndexX; ++i)
63  {
64  function(i, j, k);
65  }
66  }
67  }
68  }
69 
70  template<typename RandomIterator>
71  void SerialSort(RandomIterator begin, RandomIterator end)
72  {
73  SerialSort(begin, end, std::less<typename RandomIterator::value_type>());
74  }
75 
76  template<typename RandomIterator, typename SortingFunction>
77  void SerialSort(RandomIterator begin, RandomIterator end,
78  const SortingFunction& sortingFunction)
79  {
80  std::sort(begin, end, sortingFunction);
81  }
82 }
83 
84 #endif
void SerialFill(const RandomIterator &begin, const RandomIterator &end, const T &value)
Fills from begin to end with value.
Definition: Serial-Impl.h:17
void SerialFor(IndexType beginIndex, IndexType endIndex, const Function &function)
Makes a for-loop from beginIndex to endIndex.
Definition: Serial-Impl.h:28
Definition: pybind11Utils.h:24
void SerialSort(RandomIterator begin, RandomIterator end)
Sorts a container.
Definition: Serial-Impl.h:71