VectorN.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: VectorN.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: General purpose dynamically-sized N-D vector class.
6 > Created Time: 2017/09/28
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_VECTORN_H
10 #define CUBBYFLOW_VECTORN_H
11 
14 
15 namespace CubbyFlow
16 {
17  // MARK: VectorN
26  template <typename T>
27  class VectorN final : public VectorExpression<T, VectorN<T>>
28  {
29  public:
30  static_assert(std::is_floating_point<T>::value, "VectorN only can be instantiated with floating point types");
31 
32  using ContainerType = std::vector<T>;
33 
34  // MARK: Constructors
36  VectorN();
37 
39  VectorN(size_t n, const T& val = 0);
40 
42  template <typename U>
43  VectorN(const std::initializer_list<U>& list);
44 
46  template <typename E>
47  VectorN(const VectorExpression<T, E>& other);
48 
50  VectorN(const VectorN& other);
51 
53  VectorN(VectorN&& other) noexcept;
54 
55  // MARK: Basic setters
57  void Resize(size_t n, const T& val = 0);
58 
60  void Clear();
61 
63  void Set(const T& s);
64 
66  template <typename U>
67  void Set(const std::initializer_list<U>& list);
68 
70  template <typename E>
71  void Set(const VectorExpression<T, E>& other);
72 
74  void Append(const T& val);
75 
77  void Swap(VectorN& other);
78 
80  void SetZero();
81 
83  void Normalize();
84 
85  // MARK: Basic getters
87  size_t size() const;
88 
90  T* data();
91 
93  const T* data() const;
94 
96  typename ContainerType::iterator begin();
97 
99  typename ContainerType::const_iterator begin() const;
100 
102  typename ContainerType::iterator end();
103 
105  typename ContainerType::const_iterator end() const;
106 
109 
112 
114  T At(size_t i) const;
115 
117  T& At(size_t i);
118 
120  T Sum() const;
121 
123  T Avg() const;
124 
126  T Min() const;
127 
129  T Max() const;
130 
132  T AbsMin() const;
133 
135  T AbsMax() const;
136 
138  size_t DominantAxis() const;
139 
141  size_t SubdominantAxis() const;
142 
145 
147  T Length() const;
148 
150  T LengthSquared() const;
151 
153  template <typename E>
154  T DistanceTo(const E& other) const;
155 
157  template <typename E>
158  T DistanceSquaredTo(const E& other) const;
159 
161  template <typename U>
163 
165  template <typename E>
166  bool IsEqual(const E& other) const;
167 
169  template <typename E>
170  bool IsSimilar(const E& other, T epsilon = std::numeric_limits<T>::epsilon()) const;
171 
172  // MARK: Binary operations: new instance = this (+) v
174  template <typename E>
175  VectorAdd<T, VectorN, E> Add(const E& v) const;
176 
178  VectorScalarAdd<T, VectorN> Add(const T& s) const;
179 
181  template <typename E>
182  VectorSub<T, VectorN, E> Sub(const E& v) const;
183 
185  VectorScalarSub<T, VectorN> Sub(const T& s) const;
186 
188  template <typename E>
189  VectorMul<T, VectorN, E> Mul(const E& v) const;
190 
192  VectorScalarMul<T, VectorN> Mul(const T& s) const;
193 
195  template <typename E>
196  VectorDiv<T, VectorN, E> Div(const E& v) const;
197 
199  VectorScalarDiv<T, VectorN> Div(const T& s) const;
200 
202  template <typename E>
203  T Dot(const E& v) const;
204 
205  // MARK: Binary operations: new instance = v (+) this
207  VectorScalarRSub<T, VectorN> RSub(const T& s) const;
208 
210  template <typename E>
211  VectorSub<T, VectorN, E> RSub(const E& v) const;
212 
214  VectorScalarRDiv<T, VectorN> RDiv(const T& s) const;
215 
217  template <typename E>
218  VectorDiv<T, VectorN, E> RDiv(const E& v) const;
219 
220  // MARK: Augmented operations: this (+)= v
222  void IAdd(const T& s);
223 
225  template <typename E>
226  void IAdd(const E& v);
227 
229  void ISub(const T& s);
230 
232  template <typename E>
233  void ISub(const E& v);
234 
236  void IMul(const T& s);
237 
239  template <typename E>
240  void IMul(const E& v);
241 
243  void IDiv(const T& s);
244 
246  template <typename E>
247  void IDiv(const E& v);
248 
249  // MARK: Operators
266  template <typename Callback>
267  void ForEach(Callback func) const;
268 
285  template <typename Callback>
286  void ForEachIndex(Callback func) const;
287 
309  template <typename Callback>
310  void ParallelForEach(Callback func);
311 
330  template <typename Callback>
331  void ParallelForEachIndex(Callback func) const;
332 
334  T operator[](size_t i) const;
335 
337  T& operator[](size_t i);
338 
340  template <typename U>
341  VectorN& operator=(const std::initializer_list<U>& list);
342 
344  template <typename E>
346 
348  VectorN& operator=(const VectorN& other);
349 
351  VectorN& operator=(VectorN&& other) noexcept;
352 
354  VectorN& operator+=(const T& s);
355 
357  template <typename E>
358  VectorN& operator+=(const E& v);
359 
361  VectorN& operator-=(const T& s);
362 
364  template <typename E>
365  VectorN& operator-=(const E& v);
366 
368  VectorN& operator*=(const T& s);
369 
371  template <typename E>
372  VectorN& operator*=(const E& v);
373 
375  VectorN& operator/=(const T& s);
376 
378  template <typename E>
379  VectorN& operator/=(const E& v);
380 
382  template <typename E>
383  bool operator==(const E& v) const;
384 
386  template <typename E>
387  bool operator!=(const E& v) const;
388 
389  private:
390  ContainerType m_elements;
391  };
392 
395 
398 }
399 
401 
402 #endif
VectorSub< T, VectorN, E > Sub(const E &v) const
Computes this - v.
T * data()
Returns the raw pointer to the vector data.
Definition: VectorN-Impl.h:123
bool operator!=(const E &v) const
Returns true if other is the not same as this vector.
Definition: VectorN-Impl.h:682
void SetZero()
Sets all elements to zero.
Definition: VectorN-Impl.h:105
T At(size_t i) const
Returns const reference to the i -th element of the vector.
Definition: VectorN-Impl.h:171
void ForEach(Callback func) const
Iterates the vector and invoke given func for each element.
Definition: VectorN-Impl.h:545
void ParallelForEachIndex(Callback func) const
Iterates the vector and invoke given func for each index in parallel using multi-threading.
Definition: VectorN-Impl.h:566
void ISub(const T &s)
Computes this -= (s, s, ... , s).
Definition: VectorN-Impl.h:505
ConstArrayAccessor1< T > ConstAccessor() const
Returns the const array accessor.
Definition: VectorN-Impl.h:165
VectorN & operator*=(const T &s)
Computes this *= (s, s, ... , s)
Definition: VectorN-Impl.h:644
void ForEachIndex(Callback func) const
Iterates the vector and invoke given func for each index.
Definition: VectorN-Impl.h:552
VectorTypeCast< U, VectorN< T >, T > CastTo() const
Returns a vector with different value type.
Definition: VectorN-Impl.h:348
VectorN()
Constructs empty vector.
Definition: VectorN-Impl.h:19
Base class for vector expression.
Definition: VectorExpression.h:28
VectorScalarRDiv< T, VectorN > RDiv(const T &s) const
Computes (s, s, ... , s) / this.
Definition: VectorN-Impl.h:479
void Normalize()
Normalizes this vector.
Definition: VectorN-Impl.h:111
T DistanceTo(const E &other) const
Returns the distance to the other vector.
Definition: VectorN-Impl.h:320
VectorDiv< T, VectorN, E > Div(const E &v) const
Computes this / v.
Vector expression for unary operation.
Definition: VectorExpression.h:50
VectorN & operator/=(const T &s)
Computes this /= (s, s, ... , s)
Definition: VectorN-Impl.h:659
void IDiv(const T &s)
Computes this /= (s, s, ... , s).
Definition: VectorN-Impl.h:531
VectorScalarRSub< T, VectorN > RSub(const T &s) const
Computes (s, s, ... , s) - this.
Definition: VectorN-Impl.h:466
VectorN & operator=(const std::initializer_list< U > &list)
Sets vector with given initializer list.
General purpose dynamically-sizedN-D vector class.
Definition: VectorN.h:27
1-D read-only array accessor class.
Definition: ArrayAccessor1.h:185
Vector expression for binary operation.
Definition: VectorExpression.h:85
void ParallelForEach(Callback func)
Iterates the vector and invoke given func for each element in parallel using multi-threading.
Definition: VectorN-Impl.h:559
T Min() const
Returns the minimum element.
Definition: VectorN-Impl.h:206
VectorN & operator+=(const T &s)
Computes this += (s, s, ... , s)
Definition: VectorN-Impl.h:614
size_t DominantAxis() const
Returns the index of the dominant axis.
Definition: VectorN-Impl.h:278
Definition: pybind11Utils.h:24
void IAdd(const T &s)
Computes this += (s, s, ... , s).
Definition: VectorN-Impl.h:492
ContainerType::iterator end()
Returns the end iterator of the vector.
Definition: VectorN-Impl.h:147
size_t size() const
Returns the size of the vector.
Definition: VectorN-Impl.h:117
T Sum() const
Returns the sum of all the elements.
Definition: VectorN-Impl.h:183
T Length() const
Returns the length of the vector.
Definition: VectorN-Impl.h:307
T Max() const
Returns the maximum element.
Definition: VectorN-Impl.h:225
T Avg() const
Returns the average of all the elements.
Definition: VectorN-Impl.h:200
VectorMul< T, VectorN, E > Mul(const E &v) const
Computes this * v.
std::vector< double > ContainerType
Definition: VectorN.h:32
T LengthSquared() const
Returns the squared length of the vector.
Definition: VectorN-Impl.h:313
void Append(const T &val)
Adds an element.
Definition: VectorN-Impl.h:93
T DistanceSquaredTo(const E &other) const
Returns the squared distance to the other vector.
Definition: VectorN-Impl.h:327
bool operator==(const E &v) const
Returns true if other is the same as this vector.
Definition: VectorN-Impl.h:675
VectorScalarDiv< T, VectorN > Normalized() const
Returns normalized vector.
Definition: VectorN-Impl.h:300
T operator[](size_t i) const
Returns the i -th element.
Definition: VectorN-Impl.h:572
ContainerType::iterator begin()
Returns the begin iterator of the vector.
Definition: VectorN-Impl.h:135
1-D array accessor class.
Definition: ArrayAccessor1.h:28
T Dot(const E &v) const
Computes dot product.
Definition: VectorN-Impl.h:447
void Resize(size_t n, const T &val=0)
Resizes to n dimensional vector with initial value val.
Definition: VectorN-Impl.h:57
void Set(const T &s)
Sets all elements to s.
Definition: VectorN-Impl.h:69
T AbsMax() const
Returns the absolute maximum element.
Definition: VectorN-Impl.h:261
VectorAdd< T, VectorN, E > Add(const E &v) const
Computes this + v.
void IMul(const T &s)
Computes this *= (s, s, ... , s).
Definition: VectorN-Impl.h:518
bool IsEqual(const E &other) const
Returns true if other is the same as this vector.
Definition: VectorN-Impl.h:355
size_t SubdominantAxis() const
Returns the index of the subdominant axis.
Definition: VectorN-Impl.h:289
bool IsSimilar(const E &other, T epsilon=std::numeric_limits< T >::epsilon()) const
Returns true if other is similar to this vector.
Definition: VectorN-Impl.h:375
Vector expression for matrix-scalar binary operation.
Definition: VectorExpression.h:114
VectorN & operator-=(const T &s)
Computes this -= (s, s, ... , s)
Definition: VectorN-Impl.h:629
void Clear()
Clears the vector and make it zero-dimensional.
Definition: VectorN-Impl.h:63
ArrayAccessor1< T > Accessor()
Returns the array accessor.
Definition: VectorN-Impl.h:159
void Swap(VectorN &other)
Swaps the content of the vector with other vector.
Definition: VectorN-Impl.h:99
T AbsMin() const
Returns the absolute minimum element.
Definition: VectorN-Impl.h:244