stdvector.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 #include <Eigen/StdVector>
12 #include <Eigen/Geometry>
13 
14 template<typename MatrixType>
16 {
17  Index rows = m.rows();
18  Index cols = m.cols();
19  MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
20  std::vector<MatrixType,Eigen::aligned_allocator<MatrixType> > v(10, MatrixType::Zero(rows,cols)), w(20, y);
21  v[5] = x;
22  w[6] = v[5];
23  VERIFY_IS_APPROX(w[6], v[5]);
24  v = w;
25  for(int i = 0; i < 20; i++)
26  {
27  VERIFY_IS_APPROX(w[i], v[i]);
28  }
29 
30  v.resize(21);
31  v[20] = x;
32  VERIFY_IS_APPROX(v[20], x);
33  v.resize(22,y);
34  VERIFY_IS_APPROX(v[21], y);
35  v.push_back(x);
36  VERIFY_IS_APPROX(v[22], x);
37  VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(MatrixType));
38 
39  // do a lot of push_back such that the vector gets internally resized
40  // (with memory reallocation)
41  MatrixType* ref = &w[0];
42  for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
43  v.push_back(w[i%w.size()]);
44  for(unsigned int i=23; i<v.size(); ++i)
45  {
46  VERIFY(v[i]==w[(i-23)%w.size()]);
47  }
48 }
49 
50 template<typename TransformType>
51 void check_stdvector_transform(const TransformType&)
52 {
53  typedef typename TransformType::MatrixType MatrixType;
54  TransformType x(MatrixType::Random()), y(MatrixType::Random());
55  std::vector<TransformType,Eigen::aligned_allocator<TransformType> > v(10), w(20, y);
56  v[5] = x;
57  w[6] = v[5];
58  VERIFY_IS_APPROX(w[6], v[5]);
59  v = w;
60  for(int i = 0; i < 20; i++)
61  {
62  VERIFY_IS_APPROX(w[i], v[i]);
63  }
64 
65  v.resize(21);
66  v[20] = x;
67  VERIFY_IS_APPROX(v[20], x);
68  v.resize(22,y);
69  VERIFY_IS_APPROX(v[21], y);
70  v.push_back(x);
71  VERIFY_IS_APPROX(v[22], x);
72  VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(TransformType));
73 
74  // do a lot of push_back such that the vector gets internally resized
75  // (with memory reallocation)
76  TransformType* ref = &w[0];
77  for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
78  v.push_back(w[i%w.size()]);
79  for(unsigned int i=23; i<v.size(); ++i)
80  {
81  VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
82  }
83 }
84 
85 template<typename QuaternionType>
86 void check_stdvector_quaternion(const QuaternionType&)
87 {
88  typedef typename QuaternionType::Coefficients Coefficients;
89  QuaternionType x(Coefficients::Random()), y(Coefficients::Random()), qi=QuaternionType::Identity();
90  std::vector<QuaternionType,Eigen::aligned_allocator<QuaternionType> > v(10,qi), w(20, y);
91  v[5] = x;
92  w[6] = v[5];
93  VERIFY_IS_APPROX(w[6], v[5]);
94  v = w;
95  for(int i = 0; i < 20; i++)
96  {
97  VERIFY_IS_APPROX(w[i], v[i]);
98  }
99 
100  v.resize(21);
101  v[20] = x;
102  VERIFY_IS_APPROX(v[20], x);
103  v.resize(22,y);
104  VERIFY_IS_APPROX(v[21], y);
105  v.push_back(x);
106  VERIFY_IS_APPROX(v[22], x);
107  VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(QuaternionType));
108 
109  // do a lot of push_back such that the vector gets internally resized
110  // (with memory reallocation)
111  QuaternionType* ref = &w[0];
112  for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
113  v.push_back(w[i%w.size()]);
114  for(unsigned int i=23; i<v.size(); ++i)
115  {
116  VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
117  }
118 }
119 
120 // the code below triggered an invalid warning with gcc >= 7
121 // eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object size 9223372036854775807
122 // This has been reported to gcc there: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
124 {
125  typedef Eigen::Vector3f T;
126  std::vector<T, Eigen::aligned_allocator<T> > v;
127  v.push_back(T());
128 }
129 
131 {
132  // some non vectorizable fixed sizes
136 
137  // some vectorizable fixed sizes
142 
143  // some dynamic sizes
144  CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
145  CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
146  CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
147  CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
148 
149  // some Transform
153  //CALL_SUBTEST(heck_stdvector_transform(Projective4d()));
154 
155  // some Quaternion
158 }
w
RowVector3d w
Definition: Matrix_resize_int.cpp:3
Eigen::Projective3f
Transform< float, 3, Projective > Projective3f
Definition: Transform.h:724
Eigen::internal::UIntPtr
std::size_t UIntPtr
Definition: Meta.h:92
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
std_vector_gcc_warning
void std_vector_gcc_warning()
Definition: stdvector.cpp:123
check_stdvector_matrix
void check_stdvector_matrix(const MatrixType &m)
Definition: stdvector.cpp:15
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
check_stdvector_transform
void check_stdvector_transform(const TransformType &)
Definition: stdvector.cpp:51
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(stdvector)
Definition: stdvector.cpp:130
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
Eigen::Projective3d
Transform< double, 3, Projective > Projective3d
Definition: Transform.h:728
Eigen::Quaternionf
Quaternion< float > Quaternionf
Definition: 3rdparty/Eigen/Eigen/src/Geometry/Quaternion.h:363
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::Quaterniond
Quaternion< double > Quaterniond
Definition: 3rdparty/Eigen/Eigen/src/Geometry/Quaternion.h:366
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
y
Scalar * y
Definition: level1_cplx_impl.h:124
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
main.h
ref
Reference counting helper.
Definition: object.h:67
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Eigen::Projective2f
Transform< float, 2, Projective > Projective2f
Definition: Transform.h:722
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
check_stdvector_quaternion
void check_stdvector_quaternion(const QuaternionType &)
Definition: stdvector.cpp:86
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:05:00