Jet  v1.3.3
vector.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_H_
8 #define INCLUDE_JET_VECTOR_H_
9 
10 #include <jet/array_accessor1.h>
11 #include <jet/constants.h>
12 #include <jet/type_helpers.h>
13 #include <jet/vector_expression.h>
14 
15 #include <array>
16 #include <type_traits>
17 
18 namespace jet {
19 
29 
30 template <typename T, size_t N>
31 class Vector final : public VectorExpression<T, Vector<T, N>> {
32  public:
33  static_assert(N > 0,
34  "Size of static-sized vector should be greater than zero.");
35  static_assert(std::is_floating_point<T>::value,
36  "Vector only can be instantiated with floating point types");
37 
38  typedef std::array<T, N> ContainerType;
39 
41  Vector();
42 
44  template <typename... Params>
45  explicit Vector(Params... params);
46 
48  void set(const T& s);
49 
51  template <typename U>
52  Vector(const std::initializer_list<U>& lst);
53 
55  template <typename E>
57 
59  Vector(const Vector& other);
60 
61  // MARK: Basic setters
62 
64  template <typename U>
65  void set(const std::initializer_list<U>& lst);
66 
68  template <typename E>
69  void set(const VectorExpression<T, E>& other);
70 
72  void swap(Vector& other);
73 
75  void setZero();
76 
78  void normalize();
79 
80  // MARK: Basic getters
81 
83  constexpr size_t size() const;
84 
86  T* data();
87 
89  const T* const data() const;
90 
92  typename ContainerType::iterator begin();
93 
95  typename ContainerType::const_iterator begin() const;
96 
98  typename ContainerType::iterator end();
99 
101  typename ContainerType::const_iterator end() const;
102 
105 
108 
110  T at(size_t i) const;
111 
113  T& at(size_t i);
114 
116  T sum() const;
117 
119  T avg() const;
120 
122  T min() const;
123 
125  T max() const;
126 
128  T absmin() const;
129 
131  T absmax() const;
132 
134  size_t dominantAxis() const;
135 
137  size_t subminantAxis() const;
138 
141 
143  T length() const;
144 
146  T lengthSquared() const;
147 
149  template <typename E>
150  T distanceTo(const E& other) const;
151 
153  template <typename E>
154  T distanceSquaredTo(const E& other) const;
155 
157  template <typename U>
159 
161  template <typename E>
162  bool isEqual(const E& other) const;
163 
165  template <typename E>
166  bool isSimilar(const E& other,
167  T epsilon = std::numeric_limits<T>::epsilon()) const;
168 
169  // MARK: Binary operations: new instance = this (+) v
170 
172  template <typename E>
173  VectorAdd<T, Vector, E> add(const E& v) const;
174 
176  VectorScalarAdd<T, Vector> add(const T& s) const;
177 
179  template <typename E>
180  VectorSub<T, Vector, E> sub(const E& v) const;
181 
183  VectorScalarSub<T, Vector> sub(const T& s) const;
184 
186  template <typename E>
187  VectorMul<T, Vector, E> mul(const E& v) const;
188 
190  VectorScalarMul<T, Vector> mul(const T& s) const;
191 
193  template <typename E>
194  VectorDiv<T, Vector, E> div(const E& v) const;
195 
197  VectorScalarDiv<T, Vector> div(const T& s) const;
198 
200  template <typename E>
201  T dot(const E& v) const;
202 
203  // MARK: Binary operations: new instance = v (+) this
204 
207 
209  template <typename E>
210  VectorSub<T, Vector, E> rsub(const E& v) const;
211 
214 
216  template <typename E>
217  VectorDiv<T, Vector, E> rdiv(const E& v) const;
218 
219  // MARK: Augmented operations: this (+)= v
220 
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
250 
266  template <typename Callback>
267  void forEach(Callback func) const;
268 
284  template <typename Callback>
285  void forEachIndex(Callback func) const;
286 
288  const T& operator[](size_t i) const;
289 
291  T& operator[](size_t);
292 
294  template <typename U>
295  Vector& operator=(const std::initializer_list<U>& lst);
296 
298  template <typename E>
300 
302  Vector& operator+=(const T& s);
303 
305  template <typename E>
306  Vector& operator+=(const E& v);
307 
309  Vector& operator-=(const T& s);
310 
312  template <typename E>
313  Vector& operator-=(const E& v);
314 
316  Vector& operator*=(const T& s);
317 
319  template <typename E>
320  Vector& operator*=(const E& v);
321 
323  Vector& operator/=(const T& s);
324 
326  template <typename E>
327  Vector& operator/=(const E& v);
328 
330  template <typename E>
331  bool operator==(const E& v) const;
332 
334  template <typename E>
335  bool operator!=(const E& v) const;
336 
337  private:
338  ContainerType _elements;
339 
340  template <typename... Params>
341  void setAt(size_t i, T v, Params... params);
342  void setAt(size_t i, T v);
343 };
344 
346 template <typename T, size_t N>
347 struct ScalarType<Vector<T, N>> {
348  typedef T value;
349 };
350 
351 } // namespace jet
352 
353 #include "detail/vector-inl.h"
354 
355 #endif // INCLUDE_JET_VECTOR_H_
jet::Vector::sum
T sum() const
Returns the sum of all the elements.
jet::Vector::absmin
T absmin() const
Returns the absolute minimum element.
jet::Vector::Vector
Vector(Params... params)
Constructs vector instance with parameters.
jet::Vector::operator/=
Vector & operator/=(const T &s)
Computes this /= (s, s, ... , s)
jet::Vector::isSimilar
bool isSimilar(const E &other, T epsilon=std::numeric_limits< T >::epsilon()) const
Returns true if other is similar to this vector.
jet::Vector::imul
void imul(const T &s)
Computes this *= (s, s, ... , s).
jet::Vector::rdiv
VectorDiv< T, Vector, E > rdiv(const E &v) const
Computes v / this.
jet::Vector::ContainerType
std::array< T, N > ContainerType
Definition: vector.h:34
jet::VectorExpression
Base class for vector expression.
Definition: vector_expression.h:25
jet::Vector::distanceTo
T distanceTo(const E &other) const
Returns the distance to the other vector.
jet::Vector::set
void set(const VectorExpression< T, E > &other)
Sets vector with expression template.
jet::Vector::accessor
ArrayAccessor1< T > accessor()
Returns the array accessor.
jet::Vector::distanceSquaredTo
T distanceSquaredTo(const E &other) const
Returns the squared distance to the other vector.
jet::Vector::constAccessor
ConstArrayAccessor1< T > constAccessor() const
Returns the const array accessor.
jet::ConstArrayAccessor< T, 1 >
1-D read-only array accessor class.
Definition: array_accessor1.h:184
jet::Vector::dot
T dot(const E &v) const
Computes dot product.
jet::Vector::normalize
void normalize()
Normalizes this vector.
jet::Vector::length
T length() const
Returns the length of the vector.
jet::Vector::Vector
Vector()
Constructs a vector with zeros.
jet::Vector::idiv
void idiv(const E &v)
Computes this /= v.
jet::Vector::operator[]
T & operator[](size_t)
Returns the reference to the i -th element.
jet::Vector::operator+=
Vector & operator+=(const T &s)
Computes this += (s, s, ... , s)
jet::Vector::operator==
bool operator==(const E &v) const
Returns true if other is the same as this vector.
jet::VectorUnaryOp
Vector expression for unary operation.
Definition: vector_expression.h:47
jet::Vector::end
ContainerType::const_iterator end() const
Returns the end const iterator of the vector.
jet::Vector::isEqual
bool isEqual(const E &other) const
Returns true if other is the same as this vector.
jet::Vector::Vector
Vector(const VectorExpression< T, E > &other)
Constructs vector with expression template.
jet::Vector::operator*=
Vector & operator*=(const T &s)
Computes this *= (s, s, ... , s)
jet::Vector::at
T at(size_t i) const
Returns const reference to the i -th element of the vector.
jet::Vector::operator=
Vector & operator=(const std::initializer_list< U > &lst)
Set vector instance with initializer list.
jet::VectorScalarBinaryOp
Vector expression for matrix-scalar binary operation.
Definition: vector_expression.h:114
jet::Vector::isub
void isub(const T &s)
Computes this -= (s, s, ... , s).
jet::Vector::Vector
Vector(const Vector &other)
Copy constructor.
jet::Vector::begin
ContainerType::iterator begin()
Returns the begin iterator of the vector.
jet::Vector::operator[]
const T & operator[](size_t i) const
Returns the const reference to the i -th element.
jet::Vector::div
VectorDiv< T, Vector, E > div(const E &v) const
Computes this / v.
jet::Vector::sub
VectorScalarSub< T, Vector > sub(const T &s) const
Computes this - (s, s, ... , s).
jet::Vector::setZero
void setZero()
Sets all elements to zero.
jet::Vector
Generic statically-sized N-D vector class.
Definition: vector.h:31
jet::Vector::iadd
void iadd(const T &s)
Computes this += (s, s, ... , s).
jet::Vector::dominantAxis
size_t dominantAxis() const
Returns the index of the dominant axis.
jet::Vector::begin
ContainerType::const_iterator begin() const
Returns the begin const iterator of the vector.
jet::Vector::mul
VectorScalarMul< T, Vector > mul(const T &s) const
Computes this * (s, s, ... , s).
jet::Vector::operator-=
Vector & operator-=(const E &v)
Computes this -= v.
jet::VectorBinaryOp
Vector expression for binary operation.
Definition: vector_expression.h:84
jet::Vector::size
constexpr size_t size() const
Returns the size of the vector.
jet
Definition: advection_solver2.h:18
vector_expression.h
jet::Vector::lengthSquared
T lengthSquared() const
Returns the squared length of the vector.
jet::Vector::rsub
VectorSub< T, Vector, E > rsub(const E &v) const
Computes v - this.
jet::Vector::data
const T *const data() const
Returns the const raw pointer to the vector data.
jet::Vector::mul
VectorMul< T, Vector, E > mul(const E &v) const
Computes this * v.
jet::Vector::avg
T avg() const
Returns the average of all the elements.
jet::Vector::at
T & at(size_t i)
Returns reference to the i -th element of the vector.
jet::Vector::add
VectorScalarAdd< T, Vector > add(const T &s) const
Computes this + (s, s, ... , s).
jet::Vector::operator-=
Vector & operator-=(const T &s)
Computes this -= (s, s, ... , s)
jet::Vector::iadd
void iadd(const E &v)
Computes this += v.
jet::Vector::idiv
void idiv(const T &s)
Computes this /= (s, s, ... , s).
jet::Vector::absmax
T absmax() const
Returns the absolute maximum element.
jet::Vector::normalized
VectorScalarDiv< T, Vector > normalized() const
Returns normalized vector.
jet::Vector::set
void set(const std::initializer_list< U > &lst)
Set vector instance with initializer list.
jet::Vector::rsub
VectorScalarRSub< T, Vector > rsub(const T &s) const
Computes (s, s, ... , s) - this.
jet::Vector::castTo
VectorTypeCast< U, Vector< T, N >, T > castTo() const
Returns a vector with different value type.
jet::Vector::min
T min() const
Returns the minimum element.
jet::Vector::set
void set(const T &s)
Sets all elements to s.
constants.h
jet::Vector::imul
void imul(const E &v)
Computes this *= v.
jet::Vector::operator*=
Vector & operator*=(const E &v)
Computes this *= v.
jet::Vector::subminantAxis
size_t subminantAxis() const
Returns the index of the subminant axis.
jet::Vector::operator/=
Vector & operator/=(const E &v)
Computes this /= v.
array_accessor1.h
jet::Vector::add
VectorAdd< T, Vector, E > add(const E &v) const
Computes this + v.
jet::Vector::Vector
Vector(const std::initializer_list< U > &lst)
Constructs vector instance with initializer list.
jet::Vector::operator=
Vector & operator=(const VectorExpression< T, E > &other)
Sets vector with expression template.
jet::ScalarType< Vector< T, N > >::value
T value
Definition: vector.h:348
jet::Vector::isub
void isub(const E &v)
Computes this -= v.
type_helpers.h
jet::Vector::operator!=
bool operator!=(const E &v) const
Returns true if other is the not same as this vector.
jet::Vector::div
VectorScalarDiv< T, Vector > div(const T &s) const
Computes this / (s, s, ... , s).
jet::Vector::end
ContainerType::iterator end()
Returns the end iterator of the vector.
jet::Vector::forEachIndex
void forEachIndex(Callback func) const
Iterates the vector and invoke given func for each index.
jet::Vector::sub
VectorSub< T, Vector, E > sub(const E &v) const
Computes this - v.
jet::Vector::swap
void swap(Vector &other)
Swaps the content of the vector with other vector.
jet::Vector::forEach
void forEach(Callback func) const
Iterates the vector and invoke given func for each element.
jet::ArrayAccessor< T, 1 >
1-D array accessor class.
Definition: array_accessor1.h:27
jet::Vector::data
T * data()
Returns the raw pointer to the vector data.
jet::Vector::rdiv
VectorScalarRDiv< T, Vector > rdiv(const T &s) const
Computes (s, s, ... , s) / this.
jet::Vector::operator+=
Vector & operator+=(const E &v)
Computes this += v.
jet::ScalarType
Returns the type of the value itself.
Definition: type_helpers.h:14
jet::Vector::max
T max() const
Returns the maximum element.