StdVector.h
Go to the documentation of this file.
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 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_STDVECTOR_H
00027 #define EIGEN_STDVECTOR_H
00028 
00029 #include "Eigen/src/StlSupport/details.h"
00030 
00036 #define EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(...) \
00037 namespace std \
00038 { \
00039   template<> \
00040   class vector<__VA_ARGS__, std::allocator<__VA_ARGS__> >  \
00041     : public vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > \
00042   { \
00043     typedef vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > vector_base; \
00044   public: \
00045     typedef __VA_ARGS__ value_type; \
00046     typedef vector_base::allocator_type allocator_type; \
00047     typedef vector_base::size_type size_type;  \
00048     typedef vector_base::iterator iterator;  \
00049     explicit vector(const allocator_type& a = allocator_type()) : vector_base(a) {}  \
00050     template<typename InputIterator> \
00051     vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) : vector_base(first, last, a) {} \
00052     vector(const vector& c) : vector_base(c) {}  \
00053     explicit vector(size_type num, const value_type& val = value_type()) : vector_base(num, val) {} \
00054     vector(iterator start, iterator end) : vector_base(start, end) {}  \
00055     vector& operator=(const vector& x) {  \
00056       vector_base::operator=(x);  \
00057       return *this;  \
00058     } \
00059   }; \
00060 }
00061 
00062 namespace std {
00063 
00064 #define EIGEN_STD_VECTOR_SPECIALIZATION_BODY \
00065   public:  \
00066     typedef T value_type; \
00067     typedef typename vector_base::allocator_type allocator_type; \
00068     typedef typename vector_base::size_type size_type;  \
00069     typedef typename vector_base::iterator iterator;  \
00070     typedef typename vector_base::const_iterator const_iterator;  \
00071     explicit vector(const allocator_type& a = allocator_type()) : vector_base(a) {}  \
00072     template<typename InputIterator> \
00073     vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) \
00074     : vector_base(first, last, a) {} \
00075     vector(const vector& c) : vector_base(c) {}  \
00076     explicit vector(size_type num, const value_type& val = value_type()) : vector_base(num, val) {} \
00077     vector(iterator start, iterator end) : vector_base(start, end) {}  \
00078     vector& operator=(const vector& x) {  \
00079       vector_base::operator=(x);  \
00080       return *this;  \
00081     }
00082 
00083   template<typename T>
00084   class vector<T,EIGEN_ALIGNED_ALLOCATOR<T> >
00085     : public vector<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
00086                     Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> >
00087 {
00088   typedef vector<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
00089                  Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> > vector_base;
00090   EIGEN_STD_VECTOR_SPECIALIZATION_BODY
00091 
00092   void resize(size_type new_size)
00093   { resize(new_size, T()); }
00094 
00095 #if defined(_VECTOR_)
00096   // workaround MSVC std::vector implementation
00097   void resize(size_type new_size, const value_type& x)
00098   {
00099     if (vector_base::size() < new_size)
00100       vector_base::_Insert_n(vector_base::end(), new_size - vector_base::size(), x);
00101     else if (new_size < vector_base::size())
00102       vector_base::erase(vector_base::begin() + new_size, vector_base::end());
00103   }
00104   void push_back(const value_type& x)
00105   { vector_base::push_back(x); } 
00106   using vector_base::insert;  
00107   iterator insert(const_iterator position, const value_type& x)
00108   { return vector_base::insert(position,x); }
00109   void insert(const_iterator position, size_type new_size, const value_type& x)
00110   { vector_base::insert(position, new_size, x); }
00111 #elif defined(_GLIBCXX_VECTOR) && (!(EIGEN_GNUC_AT_LEAST(4,1)))
00112   /* Note that before gcc-4.1 we already have: std::vector::resize(size_type,const T&).
00113    * However, this specialization is still needed to make the above EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION trick to work. */
00114   void resize(size_type new_size, const value_type& x)
00115   {
00116     vector_base::resize(new_size,x);
00117   }
00118 #elif defined(_GLIBCXX_VECTOR) && EIGEN_GNUC_AT_LEAST(4,2)
00119   // workaround GCC std::vector implementation
00120   void resize(size_type new_size, const value_type& x)
00121   {
00122     if (new_size < vector_base::size())
00123       vector_base::_M_erase_at_end(this->_M_impl._M_start + new_size);
00124     else
00125       vector_base::insert(vector_base::end(), new_size - vector_base::size(), x);
00126   }
00127 #else
00128   // either GCC 4.1 or non-GCC
00129   // default implementation which should always work.
00130   void resize(size_type new_size, const value_type& x)
00131   {
00132     if (new_size < vector_base::size())
00133       vector_base::erase(vector_base::begin() + new_size, vector_base::end());
00134     else if (new_size > vector_base::size())
00135       vector_base::insert(vector_base::end(), new_size - vector_base::size(), x);
00136   }
00137 #endif
00138   };
00139 }
00140 
00141 #endif // EIGEN_STDVECTOR_H


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:33:40