Jet  v1.3.3
vector_n.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_VECTOR_N_H_
8 #define INCLUDE_JET_VECTOR_N_H_
9 
10 #include <jet/array_accessor1.h>
11 #include <jet/vector_expression.h>
12 
13 #include <initializer_list>
14 #include <limits>
15 #include <vector>
16 
17 namespace jet {
18 
19 // MARK: VectorN
20 
29 template <typename T>
30 class VectorN final : public VectorExpression<T, VectorN<T>> {
31  public:
32  static_assert(std::is_floating_point<T>::value,
33  "VectorN only can be instantiated with floating point types");
34 
35  typedef std::vector<T> ContainerType;
36 
37  // MARK: Constructors
38 
41 
43  VectorN(size_t n, const T& val = 0);
44 
46  template <typename U>
47  VectorN(const std::initializer_list<U>& lst);
48 
50  template <typename E>
52 
54  VectorN(const VectorN& other);
55 
57  VectorN(VectorN&& other);
58 
59  // MARK: Basic setters
60 
62  void resize(size_t n, const T& val = 0);
63 
65  void clear();
66 
68  void set(const T& s);
69 
71  template <typename U>
72  void set(const std::initializer_list<U>& lst);
73 
75  template <typename E>
76  void set(const VectorExpression<T, E>& other);
77 
79  void append(const T& val);
80 
82  void swap(VectorN& other);
83 
85  void setZero();
86 
88  void normalize();
89 
90  // MARK: Basic getters
91 
93  size_t size() const;
94 
96  T* data();
97 
99  const T* const data() const;
100 
102  typename ContainerType::iterator begin();
103 
105  typename ContainerType::const_iterator begin() const;
106 
108  typename ContainerType::iterator end();
109 
111  typename ContainerType::const_iterator end() const;
112 
115 
118 
120  T at(size_t i) const;
121 
123  T& at(size_t i);
124 
126  T sum() const;
127 
129  T avg() const;
130 
132  T min() const;
133 
135  T max() const;
136 
138  T absmin() const;
139 
141  T absmax() const;
142 
144  size_t dominantAxis() const;
145 
147  size_t subminantAxis() const;
148 
151 
153  T length() const;
154 
156  T lengthSquared() const;
157 
159  template <typename E>
160  T distanceTo(const E& other) const;
161 
163  template <typename E>
164  T distanceSquaredTo(const E& other) const;
165 
167  template <typename U>
169 
171  template <typename E>
172  bool isEqual(const E& other) const;
173 
175  template <typename E>
176  bool isSimilar(const E& other,
177  T epsilon = std::numeric_limits<T>::epsilon()) const;
178 
179  // MARK: Binary operations: new instance = this (+) v
180 
182  template <typename E>
183  VectorAdd<T, VectorN, E> add(const E& v) const;
184 
186  VectorScalarAdd<T, VectorN> add(const T& s) const;
187 
189  template <typename E>
190  VectorSub<T, VectorN, E> sub(const E& v) const;
191 
193  VectorScalarSub<T, VectorN> sub(const T& s) const;
194 
196  template <typename E>
197  VectorMul<T, VectorN, E> mul(const E& v) const;
198 
200  VectorScalarMul<T, VectorN> mul(const T& s) const;
201 
203  template <typename E>
204  VectorDiv<T, VectorN, E> div(const E& v) const;
205 
207  VectorScalarDiv<T, VectorN> div(const T& s) const;
208 
210  template <typename E>
211  T dot(const E& v) const;
212 
213  // MARK: Binary operations: new instance = v (+) this
214 
217 
219  template <typename E>
220  VectorSub<T, VectorN, E> rsub(const E& v) const;
221 
224 
226  template <typename E>
227  VectorDiv<T, VectorN, E> rdiv(const E& v) const;
228 
229  // MARK: Augmented operations: this (+)= v
230 
232  void iadd(const T& s);
233 
235  template <typename E>
236  void iadd(const E& v);
237 
239  void isub(const T& s);
240 
242  template <typename E>
243  void isub(const E& v);
244 
246  void imul(const T& s);
247 
249  template <typename E>
250  void imul(const E& v);
251 
253  void idiv(const T& s);
254 
256  template <typename E>
257  void idiv(const E& v);
258 
259  // MARK: Operators
260 
276  template <typename Callback>
277  void forEach(Callback func) const;
278 
294  template <typename Callback>
295  void forEachIndex(Callback func) const;
296 
317  template <typename Callback>
318  void parallelForEach(Callback func);
319 
337  template <typename Callback>
338  void parallelForEachIndex(Callback func) const;
339 
341  T operator[](size_t i) const;
342 
344  T& operator[](size_t i);
345 
347  template <typename U>
348  VectorN& operator=(const std::initializer_list<U>& lst);
349 
351  template <typename E>
353 
355  VectorN& operator=(const VectorN& other);
356 
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  VectorN& operator/=(const T& s);
383 
385  template <typename E>
386  VectorN& operator/=(const E& v);
387 
389  template <typename E>
390  bool operator==(const E& v) const;
391 
393  template <typename E>
394  bool operator!=(const E& v) const;
395 
396  private:
397  ContainerType _elements;
398 };
399 
402 
405 
406 } // namespace jet
407 
408 #include "detail/vector_n-inl.h"
409 
410 #endif // INCLUDE_JET_VECTOR_N_H_
jet::VectorN::normalized
VectorScalarDiv< T, VectorN > normalized() const
Returns normalized vector.
jet::VectorN::min
T min() const
Returns the minimum element.
jet::VectorN::dominantAxis
size_t dominantAxis() const
Returns the index of the dominant axis.
jet::VectorN
General purpose dynamically-sizedN-D vector class.
Definition: vector_n.h:30
jet::VectorN::parallelForEach
void parallelForEach(Callback func)
Iterates the vector and invoke given func for each element in parallel using multi-threading.
jet::VectorExpression
Base class for vector expression.
Definition: vector_expression.h:25
jet::VectorN::size
size_t size() const
Returns the size of the vector.
jet::VectorN::operator==
bool operator==(const E &v) const
Returns true if other is the same as this vector.
jet::VectorN::VectorN
VectorN(const VectorExpression< T, E > &other)
Constructs vector with expression template.
jet::ConstArrayAccessor< T, 1 >
1-D read-only array accessor class.
Definition: array_accessor1.h:184
jet::VectorN::idiv
void idiv(const T &s)
Computes this /= (s, s, ... , s).
jet::VectorN::subminantAxis
size_t subminantAxis() const
Returns the index of the subminant axis.
jet::VectorN::operator*=
VectorN & operator*=(const E &v)
Computes this *= v.
jet::VectorN::add
VectorAdd< T, VectorN, E > add(const E &v) const
Computes this + v.
jet::VectorN::iadd
void iadd(const T &s)
Computes this += (s, s, ... , s).
jet::VectorN::iadd
void iadd(const E &v)
Computes this += v.
jet::VectorN::imul
void imul(const T &s)
Computes this *= (s, s, ... , s).
jet::VectorN::accessor
ArrayAccessor1< T > accessor()
Returns the array accessor.
jet::VectorN::lengthSquared
T lengthSquared() const
Returns the squared length of the vector.
jet::VectorN::operator=
VectorN & operator=(const VectorExpression< T, E > &other)
Sets vector with expression template.
jet::VectorN::forEach
void forEach(Callback func) const
Iterates the vector and invoke given func for each element.
jet::VectorUnaryOp
Vector expression for unary operation.
Definition: vector_expression.h:47
jet::VectorN::resize
void resize(size_t n, const T &val=0)
Resizes to n dimensional vector with initial value val.
jet::VectorN::mul
VectorScalarMul< T, VectorN > mul(const T &s) const
Computes this * (s, s, ... , s).
jet::VectorN::isub
void isub(const E &v)
Computes this -= v.
jet::VectorN::operator[]
T operator[](size_t i) const
Returns the i -th element.
jet::VectorScalarBinaryOp
Vector expression for matrix-scalar binary operation.
Definition: vector_expression.h:114
jet::VectorN::data
const T *const data() const
Returns the const raw pointer to the vector data.
jet::VectorN::isEqual
bool isEqual(const E &other) const
Returns true if other is the same as this vector.
jet::VectorN::idiv
void idiv(const E &v)
Computes this /= v.
jet::VectorN::operator-=
VectorN & operator-=(const E &v)
Computes this -= v.
jet::VectorN::parallelForEachIndex
void parallelForEachIndex(Callback func) const
Iterates the vector and invoke given func for each index in parallel using multi-threading.
jet::VectorN::at
T at(size_t i) const
Returns const reference to the i -th element of the vector.
jet::VectorN::set
void set(const VectorExpression< T, E > &other)
Sets vector with expression template.
jet::VectorN::operator*=
VectorN & operator*=(const T &s)
Computes this *= (s, s, ... , s)
jet::VectorNF
VectorN< float > VectorNF
Float-type N-D vector.
Definition: vector_n.h:401
jet::VectorN::sub
VectorScalarSub< T, VectorN > sub(const T &s) const
Computes this - (s, s, ... , s).
jet::VectorN::swap
void swap(VectorN &other)
Swaps the content of the vector with other vector.
jet::VectorBinaryOp
Vector expression for binary operation.
Definition: vector_expression.h:84
jet::VectorN::distanceTo
T distanceTo(const E &other) const
Returns the distance to the other vector.
jet
Definition: advection_solver2.h:18
jet::VectorN::VectorN
VectorN(const std::initializer_list< U > &lst)
Constructs vector with given initializer list.
jet::VectorN::operator-=
VectorN & operator-=(const T &s)
Computes this -= (s, s, ... , s)
jet::VectorN::absmin
T absmin() const
Returns the absolute minimum element.
vector_expression.h
jet::VectorN::set
void set(const std::initializer_list< U > &lst)
Sets all elements with given initializer list.
jet::VectorN::set
void set(const T &s)
Sets all elements to s.
jet::VectorN::VectorN
VectorN(size_t n, const T &val=0)
Constructs default vector (val, val, ... , val).
jet::VectorN::operator=
VectorN & operator=(VectorN &&other)
Move assignment.
jet::VectorN::rsub
VectorSub< T, VectorN, E > rsub(const E &v) const
Computes v - this.
jet::VectorN::operator=
VectorN & operator=(const std::initializer_list< U > &lst)
Sets vector with given initializer list.
jet::VectorN::add
VectorScalarAdd< T, VectorN > add(const T &s) const
Computes this + (s, s, ... , s).
jet::VectorN::data
T * data()
Returns the raw pointer to the vector data.
jet::VectorN::operator!=
bool operator!=(const E &v) const
Returns true if other is the not same as this vector.
jet::VectorN::dot
T dot(const E &v) const
Computes dot product.
jet::VectorN::isub
void isub(const T &s)
Computes this -= (s, s, ... , s).
jet::VectorN::end
ContainerType::iterator end()
Returns the end iterator of the vector.
jet::VectorN::at
T & at(size_t i)
Returns reference to the i -th element of the vector.
jet::VectorN::operator/=
VectorN & operator/=(const E &v)
Computes this /= v.
jet::VectorN::constAccessor
ConstArrayAccessor1< T > constAccessor() const
Returns the const array accessor.
jet::VectorN::absmax
T absmax() const
Returns the absolute maximum element.
jet::VectorN::VectorN
VectorN(const VectorN &other)
Copy constructor.
jet::VectorN::operator=
VectorN & operator=(const VectorN &other)
Copy assignment.
jet::VectorN::max
T max() const
Returns the maximum element.
jet::VectorN::sum
T sum() const
Returns the sum of all the elements.
jet::VectorN::sub
VectorSub< T, VectorN, E > sub(const E &v) const
Computes this - v.
jet::VectorN::normalize
void normalize()
Normalizes this vector.
jet::VectorN::clear
void clear()
Clears the vector and make it zero-dimensional.
jet::VectorN::VectorN
VectorN()
Constructs empty vector.
jet::VectorN::operator[]
T & operator[](size_t i)
Returns the reference to the i -th element.
jet::VectorN::rdiv
VectorScalarRDiv< T, VectorN > rdiv(const T &s) const
Computes (s, s, ... , s) / this.
jet::VectorN::begin
ContainerType::iterator begin()
Returns the begin iterator of the vector.
jet::VectorN::avg
T avg() const
Returns the average of all the elements.
jet::VectorN::div
VectorScalarDiv< T, VectorN > div(const T &s) const
Computes this / (s, s, ... , s).
jet::VectorN::rdiv
VectorDiv< T, VectorN, E > rdiv(const E &v) const
Computes v / this.
jet::VectorN::distanceSquaredTo
T distanceSquaredTo(const E &other) const
Returns the squared distance to the other vector.
jet::VectorN::begin
ContainerType::const_iterator begin() const
Returns the begin const iterator of the vector.
jet::VectorN::operator+=
VectorN & operator+=(const T &s)
Computes this += (s, s, ... , s)
jet::VectorN::end
ContainerType::const_iterator end() const
Returns the end const iterator of the vector.
array_accessor1.h
jet::VectorN::operator+=
VectorN & operator+=(const E &v)
Computes this += v.
jet::VectorN::rsub
VectorScalarRSub< T, VectorN > rsub(const T &s) const
Computes (s, s, ... , s) - this.
jet::VectorN::isSimilar
bool isSimilar(const E &other, T epsilon=std::numeric_limits< T >::epsilon()) const
Returns true if other is similar to this vector.
jet::VectorN::VectorN
VectorN(VectorN &&other)
Move constructor.
jet::VectorN::div
VectorDiv< T, VectorN, E > div(const E &v) const
Computes this / v.
jet::VectorND
VectorN< double > VectorND
Double-type N-D vector.
Definition: vector_n.h:404
jet::VectorN::ContainerType
std::vector< T > ContainerType
Definition: vector_n.h:33
jet::VectorN::mul
VectorMul< T, VectorN, E > mul(const E &v) const
Computes this * v.
jet::VectorN::setZero
void setZero()
Sets all elements to zero.
jet::VectorN::forEachIndex
void forEachIndex(Callback func) const
Iterates the vector and invoke given func for each index.
jet::VectorN::operator/=
VectorN & operator/=(const T &s)
Computes this /= (s, s, ... , s)
jet::VectorN::append
void append(const T &val)
Adds an element.
jet::ArrayAccessor< T, 1 >
1-D array accessor class.
Definition: array_accessor1.h:27
jet::VectorN::imul
void imul(const E &v)
Computes this *= v.
jet::VectorN::length
T length() const
Returns the length of the vector.
jet::VectorN::castTo
VectorTypeCast< U, VectorN< T >, T > castTo() const
Returns a vector with different value type.