Home | History | Annotate | Download | only in StlSupport
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud (at) inria.fr>
      5 // Copyright (C) 2009 Hauke Heibel <hauke.heibel (at) googlemail.com>
      6 //
      7 // This Source Code Form is subject to the terms of the Mozilla
      8 // Public License v. 2.0. If a copy of the MPL was not distributed
      9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     10 
     11 #ifndef EIGEN_STDDEQUE_H
     12 #define EIGEN_STDDEQUE_H
     13 
     14 #include "details.h"
     15 
     16 /**
     17  * This section contains a convenience MACRO which allows an easy specialization of
     18  * std::deque such that for data types with alignment issues the correct allocator
     19  * is used automatically.
     20  */
     21 #define EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(...) \
     22 namespace std \
     23 { \
     24   template<> \
     25   class deque<__VA_ARGS__, std::allocator<__VA_ARGS__> >           \
     26     : public deque<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > \
     27   { \
     28     typedef deque<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > deque_base; \
     29   public: \
     30     typedef __VA_ARGS__ value_type; \
     31     typedef deque_base::allocator_type allocator_type; \
     32     typedef deque_base::size_type size_type;  \
     33     typedef deque_base::iterator iterator;  \
     34     explicit deque(const allocator_type& a = allocator_type()) : deque_base(a) {}  \
     35     template<typename InputIterator> \
     36     deque(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) : deque_base(first, last, a) {} \
     37     deque(const deque& c) : deque_base(c) {}  \
     38     explicit deque(size_type num, const value_type& val = value_type()) : deque_base(num, val) {} \
     39     deque(iterator start, iterator end) : deque_base(start, end) {}  \
     40     deque& operator=(const deque& x) {  \
     41       deque_base::operator=(x);  \
     42       return *this;  \
     43     } \
     44   }; \
     45 }
     46 
     47 // check whether we really need the std::deque specialization
     48 #if !EIGEN_HAS_CXX11_CONTAINERS && !(defined(_GLIBCXX_DEQUE) && (!EIGEN_GNUC_AT_LEAST(4,1))) /* Note that before gcc-4.1 we already have: std::deque::resize(size_type,const T&). */
     49 
     50 namespace std {
     51 
     52 #define EIGEN_STD_DEQUE_SPECIALIZATION_BODY \
     53   public:  \
     54     typedef T value_type; \
     55     typedef typename deque_base::allocator_type allocator_type; \
     56     typedef typename deque_base::size_type size_type;  \
     57     typedef typename deque_base::iterator iterator;  \
     58     typedef typename deque_base::const_iterator const_iterator;  \
     59     explicit deque(const allocator_type& a = allocator_type()) : deque_base(a) {}  \
     60     template<typename InputIterator> \
     61     deque(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) \
     62     : deque_base(first, last, a) {} \
     63     deque(const deque& c) : deque_base(c) {}  \
     64     explicit deque(size_type num, const value_type& val = value_type()) : deque_base(num, val) {} \
     65     deque(iterator start, iterator end) : deque_base(start, end) {}  \
     66     deque& operator=(const deque& x) {  \
     67       deque_base::operator=(x);  \
     68       return *this;  \
     69     }
     70 
     71   template<typename T>
     72   class deque<T,EIGEN_ALIGNED_ALLOCATOR<T> >
     73     : public deque<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
     74                    Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> >
     75 {
     76   typedef deque<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
     77                 Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> > deque_base;
     78   EIGEN_STD_DEQUE_SPECIALIZATION_BODY
     79 
     80   void resize(size_type new_size)
     81   { resize(new_size, T()); }
     82 
     83 #if defined(_DEQUE_)
     84   // workaround MSVC std::deque implementation
     85   void resize(size_type new_size, const value_type& x)
     86   {
     87     if (deque_base::size() < new_size)
     88       deque_base::_Insert_n(deque_base::end(), new_size - deque_base::size(), x);
     89     else if (new_size < deque_base::size())
     90       deque_base::erase(deque_base::begin() + new_size, deque_base::end());
     91   }
     92   void push_back(const value_type& x)
     93   { deque_base::push_back(x); }
     94   void push_front(const value_type& x)
     95   { deque_base::push_front(x); }
     96   using deque_base::insert;
     97   iterator insert(const_iterator position, const value_type& x)
     98   { return deque_base::insert(position,x); }
     99   void insert(const_iterator position, size_type new_size, const value_type& x)
    100   { deque_base::insert(position, new_size, x); }
    101 #elif defined(_GLIBCXX_DEQUE) && EIGEN_GNUC_AT_LEAST(4,2)
    102   // workaround GCC std::deque implementation
    103   void resize(size_type new_size, const value_type& x)
    104   {
    105     if (new_size < deque_base::size())
    106       deque_base::_M_erase_at_end(this->_M_impl._M_start + new_size);
    107     else
    108       deque_base::insert(deque_base::end(), new_size - deque_base::size(), x);
    109   }
    110 #else
    111   // either GCC 4.1 or non-GCC
    112   // default implementation which should always work.
    113   void resize(size_type new_size, const value_type& x)
    114   {
    115     if (new_size < deque_base::size())
    116       deque_base::erase(deque_base::begin() + new_size, deque_base::end());
    117     else if (new_size > deque_base::size())
    118       deque_base::insert(deque_base::end(), new_size - deque_base::size(), x);
    119   }
    120 #endif
    121   };
    122 }
    123 
    124 #endif // check whether specialization is actually required
    125 
    126 #endif // EIGEN_STDDEQUE_H
    127