StdDeque.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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@inria.fr>
  5. // Copyright (C) 2009 Hauke Heibel <hauke.heibel@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. #ifndef EIGEN_STDDEQUE_H
  11. #define EIGEN_STDDEQUE_H
  12. #include "details.h"
  13. /**
  14. * This section contains a convenience MACRO which allows an easy specialization of
  15. * std::deque such that for data types with alignment issues the correct allocator
  16. * is used automatically.
  17. */
  18. #define EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(...) \
  19. namespace std \
  20. { \
  21. template<> \
  22. class deque<__VA_ARGS__, std::allocator<__VA_ARGS__> > \
  23. : public deque<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > \
  24. { \
  25. typedef deque<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > deque_base; \
  26. public: \
  27. typedef __VA_ARGS__ value_type; \
  28. typedef deque_base::allocator_type allocator_type; \
  29. typedef deque_base::size_type size_type; \
  30. typedef deque_base::iterator iterator; \
  31. explicit deque(const allocator_type& a = allocator_type()) : deque_base(a) {} \
  32. template<typename InputIterator> \
  33. deque(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) : deque_base(first, last, a) {} \
  34. deque(const deque& c) : deque_base(c) {} \
  35. explicit deque(size_type num, const value_type& val = value_type()) : deque_base(num, val) {} \
  36. deque(iterator start_, iterator end_) : deque_base(start_, end_) {} \
  37. deque& operator=(const deque& x) { \
  38. deque_base::operator=(x); \
  39. return *this; \
  40. } \
  41. }; \
  42. }
  43. // check whether we really need the std::deque specialization
  44. #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&). */
  45. namespace std {
  46. #define EIGEN_STD_DEQUE_SPECIALIZATION_BODY \
  47. public: \
  48. typedef T value_type; \
  49. typedef typename deque_base::allocator_type allocator_type; \
  50. typedef typename deque_base::size_type size_type; \
  51. typedef typename deque_base::iterator iterator; \
  52. typedef typename deque_base::const_iterator const_iterator; \
  53. explicit deque(const allocator_type& a = allocator_type()) : deque_base(a) {} \
  54. template<typename InputIterator> \
  55. deque(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) \
  56. : deque_base(first, last, a) {} \
  57. deque(const deque& c) : deque_base(c) {} \
  58. explicit deque(size_type num, const value_type& val = value_type()) : deque_base(num, val) {} \
  59. deque(iterator start_, iterator end_) : deque_base(start_, end_) {} \
  60. deque& operator=(const deque& x) { \
  61. deque_base::operator=(x); \
  62. return *this; \
  63. }
  64. template<typename T>
  65. class deque<T,EIGEN_ALIGNED_ALLOCATOR<T> >
  66. : public deque<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
  67. Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> >
  68. {
  69. typedef deque<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
  70. Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> > deque_base;
  71. EIGEN_STD_DEQUE_SPECIALIZATION_BODY
  72. void resize(size_type new_size)
  73. { resize(new_size, T()); }
  74. #if defined(_DEQUE_)
  75. // workaround MSVC std::deque implementation
  76. void resize(size_type new_size, const value_type& x)
  77. {
  78. if (deque_base::size() < new_size)
  79. deque_base::_Insert_n(deque_base::end(), new_size - deque_base::size(), x);
  80. else if (new_size < deque_base::size())
  81. deque_base::erase(deque_base::begin() + new_size, deque_base::end());
  82. }
  83. void push_back(const value_type& x)
  84. { deque_base::push_back(x); }
  85. void push_front(const value_type& x)
  86. { deque_base::push_front(x); }
  87. using deque_base::insert;
  88. iterator insert(const_iterator position, const value_type& x)
  89. { return deque_base::insert(position,x); }
  90. void insert(const_iterator position, size_type new_size, const value_type& x)
  91. { deque_base::insert(position, new_size, x); }
  92. #else
  93. // default implementation which should always work.
  94. void resize(size_type new_size, const value_type& x)
  95. {
  96. if (new_size < deque_base::size())
  97. deque_base::erase(deque_base::begin() + new_size, deque_base::end());
  98. else if (new_size > deque_base::size())
  99. deque_base::insert(deque_base::end(), new_size - deque_base::size(), x);
  100. }
  101. #endif
  102. };
  103. }
  104. #endif // check whether specialization is actually required
  105. #endif // EIGEN_STDDEQUE_H