1 #ifndef GSTORE_LOCAL_VECTOR_HH
2 #define GSTORE_LOCAL_VECTOR_HH 1
8 template <typename T, int N, typename A = std::allocator<T> >
11 typedef bool (local_vector<T, N, A>::*unspecified_bool_type)() const;
13 typedef value_type* iterator;
14 typedef const value_type* const_iterator;
15 typedef std::reverse_iterator<iterator> reverse_iterator;
16 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
17 typedef unsigned size_type;
19 inline local_vector(const A& allocator = A());
20 local_vector(const local_vector<T, N, A>& x);
21 template <int NN, typename AA>
22 local_vector(const local_vector<T, NN, AA>& x);
23 inline ~local_vector();
25 inline size_type size() const;
26 inline size_type capacity() const;
27 inline bool empty() const;
28 inline operator unspecified_bool_type() const;
29 inline bool operator!() const;
31 inline iterator begin();
32 inline iterator end();
33 inline const_iterator begin() const;
34 inline const_iterator end() const;
35 inline const_iterator cbegin() const;
36 inline const_iterator cend() const;
37 inline reverse_iterator rbegin();
38 inline reverse_iterator rend();
39 inline const_reverse_iterator rbegin() const;
40 inline const_reverse_iterator rend() const;
41 inline const_reverse_iterator crbegin() const;
42 inline const_reverse_iterator crend() const;
44 inline value_type& operator[](size_type i);
45 inline const value_type& operator[](size_type i) const;
46 inline value_type& front();
47 inline const value_type& front() const;
48 inline value_type& back();
49 inline const value_type& back() const;
51 inline void push_back(const value_type& x);
52 inline void push_back(value_type&& x);
53 template <typename... Args> inline void emplace_back(Args&&... args);
54 inline void pop_back();
57 inline void resize(size_type n, value_type x = value_type());
58 iterator erase(iterator position);
59 iterator erase(iterator first, iterator last);
61 inline local_vector<T, N, A>& operator=(const local_vector<T, N, A>& x);
62 template <int NN, typename AA>
63 inline local_vector<T, N, A>& operator=(const local_vector<T, NN, AA>& x);
66 struct rep : public A {
70 char lv_[sizeof(T) * N]; // XXX does not obey alignof(T)
72 inline rep(const A& a);
76 void grow(size_type n = 0);
79 template <typename T, int N, typename A>
80 inline local_vector<T, N, A>::rep::rep(const A& a)
81 : A(a), first_(reinterpret_cast<T*>(lv_)),
82 last_(first_), capacity_(first_ + N) {
85 template <typename T, int N, typename A>
86 inline local_vector<T, N, A>::local_vector(const A& allocator)
90 template <typename T, int N, typename A>
91 local_vector<T, N, A>::local_vector(const local_vector<T, N, A>& x)
93 for (const T* it = x.r_.first_; it != x.r_.last_; ++it)
97 template <typename T, int N, typename A> template <int NN, typename AA>
98 local_vector<T, N, A>::local_vector(const local_vector<T, NN, AA>& x)
100 for (const T* it = x.r_.first_; it != x.r_.last_; ++it)
104 template <typename T, int N, typename A>
105 inline local_vector<T, N, A>::~local_vector() {
106 for (T* it = r_.first_; it != r_.last_; ++it)
108 if (r_.first_ != reinterpret_cast<T*>(r_.lv_))
109 r_.deallocate(r_.first_, r_.capacity_ - r_.first_);
112 template <typename T, int N, typename A>
113 inline unsigned local_vector<T, N, A>::size() const {
114 return r_.last_ - r_.first_;
117 template <typename T, int N, typename A>
118 inline unsigned local_vector<T, N, A>::capacity() const {
119 return r_.capacity_ - r_.first_;
122 template <typename T, int N, typename A>
123 inline bool local_vector<T, N, A>::empty() const {
124 return r_.first_ == r_.last_;
127 template <typename T, int N, typename A>
128 inline local_vector<T, N, A>::operator unspecified_bool_type() const {
129 return empty() ? 0 : &local_vector<T, N, A>::empty;
132 template <typename T, int N, typename A>
133 inline bool local_vector<T, N, A>::operator!() const {
137 template <typename T, int N, typename A>
138 void local_vector<T, N, A>::grow(size_type n) {
139 size_t newcap = capacity() * 2;
142 T* m = r_.allocate(newcap);
143 for (T* it = r_.first_, *mit = m; it != r_.last_; ++it, ++mit) {
144 r_.construct(mit, std::move(*it));
147 if (r_.first_ != reinterpret_cast<T*>(r_.lv_))
148 r_.deallocate(r_.first_, capacity());
149 r_.last_ = m + (r_.last_ - r_.first_);
151 r_.capacity_ = m + newcap;
154 template <typename T, int N, typename A>
155 inline auto local_vector<T, N, A>::begin() -> iterator {
159 template <typename T, int N, typename A>
160 inline auto local_vector<T, N, A>::end() -> iterator {
164 template <typename T, int N, typename A>
165 inline auto local_vector<T, N, A>::begin() const -> const_iterator {
169 template <typename T, int N, typename A>
170 inline auto local_vector<T, N, A>::end() const -> const_iterator {
174 template <typename T, int N, typename A>
175 inline auto local_vector<T, N, A>::cbegin() const -> const_iterator {
179 template <typename T, int N, typename A>
180 inline auto local_vector<T, N, A>::cend() const -> const_iterator {
184 template <typename T, int N, typename A>
185 inline auto local_vector<T, N, A>::rbegin() -> reverse_iterator {
186 return reverse_iterator(end());
189 template <typename T, int N, typename A>
190 inline auto local_vector<T, N, A>::rend() -> reverse_iterator {
191 return reverse_iterator(begin());
194 template <typename T, int N, typename A>
195 inline auto local_vector<T, N, A>::rbegin() const -> const_reverse_iterator {
196 return const_reverse_iterator(end());
199 template <typename T, int N, typename A>
200 inline auto local_vector<T, N, A>::rend() const -> const_reverse_iterator {
201 return const_reverse_iterator(begin());
204 template <typename T, int N, typename A>
205 inline auto local_vector<T, N, A>::crbegin() const -> const_reverse_iterator {
206 return const_reverse_iterator(end());
209 template <typename T, int N, typename A>
210 inline auto local_vector<T, N, A>::crend() const -> const_reverse_iterator {
211 return const_reverse_iterator(begin());
214 template <typename T, int N, typename A>
215 inline T& local_vector<T, N, A>::operator[](size_type i) {
219 template <typename T, int N, typename A>
220 inline const T& local_vector<T, N, A>::operator[](size_type i) const {
224 template <typename T, int N, typename A>
225 inline T& local_vector<T, N, A>::front() {
229 template <typename T, int N, typename A>
230 inline const T& local_vector<T, N, A>::front() const {
234 template <typename T, int N, typename A>
235 inline T& local_vector<T, N, A>::back() {
239 template <typename T, int N, typename A>
240 inline const T& local_vector<T, N, A>::back() const {
244 template <typename T, int N, typename A>
245 inline void local_vector<T, N, A>::push_back(const T& x) {
246 if (r_.last_ == r_.capacity_)
248 r_.construct(r_.last_, x);
252 template <typename T, int N, typename A>
253 inline void local_vector<T, N, A>::push_back(T&& x) {
254 if (r_.last_ == r_.capacity_)
256 r_.construct(r_.last_, std::move(x));
260 template <typename T, int N, typename A> template <typename... Args>
261 inline void local_vector<T, N, A>::emplace_back(Args&&... args) {
262 if (r_.last_ == r_.capacity_)
264 r_.construct(r_.last_, std::forward<Args>(args)...);
268 template <typename T, int N, typename A>
269 inline void local_vector<T, N, A>::pop_back() {
270 assert(r_.first_ != r_.last_);
272 r_.destroy(r_.last_);
275 template <typename T, int N, typename A>
276 inline void local_vector<T, N, A>::clear() {
277 for (auto it = r_.first_; it != r_.last_; ++it)
279 r_.last_ = r_.first_;
282 template <typename T, int N, typename A>
283 inline void local_vector<T, N, A>::resize(size_type n, value_type v) {
286 auto it = r_.first_ + n;
289 for (; it < xt; ++it)
291 for (; xt < it; ++xt)
295 template <typename T, int N, typename A>
296 local_vector<T, N, A>&
297 local_vector<T, N, A>::operator=(const local_vector<T, N, A>& x) {
300 if (capacity() < x.capacity())
302 for (auto xit = x.r_.first_; xit != x.r_.last_; ++xit, ++r_.last_)
303 r_.construct(r_.last_, *xit);
308 template <typename T, int N, typename A> template <int NN, typename AA>
309 local_vector<T, N, A>&
310 local_vector<T, N, A>::operator=(const local_vector<T, NN, AA>& x) {
312 if (capacity() < x.capacity())
314 for (auto xit = x.r_.first_; xit != x.r_.last_; ++xit, ++r_.last_)
315 r_.construct(r_.last_, *xit);
319 template <typename T, int N, typename A>
320 inline T* local_vector<T, N, A>::erase(iterator position) {
321 return erase(position, position + 1);
324 template <typename T, int N, typename A>
325 T* local_vector<T, N, A>::erase(iterator first, iterator last) {
327 iterator it = first, xend = end();
328 for (; last != xend; ++it, ++last)
329 *it = std::move(*last);
331 for (; it != xend; ++it)