![]() |
Eigen
3.3.3
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // Copyright (C) 2009 Hauke Heibel <hauke.heibel@googlemail.com> 00006 // 00007 // This Source Code Form is subject to the terms of the Mozilla 00008 // Public License v. 2.0. If a copy of the MPL was not distributed 00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00010 00011 #ifndef EIGEN_STDVECTOR_H 00012 #define EIGEN_STDVECTOR_H 00013 00014 #include "details.h" 00015 00021 #define EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(...) \ 00022 namespace std \ 00023 { \ 00024 template<> \ 00025 class vector<__VA_ARGS__, std::allocator<__VA_ARGS__> > \ 00026 : public vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > \ 00027 { \ 00028 typedef vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > vector_base; \ 00029 public: \ 00030 typedef __VA_ARGS__ value_type; \ 00031 typedef vector_base::allocator_type allocator_type; \ 00032 typedef vector_base::size_type size_type; \ 00033 typedef vector_base::iterator iterator; \ 00034 explicit vector(const allocator_type& a = allocator_type()) : vector_base(a) {} \ 00035 template<typename InputIterator> \ 00036 vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) : vector_base(first, last, a) {} \ 00037 vector(const vector& c) : vector_base(c) {} \ 00038 explicit vector(size_type num, const value_type& val = value_type()) : vector_base(num, val) {} \ 00039 vector(iterator start, iterator end) : vector_base(start, end) {} \ 00040 vector& operator=(const vector& x) { \ 00041 vector_base::operator=(x); \ 00042 return *this; \ 00043 } \ 00044 }; \ 00045 } 00046 00047 // Don't specialize if containers are implemented according to C++11 00048 #if !EIGEN_HAS_CXX11_CONTAINERS 00049 00050 namespace std { 00051 00052 #define EIGEN_STD_VECTOR_SPECIALIZATION_BODY \ 00053 public: \ 00054 typedef T value_type; \ 00055 typedef typename vector_base::allocator_type allocator_type; \ 00056 typedef typename vector_base::size_type size_type; \ 00057 typedef typename vector_base::iterator iterator; \ 00058 typedef typename vector_base::const_iterator const_iterator; \ 00059 explicit vector(const allocator_type& a = allocator_type()) : vector_base(a) {} \ 00060 template<typename InputIterator> \ 00061 vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) \ 00062 : vector_base(first, last, a) {} \ 00063 vector(const vector& c) : vector_base(c) {} \ 00064 explicit vector(size_type num, const value_type& val = value_type()) : vector_base(num, val) {} \ 00065 vector(iterator start, iterator end) : vector_base(start, end) {} \ 00066 vector& operator=(const vector& x) { \ 00067 vector_base::operator=(x); \ 00068 return *this; \ 00069 } 00070 00071 template<typename T> 00072 class vector<T,EIGEN_ALIGNED_ALLOCATOR<T> > 00073 : public vector<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T), 00074 Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> > 00075 { 00076 typedef vector<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T), 00077 Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> > vector_base; 00078 EIGEN_STD_VECTOR_SPECIALIZATION_BODY 00079 00080 void resize(size_type new_size) 00081 { resize(new_size, T()); } 00082 00083 #if defined(_VECTOR_) 00084 // workaround MSVC std::vector implementation 00085 void resize(size_type new_size, const value_type& x) 00086 { 00087 if (vector_base::size() < new_size) 00088 vector_base::_Insert_n(vector_base::end(), new_size - vector_base::size(), x); 00089 else if (new_size < vector_base::size()) 00090 vector_base::erase(vector_base::begin() + new_size, vector_base::end()); 00091 } 00092 void push_back(const value_type& x) 00093 { vector_base::push_back(x); } 00094 using vector_base::insert; 00095 iterator insert(const_iterator position, const value_type& x) 00096 { return vector_base::insert(position,x); } 00097 void insert(const_iterator position, size_type new_size, const value_type& x) 00098 { vector_base::insert(position, new_size, x); } 00099 #elif defined(_GLIBCXX_VECTOR) && (!(EIGEN_GNUC_AT_LEAST(4,1))) 00100 /* Note that before gcc-4.1 we already have: std::vector::resize(size_type,const T&). 00101 * However, this specialization is still needed to make the above EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION trick to work. */ 00102 void resize(size_type new_size, const value_type& x) 00103 { 00104 vector_base::resize(new_size,x); 00105 } 00106 #elif defined(_GLIBCXX_VECTOR) && EIGEN_GNUC_AT_LEAST(4,2) 00107 // workaround GCC std::vector implementation 00108 void resize(size_type new_size, const value_type& x) 00109 { 00110 if (new_size < vector_base::size()) 00111 vector_base::_M_erase_at_end(this->_M_impl._M_start + new_size); 00112 else 00113 vector_base::insert(vector_base::end(), new_size - vector_base::size(), x); 00114 } 00115 #else 00116 // either GCC 4.1 or non-GCC 00117 // default implementation which should always work. 00118 void resize(size_type new_size, const value_type& x) 00119 { 00120 if (new_size < vector_base::size()) 00121 vector_base::erase(vector_base::begin() + new_size, vector_base::end()); 00122 else if (new_size > vector_base::size()) 00123 vector_base::insert(vector_base::end(), new_size - vector_base::size(), x); 00124 } 00125 #endif 00126 }; 00127 } 00128 #endif // !EIGEN_HAS_CXX11_CONTAINERS 00129 00130 00131 #endif // EIGEN_STDVECTOR_H