| IOLink 1.11.0

Loading...
Searching...
No Matches
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
100 inline ValueType* data() { return m_data; }
101
105 inline const ValueType* data() const { return m_data; }
106
107 bool operator==(const VectorX& other) const;
108 bool operator!=(const VectorX& other) const;
109
115 double squaredLength() const;
116
122 double length() const;
123
127 void normalize();
128
129 std::string toString() const;
130
131 // ==================== //
132 // Arithmetic operators //
133 // ==================== //
134
135 VectorX operator-() const;
136
137 VectorX& operator+=(const VectorX& v);
138 VectorX& operator-=(const VectorX& v);
139
140 inline VectorX operator+(const VectorX& v) const { return VectorX(*this) += v; }
141 inline VectorX operator-(const VectorX& v) const { return VectorX(*this) -= v; }
142
143 VectorX& operator*=(ValueType value);
144 VectorX& operator/=(ValueType value);
145
146 inline VectorX operator*(ValueType s) const { return VectorX(*this) *= s; }
147 inline VectorX operator/(ValueType s) const { return VectorX(*this) /= s; }
148
149 friend inline VectorX operator*(ValueType s, VectorX v) { return v *= s; }
150
154 ValueType dot(const VectorX& v) const;
155
161 VectorX cross(const VectorX& v) const;
162
163 // ==================== //
164 // GLSL style operators //
165 // ==================== //
166
167 VectorX& operator*=(const VectorX& v);
168 VectorX& operator/=(const VectorX& v);
169
170 inline VectorX operator*(const VectorX& v) const { return VectorX(*this) *= v; }
171 inline VectorX operator/(const VectorX& v) const { return VectorX(*this) /= v; }
172
173 bool operator<(const VectorX& other) const;
174 bool operator<=(const VectorX& other) const;
175 bool operator>(const VectorX& other) const;
176 bool operator>=(const VectorX& other) const;
177
178 private:
179 size_t m_size;
180
181 // Pointer on values. If m_size < m_array.size(), it point to array. Else,
182 // it point to memory allocated on heap with new[].
183 // In case m_data != m_array.data(), it should be deallocated with delete[]
184 ValueType* m_data;
185
186 // 4 sized array storing values if VectorX size is < 4.
187 // This avoid allocating values on heap and greatly optimise VectorX in most common case.
188 std::array<ValueType, 4> m_array;
189};
190
191 template <typename T>
192 inline std::ostream&
193operator<<(std::ostream& os, const VectorX<T>& v)
194{
195 os << v.toString();
196 return os;
197}
198
199// ===================== //
200// Template declarations //
201// ===================== //
202
203 extern template class IOLINK_API_IMPORT VectorX<uint8_t>;
204 extern template class IOLINK_API_IMPORT VectorX<int8_t>;
205 extern template class IOLINK_API_IMPORT VectorX<uint16_t>;
206 extern template class IOLINK_API_IMPORT VectorX<int16_t>;
207 extern template class IOLINK_API_IMPORT VectorX<uint32_t>;
208 extern template class IOLINK_API_IMPORT VectorX<int32_t>;
209 extern template class IOLINK_API_IMPORT VectorX<uint64_t>;
210 extern template class IOLINK_API_IMPORT VectorX<int64_t>;
211 extern template class IOLINK_API_IMPORT VectorX<float>;
212 extern template class IOLINK_API_IMPORT VectorX<double>;
213
214//==================== //
215// Aliases declaration //
216//==================== //
217
218 using VectorXu8 = VectorX<uint8_t>;
219 using VectorXu16 = VectorX<uint16_t>;
220 using VectorXu32 = VectorX<uint32_t>;
221 using VectorXu64 = VectorX<uint64_t>;
222 using VectorXi8 = VectorX<int8_t>;
223 using VectorXi16 = VectorX<int16_t>;
224 using VectorXi32 = VectorX<int32_t>;
225 using VectorXi64 = VectorX<int64_t>;
226 using VectorXf = VectorX<float>;
227 using VectorXd = VectorX<double>;
228
229} // end namespace iolink
iolink::VectorX
A dynamically sized arithmetic vector.
Definition VectorX.h:18
iolink::VectorX::VectorX
VectorX(std::initializer_list< ValueType > init)
Creates a vector from an initialization list.
iolink::VectorX::createUniform
static VectorX createUniform(size_t size, ValueType value)
Creates a vector filled with one value.
iolink::VectorX::setAt
void setAt(size_t index, ValueType value)
Set value as given index.
Definition VectorX.h:87
iolink::VectorX::dot
ValueType dot(const VectorX &v) const
Dot product.
iolink::VectorX::normalize
void normalize()
Normalize the vector.
iolink::VectorX::VectorX
VectorX(size_t size)
Creates an unitialized vector.
iolink::VectorX::squaredLength
double squaredLength() const
Returns the vector's norm, squared.
iolink::VectorX::length
double length() const
Returns the vector's norm.
iolink::VectorX::data
const ValueType * data() const
Direct const access to the vector's data.
Definition VectorX.h:105
iolink::VectorX::size
size_t size() const
Returns the size of the vector, which also correspond to vector number of dimension.
Definition VectorX.h:77
iolink::VectorX::VectorX
VectorX(const VectorX< U > &other)
Build a vector from a vector storing another type.
Definition VectorX.h:57
iolink::VectorX::at
ValueType at(size_t index) const
Access vector's value at given index.
Definition VectorX.h:82
iolink::VectorX::VectorX
VectorX()
Creates an empty vector.
iolink::VectorX::data
ValueType * data()
Direct access to the vector's data.
Definition VectorX.h:100
iolink::VectorX::cross
VectorX cross(const VectorX &v) const
Cross product of two vectors.
iolink
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8