From 82df5b4a245faf570f58a9f16d8f36f605fba2a9 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Sat, 29 Aug 2009 12:13:52 -0400 Subject: [PATCH] backport the new StdVector as NewStdVector make StdVector be a wrapper around it if EIGEN_USE_NEW_STDVECTOR is defined otherwise StdVector doesn't change ---> compatibility is preserved backport unit-test --- Eigen/NewStdVector | 168 ++++++++++++++++++++++++++++++++++++++++++ Eigen/StdVector | 6 ++ test/CMakeLists.txt | 1 + test/newstdvector.cpp | 164 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 339 insertions(+) create mode 100644 Eigen/NewStdVector create mode 100644 test/newstdvector.cpp diff --git a/Eigen/NewStdVector b/Eigen/NewStdVector new file mode 100644 index 000000000..f37de5ff6 --- /dev/null +++ b/Eigen/NewStdVector @@ -0,0 +1,168 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2009 Gael Guennebaud +// Copyright (C) 2009 Hauke Heibel +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#ifndef EIGEN_STDVECTOR_MODULE_H +#define EIGEN_STDVECTOR_MODULE_H + +#include "Core" +#include + +namespace Eigen { + +// This one is needed to prevent reimplementing the whole std::vector. +template +class aligned_allocator_indirection : public aligned_allocator +{ +public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T* pointer; + typedef const T* const_pointer; + typedef T& reference; + typedef const T& const_reference; + typedef T value_type; + + template + struct rebind + { + typedef aligned_allocator_indirection other; + }; + + aligned_allocator_indirection() throw() {} + aligned_allocator_indirection(const aligned_allocator_indirection& ) throw() : aligned_allocator() {} + aligned_allocator_indirection(const aligned_allocator& ) throw() {} + template + aligned_allocator_indirection(const aligned_allocator_indirection& ) throw() {} + template + aligned_allocator_indirection(const aligned_allocator& ) throw() {} + ~aligned_allocator_indirection() throw() {} +}; + +#ifdef _MSC_VER + + // sometimes, MSVC detects, at compile time, that the argument x + // in std::vector::resize(size_t s,T x) won't be aligned and generate an error + // even if this function is never called. Whence this little wrapper. + #define EIGEN_WORKAROUND_MSVC_STD_VECTOR(T) Eigen::ei_workaround_msvc_std_vector + template struct ei_workaround_msvc_std_vector : public T + { + inline ei_workaround_msvc_std_vector() : T() {} + inline ei_workaround_msvc_std_vector(const T& other) : T(other) {} + inline operator T& () { return *static_cast(this); } + inline operator const T& () const { return *static_cast(this); } + template + inline T& operator=(const OtherT& other) + { T::operator=(other); return *this; } + inline ei_workaround_msvc_std_vector& operator=(const ei_workaround_msvc_std_vector& other) + { T::operator=(other); return *this; } + }; + +#else + + #define EIGEN_WORKAROUND_MSVC_STD_VECTOR(T) T + +#endif + +} + +namespace std { + +#define EIGEN_STD_VECTOR_SPECIALIZATION_BODY \ + public: \ + typedef T value_type; \ + typedef typename vector_base::allocator_type allocator_type; \ + typedef typename vector_base::size_type size_type; \ + typedef typename vector_base::iterator iterator; \ + typedef typename vector_base::const_iterator const_iterator; \ + explicit vector(const allocator_type& a = allocator_type()) : vector_base(a) {} \ + template \ + vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) \ + : vector_base(first, last, a) {} \ + vector(const vector& c) : vector_base(c) {} \ + explicit vector(size_type num, const value_type& val = value_type()) : vector_base(num, val) {} \ + vector(iterator start, iterator end) : vector_base(start, end) {} \ + vector& operator=(const vector& x) { \ + vector_base::operator=(x); \ + return *this; \ + } + +template +class vector > + : public vector > +{ + typedef vector > vector_base; + EIGEN_STD_VECTOR_SPECIALIZATION_BODY + + void resize(size_type new_size) + { resize(new_size, T()); } + +#if defined(_VECTOR_) + // workaround MSVC std::vector implementation + void resize(size_type new_size, const value_type& x) + { + if (vector_base::size() < new_size) + vector_base::_Insert_n(vector_base::end(), new_size - vector_base::size(), x); + else if (new_size < vector_base::size()) + vector_base::erase(vector_base::begin() + new_size, vector_base::end()); + } + void push_back(const value_type& x) + { vector_base::push_back(x); } + using vector_base::insert; + iterator insert(const_iterator position, const value_type& x) + { return vector_base::insert(position,x); } + void insert(const_iterator position, size_type new_size, const value_type& x) + { vector_base::insert(position, new_size, x); } +#elif defined(_GLIBCXX_VECTOR) && EIGEN_GNUC_AT_LEAST(4,2) + // workaround GCC std::vector implementation + void resize(size_type new_size, const value_type& x) + { + if (new_size < vector_base::size()) + vector_base::_M_erase_at_end(this->_M_impl._M_start + new_size); + else + vector_base::insert(vector_base::end(), new_size - vector_base::size(), x); + } +#elif defined(_GLIBCXX_VECTOR) && (!EIGEN_GNUC_AT_LEAST(4,1)) + // Note that before gcc-4.1 we already have: std::vector::resize(size_type,const T&), + // no no need to workaround ! + using vector_base::resize; +#else + // either GCC 4.1 or non-GCC + // default implementation which should always work. + void resize(size_type new_size, const value_type& x) + { + if (new_size < vector_base::size()) + vector_base::erase(vector_base::begin() + new_size, vector_base::end()); + else if (new_size > vector_base::size()) + vector_base::insert(vector_base::end(), new_size - vector_base::size(), x); + } +#endif + +}; + +} + +#endif // EIGEN_STDVECTOR_MODULE_H diff --git a/Eigen/StdVector b/Eigen/StdVector index e8e8dc0aa..c0744d6a0 100644 --- a/Eigen/StdVector +++ b/Eigen/StdVector @@ -1,3 +1,7 @@ +#ifdef EIGEN_USE_NEW_STDVECTOR +#include "NewStdVector" +#else + #ifndef EIGEN_STDVECTOR_MODULE_H #define EIGEN_STDVECTOR_MODULE_H @@ -139,3 +143,5 @@ class vector } #endif // EIGEN_STDVECTOR_MODULE_H + +#endif // EIGEN_USE_NEW_STDVECTOR \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dda8ea8e2..ba328f7ae 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -211,6 +211,7 @@ ei_add_test(parametrizedline) ei_add_test(alignedbox) ei_add_test(regression) ei_add_test(stdvector) +ei_add_test(newstdvector) if(QT4_FOUND) ei_add_test(qtvector " " "${QT_QTCORE_LIBRARY}") endif(QT4_FOUND) diff --git a/test/newstdvector.cpp b/test/newstdvector.cpp new file mode 100644 index 000000000..7d1909df9 --- /dev/null +++ b/test/newstdvector.cpp @@ -0,0 +1,164 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2008 Benoit Jacob +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#define EIGEN_USE_NEW_STDVECTOR +#include "main.h" +#include +#include + +template +void check_stdvector_matrix(const MatrixType& m) +{ + int rows = m.rows(); + int cols = m.cols(); + MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols); + std::vector > v(10, MatrixType(rows,cols)), w(20, y); + v[5] = x; + w[6] = v[5]; + VERIFY_IS_APPROX(w[6], v[5]); + v = w; + for(int i = 0; i < 20; i++) + { + VERIFY_IS_APPROX(w[i], v[i]); + } + + v.resize(21); + v[20] = x; + VERIFY_IS_APPROX(v[20], x); + v.resize(22,y); + VERIFY_IS_APPROX(v[21], y); + v.push_back(x); + VERIFY_IS_APPROX(v[22], x); + VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType)); + + // do a lot of push_back such that the vector gets internally resized + // (with memory reallocation) + MatrixType* ref = &w[0]; + for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i) + v.push_back(w[i%w.size()]); + for(unsigned int i=23; i +void check_stdvector_transform(const TransformType&) +{ + typedef typename TransformType::MatrixType MatrixType; + TransformType x(MatrixType::Random()), y(MatrixType::Random()); + std::vector > v(10), w(20, y); + v[5] = x; + w[6] = v[5]; + VERIFY_IS_APPROX(w[6], v[5]); + v = w; + for(int i = 0; i < 20; i++) + { + VERIFY_IS_APPROX(w[i], v[i]); + } + + v.resize(21); + v[20] = x; + VERIFY_IS_APPROX(v[20], x); + v.resize(22,y); + VERIFY_IS_APPROX(v[21], y); + v.push_back(x); + VERIFY_IS_APPROX(v[22], x); + VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType)); + + // do a lot of push_back such that the vector gets internally resized + // (with memory reallocation) + TransformType* ref = &w[0]; + for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i) + v.push_back(w[i%w.size()]); + for(unsigned int i=23; i +void check_stdvector_quaternion(const QuaternionType&) +{ + typedef typename QuaternionType::Coefficients Coefficients; + QuaternionType x(Coefficients::Random()), y(Coefficients::Random()); + std::vector > v(10), w(20, y); + v[5] = x; + w[6] = v[5]; + VERIFY_IS_APPROX(w[6], v[5]); + v = w; + for(int i = 0; i < 20; i++) + { + VERIFY_IS_APPROX(w[i], v[i]); + } + + v.resize(21); + v[20] = x; + VERIFY_IS_APPROX(v[20], x); + v.resize(22,y); + VERIFY_IS_APPROX(v[21], y); + v.push_back(x); + VERIFY_IS_APPROX(v[22], x); + VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType)); + + // do a lot of push_back such that the vector gets internally resized + // (with memory reallocation) + QuaternionType* ref = &w[0]; + for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i) + v.push_back(w[i%w.size()]); + for(unsigned int i=23; i