Skip to content

Commit 2baac79

Browse files
committed
STL <algorithm>
1 parent 862612c commit 2baac79

File tree

4 files changed

+40
-58
lines changed

4 files changed

+40
-58
lines changed

src/modm/math/geometry/vector_impl.hpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
template<typename T, std::size_t N>
1616
modm::Vector<T, N>::Vector(const T *ptData)
1717
{
18-
memcpy(coords, ptData, sizeof(T) * N);
18+
std::copy(coords, coords + N, ptData);
1919
}
2020

2121
template<typename T, std::size_t N>
2222
modm::Vector<T, N>::Vector(const modm::Matrix<T, N, 1> &rhs)
2323
{
24-
memcpy(coords, &rhs, sizeof(T) * N);
24+
std::copy(coords, coords + N, &rhs);
2525
}
2626

2727
// ----------------------------------------------------------------------------
@@ -37,7 +37,7 @@ template<typename T, std::size_t N>
3737
modm::Vector<T, N>&
3838
modm::Vector<T, N>::operator = (const modm::Matrix<T, N, 1> &rhs)
3939
{
40-
memcpy(coords, &rhs, sizeof(T) * N);
40+
std::copy(coords, coords + N, &rhs);
4141
return *this;
4242
}
4343

@@ -156,9 +156,9 @@ modm::Vector<T, N>
156156
modm::Vector<T, N>::operator + (const modm::Vector<T, N> &rhs) const
157157
{
158158
modm::Vector<T, N> pt;
159-
for (uint_fast8_t i = 0; i < N; ++i) {
159+
for (uint_fast8_t i = 0; i < N; ++i)
160160
pt[i] = coords[i] + rhs.coords[i];
161-
}
161+
162162
return pt;
163163
}
164164

@@ -167,9 +167,9 @@ modm::Vector<T, N>
167167
modm::Vector<T, N>::operator - (const modm::Vector<T, N> &rhs) const
168168
{
169169
modm::Vector<T, N> pt;
170-
for (uint_fast8_t i = 0; i < N; ++i) {
170+
for (uint_fast8_t i = 0; i < N; ++i)
171171
pt[i] = coords[i] - rhs.coords[i];
172-
}
172+
173173
return pt;
174174
}
175175

@@ -207,9 +207,8 @@ T
207207
modm::Vector<T, N>::operator * (const Vector &rhs) const
208208
{
209209
T v = 0;
210-
for (uint_fast8_t i = 0; i < N; ++i) {
210+
for (uint_fast8_t i = 0; i < N; ++i)
211211
v += (*this)[i]*rhs[i];
212-
}
213212

214213
return v;
215214
}
@@ -220,9 +219,9 @@ modm::Vector<T, N>
220219
modm::Vector<T, N>::operator * (const T &rhs) const
221220
{
222221
modm::Vector<T, N> pt;
223-
for (uint_fast8_t i = 0; i < N; ++i) {
222+
for (uint_fast8_t i = 0; i < N; ++i)
224223
pt[i] = coords[i] * rhs;
225-
}
224+
226225
return pt;
227226
}
228227

@@ -231,9 +230,9 @@ modm::Vector<T, N>
231230
modm::Vector<T, N>::operator / (const T &rhs) const
232231
{
233232
modm::Vector<T, N> pt;
234-
for (uint_fast8_t i = 0; i < N; ++i) {
233+
for (uint_fast8_t i = 0; i < N; ++i)
235234
pt[i] = coords[i] / rhs;
236-
}
235+
237236
return pt;
238237
}
239238

@@ -242,39 +241,39 @@ template<typename T, std::size_t N>
242241
modm::Vector<T, N>&
243242
modm::Vector<T, N>::operator += (const Vector &rhs)
244243
{
245-
for (uint_fast8_t i = 0; i < N; ++i) {
244+
for (uint_fast8_t i = 0; i < N; ++i)
246245
coords[i] += rhs.coords[i];
247-
}
246+
248247
return *this;
249248
}
250249

251250
template<typename T, std::size_t N>
252251
modm::Vector<T, N>&
253252
modm::Vector<T, N>::operator -= (const Vector &rhs)
254253
{
255-
for (uint_fast8_t i = 0; i < N; ++i) {
254+
for (uint_fast8_t i = 0; i < N; ++i)
256255
coords[i] -= rhs.coords[i];
257-
}
256+
258257
return *this;
259258
}
260259

261260
template<typename T, std::size_t N>
262261
modm::Vector<T, N>&
263262
modm::Vector<T, N>::operator *= (const T &rhs)
264263
{
265-
for (uint_fast8_t i = 0; i < N; ++i) {
264+
for (uint_fast8_t i = 0; i < N; ++i)
266265
coords[i] -= rhs;
267-
}
266+
268267
return *this;
269268
}
270269

271270
template<typename T, std::size_t N>
272271
modm::Vector<T, N>&
273272
modm::Vector<T, N>::operator /= (const T &rhs)
274273
{
275-
for (uint_fast8_t i = 0; i < N; ++i) {
274+
for (uint_fast8_t i = 0; i < N; ++i)
276275
coords[i] /= rhs;
277-
}
276+
278277
return *this;
279278
}
280279

@@ -283,9 +282,9 @@ template<typename T, std::size_t N>
283282
modm::Vector<T, N>&
284283
modm::Vector<T, N>::operator - ()
285284
{
286-
for (uint_fast8_t i = 0; i < N; ++i) {
285+
for (uint_fast8_t i = 0; i < N; ++i)
287286
coords[i] = -coords[i];
288-
}
287+
289288
return *this;
290289
}
291290

