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
10namespace iolink
11{
12
16template <typename T>
17class VectorX final
18{
19public:
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
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
178private:
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
191template <typename T>
192inline 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
203extern template class IOLINK_API_IMPORT VectorX<uint8_t>;
204extern template class IOLINK_API_IMPORT VectorX<int8_t>;
205extern template class IOLINK_API_IMPORT VectorX<uint16_t>;
206extern template class IOLINK_API_IMPORT VectorX<int16_t>;
207extern template class IOLINK_API_IMPORT VectorX<uint32_t>;
208extern template class IOLINK_API_IMPORT VectorX<int32_t>;
209extern template class IOLINK_API_IMPORT VectorX<uint64_t>;
210extern template class IOLINK_API_IMPORT VectorX<int64_t>;
211extern template class IOLINK_API_IMPORT VectorX<float>;
212extern template class IOLINK_API_IMPORT VectorX<double>;
213
214//==================== //
215// Aliases declaration //
216//==================== //
217
228
229} // end namespace iolink