Debian Codegraph @

3rdparty/xspublic/xscommon/xsens_generic_matrix.h

raw symbols SCIP
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSENS_GENERIC_MATRIX_H
34 #define XSENS_GENERIC_MATRIX_H
35
36 #include "xsens_math_throw.h"
37
38 #ifndef XSENS_MATH_FIRMWARE
39 # ifndef XSENS_EXCEPTION_H
40 # include <xstypes/xsexception.h>
41 # endif
42 #endif
43
44 #ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS
45 # ifdef _MSC_VER
46 # define XSENS_GENERIC_MATRIX_THROW throw(...)
47 # else
48 # define XSENS_GENERIC_MATRIX_THROW
49 # endif
50 #else
51 # define XSENS_GENERIC_MATRIX_THROW
52 #endif
53
54 #ifndef XSENS_THROW_BAD_ALLOC
55 # ifdef XSENS_NO_EXCEPTIONS
56 # include <assert.h>
57 # define XSENS_THROW_BAD_ALLOC XM_THROW("Bad alloc")
58 # else
59 # define XSENS_THROW_BAD_ALLOC throw std::bad_alloc()
60 # endif
61 #endif
62
63 #ifndef _PSTDINT_H_INCLUDED
64 #include <xstypes/pstdint.h>
65 #endif
66
67 #include <stdlib.h>
68 #include <string.h>
69
70 #ifdef __cplusplus
71 namespace xsens
72 {
73 namespace {
74 /*! \brief Swap the values of two objects of a basic type T through assignment */
75 template <typename T>
76 inline void swapb(T& a, T& b)
77 {
78 T temp = a;
79 a = b;
80 b = temp;
81 }
82 }
83
84 /*! \brief Generic matrix class
85
86 This class can store items of the given type.
87 Items in the list can be accessed through the [] operator or the get() function.
88
89 Do NOT use any item type that requires a constructor to work correctly. Pointers to these
90 objects can work though.
91 */
92 template <typename T>
93 class GenericMatrix
94 {
95 protected:
96 T* m_data; //!< The array containing the items.
97 uint32_t m_rows; //!< The number of rows currently in the matrix.
98 uint32_t m_cols; //!< The number of columns currently in the matrix.
99 uint32_t m_allocSize; //!< The number of elemens currently allocated for the matrix.
100 int m_flags; //!< Flags for data management.
101
102 public:
103 //! Standard constructor, creates a 1x1 matrix.
104 GenericMatrix();
105 //! Construct a matrix with a capacity of at least the given size.
106 explicit GenericMatrix(const uint32_t newRows, const uint32_t newCols, bool zeroValues = true);
107 //! Construct a matrix as a direct copy of another one
108 GenericMatrix(const GenericMatrix<T>& src);
109 //! Construct a matrix as a copy of a raw data buffer
110 explicit GenericMatrix(const uint32_t newRows, const uint32_t newCols, const T* src);
111 //! Construct a matrix using the given buffer, useful when the matrix should be stack allocated.
112 explicit GenericMatrix(const uint32_t newRows, const uint32_t newCols, T* buffer, XsDataFlags flags);
113 //! Destroy the matrix. This does NOT automatically delete items IN the list.
114 virtual ~GenericMatrix();
115
116 //! Sets the size of the matrix to the new dimensions. The contents should be considered garbage. If zeroValues is set to true a memset 0 is done on the newly created items
117 void setSize(uint32_t newRows, uint32_t newCols, bool zeroValues = true);
118 //! Retrieves the item at the given row,col. An index beyond the end causes an exception.
119 T& get(const uint32_t row, const uint32_t cols) const XSENS_GENERIC_MATRIX_THROW;
120 //! Retrieves a pointer to the first item of the given row. An index beyond the end causes an exception.
121 T* operator [] (const uint32_t row) const XSENS_GENERIC_MATRIX_THROW;
122 //! Returns the number of rows currently in the matrix.
123 uint32_t rows(void) const { return m_rows; }
124 //! Returns the number of cols currently in the matrix.
125 uint32_t cols(void) const { return m_cols; }
126 //! Returns the number of items currently in the matrix.
127 uint32_t count(void) const { return m_rows*m_cols; }
128 //! Returns the number of items currently in the matrix.
129 uint32_t size(void) const { return m_rows*m_cols; }
130 //! Copy a matrix into this one
131 GenericMatrix<T>& operator = (const GenericMatrix<T>& src);
132 //! Swap a matrix with this one
133 void swap(GenericMatrix<T>& other);
134 //! Calls memset 0 on the contents of the matrix
135 void zeroValues();
136 };
137
138 template <typename T>
139 GenericMatrix<T>::GenericMatrix()
140 : m_rows(1)
141 , m_cols(1)
142 , m_flags(XSDF_Managed)
143 {
144 m_data = (T*)malloc(m_rows*m_cols * sizeof(T));
145
146 if (!m_data)
147 XSENS_THROW_BAD_ALLOC;
148
149 m_allocSize = m_rows*m_cols;
150 }
151
152 template <typename T>
153 GenericMatrix<T>::GenericMatrix(uint32_t newRows, uint32_t newCols, bool zeroVals)
154 : m_rows(newRows)
155 , m_cols(newCols)
156 , m_flags(XSDF_Managed)
157 {
158 m_data = (T*)malloc(sizeof(T) * (size_t)m_rows*m_cols);
159
160 if (!m_data)
161 XSENS_THROW_BAD_ALLOC;
162
163 m_allocSize = m_rows*m_cols;
164
165 if (zeroVals)
166 zeroValues();
167 }
168
169 template <typename T>
170 GenericMatrix<T>::GenericMatrix(const GenericMatrix<T>& src)
171 : m_rows(src.m_rows)
172 , m_cols(src.m_cols)
173 , m_flags(XSDF_Managed)
174 {
175 m_data = (T*)malloc(sizeof(T) * (size_t)m_rows*m_cols);
176
177 if (!m_data)
178 XSENS_THROW_BAD_ALLOC;
179
180 m_allocSize = m_rows*m_cols;
181
182 memcpy(m_data, src.m_data, sizeof(T) * (size_t)m_rows*m_cols);
183 }
184
185 template <typename T>
186 GenericMatrix<T>::GenericMatrix(const uint32_t newRows, uint32_t newCols, const T* src)
187 : m_rows(newRows)
188 , m_cols(newCols)
189 , m_flags(XSDF_Managed)
190 {
191 m_data = (T*)malloc(sizeof(T) * (size_t)m_rows*m_cols);
192
193 if (!m_data)
194 XSENS_THROW_BAD_ALLOC;
195
196 m_allocSize = m_rows*m_cols;
197
198 memcpy(m_data, src, sizeof(T) * (size_t)m_rows*m_cols);
199 }
200
201 template <typename T>
202 GenericMatrix<T>::GenericMatrix(const uint32_t newRows, const uint32_t newCols, T* const buffer, XsDataFlags flags)
203 : m_rows(newRows)
204 , m_cols(newCols)
205 , m_flags(flags)
206 {
207 if (m_flags & XSDF_FixedSize)
208 {
209 m_data = buffer;
210
211 if (size() == 0)
212 m_flags |= XSDF_Empty;
213 }
214
215 if (m_flags & XSDF_Managed)
216 m_data = (T*)malloc(sizeof(T) * (size_t)m_rows*m_cols);
217
218 if (!m_data)
219 XSENS_THROW_BAD_ALLOC;
220
221 m_allocSize = m_rows*m_cols;
222
223 if (m_flags & XSDF_Managed)
224 memcpy(m_data, buffer, sizeof(T) * (size_t)m_rows*m_cols);
225 }
226
227 template <typename T>
228 GenericMatrix<T>::~GenericMatrix()
229 {
230 if (m_flags & XSDF_Managed)
231 free(m_data);
232
233 if (!(m_flags & XSDF_FixedSize))
234 m_data = NULL;
235 else
236 m_flags |= XSDF_Empty;
237 }
238
239 template <typename T>
240 void GenericMatrix<T>::setSize(uint32_t newRows, uint32_t newCols, bool zeroVals)
241 {
242 if (newRows == m_rows && newCols == m_cols)
243 {
244 if (zeroVals)
245 zeroValues();
246
247 return;
248 }
249
250 #ifdef XSENS_MATH_RESTRICT_RESIZE
251 if (m_data && !(m_flags & XSDF_Managed))
252 XM_THROW("resizing of this object is not allowed");
253 #endif
254
255 const uint32_t newSize = newRows * newCols;
256
257 if (m_flags & XSDF_FixedSize)
258 {
259 if (newSize == 0)
260 {
261 m_flags |= XSDF_Empty;
262 m_rows = newRows;
263 m_cols = newCols;
264 return;
265 }
266
267 assert(newSize <= size());
268 m_flags &= ~XSDF_Empty;
269 }
270
271 if (newSize > m_allocSize || newSize == 0)
272 {
273 if (m_flags & XSDF_Managed)
274 {
275 free(m_data);
276 m_data = 0;
277 m_allocSize = 0;
278 }
279
280 if (newSize > 0)
281 {
282 m_data = (T*)malloc(sizeof(T) * (size_t)newRows*newCols);
283 m_flags = XSDF_Managed;
284 m_allocSize = newRows*newCols;
285 }
286 }
287
288 m_rows = newRows;
289 m_cols = newCols;
290
291 if (!m_data)
292 {
293 XSENS_THROW_BAD_ALLOC;
294 }
295
296 if (zeroVals)
297 zeroValues();
298 }
299
300 template <typename T>
301 T& GenericMatrix<T>::get(uint32_t row, uint32_t col) const XSENS_GENERIC_MATRIX_THROW
302 {
303 #ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS
304 if (row >= m_rows || col >= m_cols)
305 XM_THROW("index out of bounds");
306 #endif
307 return m_data[row*m_cols + col];
308 }
309
310 template <typename T>
311 T* GenericMatrix<T>::operator [] (const uint32_t row) const XSENS_GENERIC_MATRIX_THROW
312 {
313 #ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS
314 if (row >= m_rows)
315 XM_THROW("index out of bounds");
316 #endif
317 return &m_data[row*m_cols];
318 }
319
320 template <typename T>
321 void GenericMatrix<T>::zeroValues()
322 {
323 memset(static_cast<void*>(m_data), 0, sizeof(T) * (size_t)m_rows*m_cols);
324 }
325
326 template <typename T>
327 void GenericMatrix<T>::swap(GenericMatrix<T>& other)
328 {
329 swapb(m_rows, other.m_rows);
330 swapb(m_cols, other.m_cols);
331 swapb(m_allocSize, other.m_allocSize);
332 swapb(m_flags, other.m_flags);
333 swapb(m_data, other.m_data);
334 }
335
336 template <typename T>
337 GenericMatrix<T>& GenericMatrix<T>::operator = (const GenericMatrix<T>& src)
338 {
339 if (this != &src)
340 {
341 GenericMatrix temp(src);
342 swap(temp);
343 }
344 return *this;
345 }
346
347 }
348
349 #endif
350 #endif