IOLink  IOL_v1.1.0_release
VectorX.h
1 #pragma once
2 
3 #include <iolink/IOLinkAPI.h>
4 
5 #include <array>
6 #include <cstdint>
7 #include <initializer_list>
8 #include <iostream>
9 
10 namespace iolink
11 {
12 
16 template <typename T>
17 class VectorX final
18 {
19 public:
20  using ValueType = T;
21 
22  using Iterator = ValueType*;
23  using ConstIterator = const ValueType*;
24 
31  static VectorX createUniform(size_t size, ValueType value);
32 
36  VectorX();
37 
43  explicit VectorX(size_t size);
44 
48  VectorX(std::initializer_list<ValueType> init);
49 
56  template <typename U>
57  explicit VectorX(const VectorX<U>& other)
58  : VectorX(other.size())
59  {
60  for (size_t i = 0; i < m_size; ++i)
61  {
62  m_data[i] = static_cast<ValueType>(other[i]);
63  }
64  }
65 
66  VectorX(const VectorX& other);
67  VectorX& operator=(const VectorX& other);
68 
69  VectorX(VectorX&& other) noexcept;
70  VectorX& operator=(VectorX&& other) noexcept;
71 
72  ~VectorX();
73 
77  inline size_t size() const { return m_size; }
78 
82  inline ValueType at(size_t index) const { return m_data[index]; }
83 
87  inline void setAt(size_t index, ValueType value) { m_data[index] = value; }
88 
89  inline ValueType& operator[](size_t index) { return m_data[index]; }
90  inline ValueType operator[](size_t index) const { return m_data[index]; }
91 
92  inline Iterator begin() noexcept { return m_data; }
93  inline ConstIterator begin() const noexcept { return m_data; }
94  inline Iterator end() noexcept { return static_cast<Iterator>(m_data + m_size); }
95  inline ConstIterator end() const noexcept { return static_cast<ConstIterator>(m_data + m_size); }
96 
97  bool operator==(const VectorX& other) const;
98  bool operator!=(const VectorX& other) const;
99 
105  double squaredLength() const;
106 
112  double length() const;
113 
117  void normalize();
118 
119  std::string toString() const;
120 
121  // ==================== //
122  // Arithmetic operators //
123  // ==================== //
124 
125  VectorX operator-() const;
126 
127  VectorX& operator+=(const VectorX& v);
128  VectorX& operator-=(const VectorX& v);
129 
130  inline VectorX operator+(const VectorX& v) const { return VectorX(*this) += v; }
131  inline VectorX operator-(const VectorX& v) const { return VectorX(*this) -= v; }
132 
133  VectorX& operator*=(ValueType value);
134  VectorX& operator/=(ValueType value);
135 
136  inline VectorX operator*(ValueType s) const { return VectorX(*this) *= s; }
137  inline VectorX operator/(ValueType s) const { return VectorX(*this) /= s; }
138 
139  friend inline VectorX operator*(ValueType s, VectorX v) { return v *= s; }
140 
144  ValueType dot(const VectorX& v) const;
145 
151  VectorX cross(const VectorX& v) const;
152 
153  // ==================== //
154  // GLSL style operators //
155  // ==================== //
156 
157  VectorX& operator*=(const VectorX& v);
158  VectorX& operator/=(const VectorX& v);
159 
160  inline VectorX operator*(const VectorX& v) const { return VectorX(*this) *= v; }
161  inline VectorX operator/(const VectorX& v) const { return VectorX(*this) /= v; }
162 
163  bool operator<(const VectorX& other) const;
164  bool operator<=(const VectorX& other) const;
165  bool operator>(const VectorX& other) const;
166  bool operator>=(const VectorX& other) const;
167 
168 private:
169  size_t m_size;
170 
171  // Pointer on values. If m_size < m_array.size(), it point to array. Else,
172  // it point to memory allocated on heap with new[].
173  // In case m_data != m_array.data(), it should be deallocated with delete[]
174  ValueType* m_data;
175 
176  // 4 sized array storing values if VectorX size is < 4.
177  // This avoid allocating values on heap and greatly optimise VectorX in most common case.
178  std::array<ValueType, 4> m_array;
179 };
180 
181 template <typename T>
182 inline std::ostream&
183 operator<<(std::ostream& os, const VectorX<T>& v)
184 {
185  os << v.toString();
186  return os;
187 }
188 
189 // ===================== //
190 // Template declarations //
191 // ===================== //
192 
193 extern template class IOLINK_API_IMPORT VectorX<uint8_t>;
194 extern template class IOLINK_API_IMPORT VectorX<int8_t>;
195 extern template class IOLINK_API_IMPORT VectorX<uint16_t>;
196 extern template class IOLINK_API_IMPORT VectorX<int16_t>;
197 extern template class IOLINK_API_IMPORT VectorX<uint32_t>;
198 extern template class IOLINK_API_IMPORT VectorX<int32_t>;
199 extern template class IOLINK_API_IMPORT VectorX<uint64_t>;
200 extern template class IOLINK_API_IMPORT VectorX<int64_t>;
201 extern template class IOLINK_API_IMPORT VectorX<float>;
202 extern template class IOLINK_API_IMPORT VectorX<double>;
203 
204 //==================== //
205 // Aliases declaration //
206 //==================== //
207 
218 
219 } // end namespace iolink