@@ -302,9 +301,8 @@ T
302301
modm::Vector<T, N>::getLengthSquared() const
303302
{
304303
T len2 = 0;
305-
for (uint_fast8_t i = 0; i < N; ++i) {
304+
for (uint_fast8_t i = 0; i < N; ++i)
306305
len2 += (*this)[i]*(*this)[i];
307-
}
308306

309307
return len2;
310308
}

src/modm/math/lu_decomposition_impl.hpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
#pragma once
1313
#include "lu_decomposition.hpp"
1414

15-
#ifndef MODM_LU_DECOMPOSITION_HPP
16-
#error "Don't include this file directly, use 'lu_decomposition.hpp' instead!"
17-
#endif
15+
#include <algorithm>
1816

1917
template<typename T, std::size_t SIZE>
2018
bool
@@ -119,12 +117,12 @@ template<typename T, std::size_t SIZE>
119117
void
120118
modm::LUDecomposition::RowOperation<T, SIZE>::swap(T *row1, T *row2)
121119
{
122-
T tmp[SIZE];
120+
// TODO prefer std::swap
123121

124-
// TODO use std::copy
125-
memcpy(tmp, row1, SIZE*sizeof(T));
126-
memcpy(row1, row2, SIZE*sizeof(T));
127-
memcpy(row2, tmp, SIZE*sizeof(T));
122+
T tmp[SIZE];
123+
std::copy(row1, row1 + SIZE, tmp);
124+
std::copy(row2, row2 + SIZE, row1);
125+
std::copy(tmp, tmp + SIZE, row2);
128126
}
129127

130128
//=============================================================================

src/modm/math/matrix.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#pragma once
1515

1616
#include <cmath>
17-
#include <string.h> // for memset() and memcmp()
17+
#include <algorithm>
1818
#include <stdint.h>
1919

2020
#include <modm/io/iostream.hpp>
@@ -79,7 +79,7 @@ namespace modm
7979
* \endcode
8080
*/
8181
constexpr Matrix(const T *data) {
82-
std::copy(data, data + getNumberOfElements(), element);
82+
std::copy(data, data + ElementCount, element);
8383
}
8484

8585
/**
@@ -123,9 +123,9 @@ namespace modm
123123
Matrix<T, ROWS, 1>
124124
getColumn(std::size_t index) const;
125125

126-
// TODO remove these?
127-
const T* ptr() const;
128-
T* ptr();
126+
// DEPRICATED element is public
127+
T* ptr() { return element; }
128+
const T* ptr() const { return element;}
129129

130130
Matrix operator - ();
131131
Matrix operator + (const Matrix &rhs) const;

src/modm/math/matrix_impl.hpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#pragma once
1313
#include "matrix.hpp"
1414

15+
#include <algorithm>
16+
1517
template<typename T, std::size_t ROWS, std::size_t COLUMNS>
1618
const modm::Matrix<T, ROWS, COLUMNS>&
1719
modm::Matrix<T, ROWS, COLUMNS>::identityMatrix()
@@ -44,7 +46,7 @@ modm::Matrix<T, ROWS, COLUMNS>::zeroMatrix()
4446

4547
if (!hasZeroMatrix)
4648
{
47-
std::fill(matrix.ptr(), matrix.ptr() + ElementCount, 0);
49+
std::fill(matrix.element, matrix.element + ElementCount, 0);
4850
hasZeroMatrix = true;
4951
}
5052

@@ -56,15 +58,14 @@ template<typename T, std::size_t ROWS, std::size_t COLUMNS>
5658
bool
5759
modm::Matrix<T, ROWS, COLUMNS>::operator == (const modm::Matrix<T, ROWS, COLUMNS> &m) const
5860
{
59-
return memcmp(element, m.element, getSize()) == 0;
61+
return std::equal(element, element + ElementCount, m.element);
6062
}
6163

62-
// ----------------------------------------------------------------------------
6364
template<typename T, std::size_t ROWS, std::size_t COLUMNS>
6465
bool
6566
modm::Matrix<T, ROWS, COLUMNS>::operator != (const modm::Matrix<T, ROWS, COLUMNS> &m) const
6667
{
67-
return memcmp(element, m.element, getSize()) != 0;
68+
return !std::equal(element, element + ElementCount, m.element);
6869
}
6970

7071
// ----------------------------------------------------------------------------
@@ -99,22 +100,7 @@ modm::Matrix<T, ROWS, COLUMNS>::operator [] (std::size_t row) const
99100

100101
// ----------------------------------------------------------------------------
101102
template<typename T, std::size_t ROWS, std::size_t COLUMNS>
102-
const T*
103-
modm::Matrix<T, ROWS, COLUMNS>::ptr() const
104-
{
105-
return element;
106-
}
107-
108-
template<typename T, std::size_t ROWS, std::size_t COLUMNS>
109-
T*
110-
modm::Matrix<T, ROWS, COLUMNS>::ptr()
111-
{
112-
return element;
113-
}
114-
115-
// ----------------------------------------------------------------------------
116-
template<typename T, std::size_t ROWS, std::size_t COLUMNS>
117-
modm::Matrix<T, ROWS, COLUMNS>
103+
constexpr modm::Matrix<T, ROWS, COLUMNS>
118104
modm::Matrix<T, ROWS, COLUMNS>::operator - ()
119105
{
120106
modm::Matrix<T, ROWS, COLUMNS> m;

0 commit comments

Comments
 (0)