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 |
|
---|
11 | #ifndef EIGEN_STDVECTOR_H
|
---|
12 | #define EIGEN_STDVECTOR_H
|
---|
13 |
|
---|
14 | #include "details.h"
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This section contains a convenience MACRO which allows an easy specialization of
|
---|
18 | * std::vector such that for data types with alignment issues the correct allocator
|
---|
19 | * is used automatically.
|
---|
20 | */
|
---|
21 | #define EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(...) \
|
---|
22 | namespace std \
|
---|
23 | { \
|
---|
24 | template<> \
|
---|
25 | class vector<__VA_ARGS__, std::allocator<__VA_ARGS__> > \
|
---|
26 | : public vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > \
|
---|
27 | { \
|
---|
28 | typedef vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > vector_base; \
|
---|
29 | public: \
|
---|
30 | typedef __VA_ARGS__ value_type; \
|
---|
31 | typedef vector_base::allocator_type allocator_type; \
|
---|
32 | typedef vector_base::size_type size_type; \
|
---|
33 | typedef vector_base::iterator iterator; \
|
---|
34 | explicit vector(const allocator_type& a = allocator_type()) : vector_base(a) {} \
|
---|
35 | template<typename InputIterator> \
|
---|
36 | vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) : vector_base(first, last, a) {} \
|
---|
37 | vector(const vector& c) : vector_base(c) {} \
|
---|
38 | explicit vector(size_type num, const value_type& val = value_type()) : vector_base(num, val) {} \
|
---|
39 | vector(iterator start, iterator end) : vector_base(start, end) {} \
|
---|
40 | vector& operator=(const vector& x) { \
|
---|
41 | vector_base::operator=(x); \
|
---|
42 | return *this; \
|
---|
43 | } \
|
---|
44 | }; \
|
---|
45 | }
|
---|
46 |
|
---|
47 | namespace std {
|
---|
48 |
|
---|
49 | #define EIGEN_STD_VECTOR_SPECIALIZATION_BODY \
|
---|
50 | public: \
|
---|
51 | typedef T value_type; \
|
---|
52 | typedef typename vector_base::allocator_type allocator_type; \
|
---|
53 | typedef typename vector_base::size_type size_type; \
|
---|
54 | typedef typename vector_base::iterator iterator; \
|
---|
55 | typedef typename vector_base::const_iterator const_iterator; \
|
---|
56 | explicit vector(const allocator_type& a = allocator_type()) : vector_base(a) {} \
|
---|
57 | template<typename InputIterator> \
|
---|
58 | vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) \
|
---|
59 | : vector_base(first, last, a) {} \
|
---|
60 | vector(const vector& c) : vector_base(c) {} \
|
---|
61 | explicit vector(size_type num, const value_type& val = value_type()) : vector_base(num, val) {} \
|
---|
62 | vector(iterator start, iterator end) : vector_base(start, end) {} \
|
---|
63 | vector& operator=(const vector& x) { \
|
---|
64 | vector_base::operator=(x); \
|
---|
65 | return *this; \
|
---|
66 | }
|
---|
67 |
|
---|
68 | template<typename T>
|
---|
69 | class vector<T,EIGEN_ALIGNED_ALLOCATOR<T> >
|
---|
70 | : public vector<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
|
---|
71 | Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> >
|
---|
72 | {
|
---|
73 | typedef vector<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
|
---|
74 | Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> > vector_base;
|
---|
75 | EIGEN_STD_VECTOR_SPECIALIZATION_BODY
|
---|
76 |
|
---|
77 | void resize(size_type new_size)
|
---|
78 | { resize(new_size, T()); }
|
---|
79 |
|
---|
80 | #if defined(_VECTOR_)
|
---|
81 | // workaround MSVC std::vector implementation
|
---|
82 | void resize(size_type new_size, const value_type& x)
|
---|
83 | {
|
---|
84 | if (vector_base::size() < new_size)
|
---|
85 | vector_base::_Insert_n(vector_base::end(), new_size - vector_base::size(), x);
|
---|
86 | else if (new_size < vector_base::size())
|
---|
87 | vector_base::erase(vector_base::begin() + new_size, vector_base::end());
|
---|
88 | }
|
---|
89 | void push_back(const value_type& x)
|
---|
90 | { vector_base::push_back(x); }
|
---|
91 | using vector_base::insert;
|
---|
92 | iterator insert(const_iterator position, const value_type& x)
|
---|
93 | { return vector_base::insert(position,x); }
|
---|
94 | void insert(const_iterator position, size_type new_size, const value_type& x)
|
---|
95 | { vector_base::insert(position, new_size, x); }
|
---|
96 | #elif defined(_GLIBCXX_VECTOR) && (!(EIGEN_GNUC_AT_LEAST(4,1)))
|
---|
97 | /* Note that before gcc-4.1 we already have: std::vector::resize(size_type,const T&).
|
---|
98 | * However, this specialization is still needed to make the above EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION trick to work. */
|
---|
99 | void resize(size_type new_size, const value_type& x)
|
---|
100 | {
|
---|
101 | vector_base::resize(new_size,x);
|
---|
102 | }
|
---|
103 | #elif defined(_GLIBCXX_VECTOR) && EIGEN_GNUC_AT_LEAST(4,2)
|
---|
104 | // workaround GCC std::vector implementation
|
---|
105 | void resize(size_type new_size, const value_type& x)
|
---|
106 | {
|
---|
107 | if (new_size < vector_base::size())
|
---|
108 | vector_base::_M_erase_at_end(this->_M_impl._M_start + new_size);
|
---|
109 | else
|
---|
110 | vector_base::insert(vector_base::end(), new_size - vector_base::size(), x);
|
---|
111 | }
|
---|
112 | #else
|
---|
113 | // either GCC 4.1 or non-GCC
|
---|
114 | // default implementation which should always work.
|
---|
115 | void resize(size_type new_size, const value_type& x)
|
---|
116 | {
|
---|
117 | if (new_size < vector_base::size())
|
---|
118 | vector_base::erase(vector_base::begin() + new_size, vector_base::end());
|
---|
119 | else if (new_size > vector_base::size())
|
---|
120 | vector_base::insert(vector_base::end(), new_size - vector_base::size(), x);
|
---|
121 | }
|
---|
122 | #endif
|
---|
123 | };
|
---|
124 | }
|
---|
125 |
|
---|
126 | #endif // EIGEN_STDVECTOR_H
|
---|