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 
00031 // Define the explicit instantiation (e.g. necessary for the Intel compiler)
00032 #if defined(__INTEL_COMPILER) || defined(__GNUC__)
00033   #define EIGEN_EXPLICIT_STL_VECTOR_INSTANTIATION(...) template class std::vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> >;
00034 #else
00035   #define EIGEN_EXPLICIT_STL_VECTOR_INSTANTIATION(...)
00036 #endif
00037 
00043 #define EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(...) \
00044 EIGEN_EXPLICIT_STL_VECTOR_INSTANTIATION(__VA_ARGS__) \
00045 namespace std \
00046 { \
00047   template<typename _Ay> \
00048   class vector<__VA_ARGS__, _Ay>  \
00049     : public vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > \
00050   { \
00051     typedef vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > vector_base; \
00052   public: \
00053     typedef __VA_ARGS__ value_type; \
00054     typedef typename vector_base::allocator_type allocator_type; \
00055     typedef typename vector_base::size_type size_type;  \
00056     typedef typename vector_base::iterator iterator;  \
00057     explicit vector(const allocator_type& a = allocator_type()) : vector_base(a) {}  \
00058     template<typename InputIterator> \
00059     vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) : vector_base(first, last, a) {} \
00060     vector(const vector& c) : vector_base(c) {}  \
00061     explicit vector(size_type num, const value_type& val = value_type()) : vector_base(num, val) {} \
00062     vector(iterator start, iterator end) : vector_base(start, end) {}  \
00063     vector& operator=(const vector& x) {  \
00064       vector_base::operator=(x);  \
00065       return *this;  \
00066     } \
00067   }; \
00068 }
00069 
00070 namespace std {
00071 
00072 #define EIGEN_STD_VECTOR_SPECIALIZATION_BODY \
00073   public:  \
00074     typedef T value_type; \
00075     typedef typename vector_base::allocator_type allocator_type; \
00076     typedef typename vector_base::size_type size_type;  \
00077     typedef typename vector_base::iterator iterator;  \
00078     typedef typename vector_base::const_iterator const_iterator;  \
00079     explicit vector(const allocator_type& a = allocator_type()) : vector_base(a) {}  \
00080     template<typename InputIterator> \
00081     vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) \
00082     : vector_base(first, last, a) {} \
00083     vector(const vector& c) : vector_base(c) {}  \
00084     explicit vector(size_type num, const value_type& val = value_type()) : vector_base(num, val) {} \
00085     vector(iterator start, iterator end) : vector_base(start, end) {}  \
00086     vector& operator=(const vector& x) {  \
00087       vector_base::operator=(x);  \
00088       return *this;  \
00089     }
00090 
00091   template<typename T>
00092   class vector<T,EIGEN_ALIGNED_ALLOCATOR<T> >
00093     : public vector<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
00094                     Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> >
00095 {
00096   typedef vector<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
00097                  Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> > vector_base;
00098   EIGEN_STD_VECTOR_SPECIALIZATION_BODY
00099 
00100   void resize(size_type new_size)
00101   { resize(new_size, T()); }
00102 
00103 #if defined(_VECTOR_)
00104   // workaround MSVC std::vector implementation
00105   void resize(size_type new_size, const value_type& x)
00106   {
00107     if (vector_base::size() < new_size)
00108       vector_base::_Insert_n(vector_base::end(), new_size - vector_base::size(), x);
00109     else if (new_size < vector_base::size())
00110       vector_base::erase(vector_base::begin() + new_size, vector_base::end());
00111   }
00112   void push_back(const value_type& x)
00113   { vector_base::push_back(x); } 
00114   using vector_base::insert;  
00115   iterator insert(const_iterator position, const value_type& x)
00116   { return vector_base::insert(position,x); }
00117   void insert(const_iterator position, size_type new_size, const value_type& x)
00118   { vector_base::insert(position, new_size, x); }
00119 #elif defined(_GLIBCXX_VECTOR) && (!(EIGEN_GNUC_AT_LEAST(4,1)))
00120   /* Note that before gcc-4.1 we already have: std::vector::resize(size_type,const T&).
00121    * However, this specialization is still needed to make the above EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION trick to work. */
00122   void resize(size_type new_size, const value_type& x)
00123   {
00124     vector_base::resize(new_size,x);
00125   }
00126 #elif defined(_GLIBCXX_VECTOR) && EIGEN_GNUC_AT_LEAST(4,2)
00127   // workaround GCC std::vector implementation
00128   void resize(size_type new_size, const value_type& x)
00129   {
00130     if (new_size < vector_base::size())
00131       vector_base::_M_erase_at_end(this->_M_impl._M_start + new_size);
00132     else
00133       vector_base::insert(vector_base::end(), new_size - vector_base::size(), x);
00134   }
00135 #else
00136   // either GCC 4.1 or non-GCC
00137   // default implementation which should always work.
00138   void resize(size_type new_size, const value_type& x)
00139   {
00140     if (new_size < vector_base::size())
00141       vector_base::erase(vector_base::begin() + new_size, vector_base::end());
00142     else if (new_size > vector_base::size())
00143       vector_base::insert(vector_base::end(), new_size - vector_base::size(), x);
00144   }
00145 #endif
00146   };
00147 }
00148 
00149 #endif // EIGEN_STDVECTOR_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:00