Point-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Point-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Generic N-D point class.
6 > Created Time: 2017/02/03
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_POINT_IMPL_H
10 #define CUBBYFLOW_POINT_IMPL_H
11 
12 #include <cassert>
13 
14 namespace CubbyFlow
15 {
16  template <typename T, size_t N>
18  {
19  for (auto& elem : m_elements)
20  {
21  elem = static_cast<T>(0);
22  }
23  }
24 
25  template <typename T, size_t N>
26  template <typename... Params>
27  Point<T, N>::Point(Params... params)
28  {
29  static_assert(sizeof...(params) == N, "Invalid number of parameters.");
30 
31  SetAt(0, params...);
32  }
33 
34  template <typename T, size_t N>
35  template <typename U>
36  Point<T, N>::Point(const std::initializer_list<U>& list)
37  {
38  assert(list.size() >= N);
39 
40  size_t i = 0;
41  for (const auto& inputElem : list)
42  {
43  m_elements[i] = static_cast<T>(inputElem);
44  ++i;
45  }
46  }
47 
48  template <typename T, size_t N>
49  Point<T, N>::Point(const Point& other) :
50  m_elements(other.m_elements)
51  {
52  // Do nothing
53  }
54 
55  template <typename T, size_t N>
56  template <typename U>
57  void Point<T, N>::Set(const std::initializer_list<U>& list)
58  {
59  assert(list.size() >= N);
60 
61  size_t i = 0;
62  for (const auto& inputElem : list)
63  {
64  m_elements[i] = static_cast<T>(inputElem);
65  ++i;
66  }
67  }
68 
69  template <typename T, size_t N>
70  void Point<T, N>::Set(const Point& other)
71  {
72  m_elements = other.m_elements;
73  }
74 
75  template <typename T, size_t N>
76  template <typename U>
77  Point<T, N>& Point<T, N>::operator=(const std::initializer_list<U>& list)
78  {
79  Set(list);
80  return *this;
81  }
82 
83  template <typename T, size_t N>
85  {
86  Set(other);
87  return *this;
88  }
89 
90  template <typename T, size_t N>
91  const T& Point<T, N>::operator[](size_t i) const
92  {
93  return m_elements[i];
94  }
95 
96  template <typename T, size_t N>
98  {
99  return m_elements[i];
100  }
101 
102  template <typename T, size_t N>
103  template <typename... Params>
104  void Point<T, N>::SetAt(size_t i, T v, Params... params)
105  {
106  m_elements[i] = v;
107 
108  SetAt(i + 1, params...);
109  }
110 
111  template <typename T, size_t N>
112  void Point<T, N>::SetAt(size_t i, T v)
113  {
114  m_elements[i] = v;
115  }
116 }
117 
118 #endif
void Set(const std::initializer_list< U > &list)
Set point instance with initializer list.
Definition: Point-Impl.h:57
Generic N-D point class.
Definition: Point.h:24
Definition: pybind11Utils.h:24
Point & operator=(const std::initializer_list< U > &list)
Set point instance with initializer list.
const T & operator[](size_t i) const
Returns the const reference to the i -th element.
Definition: Point-Impl.h:91
Point()
Constructs a point with zeros.
Definition: Point-Impl.h:17