Merge from mainline (151362:151806)
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / debug / deque
blobd6078f580e1504328fe3e3142e3fa8f9e8a19a64
1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file debug/deque
27  *  This file is a GNU debug extension to the Standard C++ Library.
28  */
30 #ifndef _GLIBCXX_DEBUG_DEQUE
31 #define _GLIBCXX_DEBUG_DEQUE 1
33 #include <deque>
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
37 namespace std
39 namespace __debug
41   /// Class std::deque with safety/checking/debug instrumentation.
42   template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
43     class deque
44     : public _GLIBCXX_STD_D::deque<_Tp, _Allocator>,
45       public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
46     {
47       typedef  _GLIBCXX_STD_D::deque<_Tp, _Allocator> _Base;
48       typedef __gnu_debug::_Safe_sequence<deque> _Safe_base;
50     public:
51       typedef typename _Base::reference             reference;
52       typedef typename _Base::const_reference       const_reference;
54       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,deque>
55                                                     iterator;
56       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,deque>
57                                                      const_iterator;
59       typedef typename _Base::size_type             size_type;
60       typedef typename _Base::difference_type       difference_type;
62       typedef _Tp                                   value_type;
63       typedef _Allocator                            allocator_type;
64       typedef typename _Base::pointer               pointer;
65       typedef typename _Base::const_pointer         const_pointer;
66       typedef std::reverse_iterator<iterator>       reverse_iterator;
67       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
69       // 23.2.1.1 construct/copy/destroy:
70       explicit deque(const _Allocator& __a = _Allocator())
71       : _Base(__a) { }
73       explicit deque(size_type __n, const _Tp& __value = _Tp(),
74                      const _Allocator& __a = _Allocator())
75       : _Base(__n, __value, __a) { }
77       template<class _InputIterator>
78         deque(_InputIterator __first, _InputIterator __last,
79               const _Allocator& __a = _Allocator())
80         : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
81         { }
83       deque(const deque& __x)
84       : _Base(__x), _Safe_base() { }
86       deque(const _Base& __x)
87       : _Base(__x), _Safe_base() { }
89 #ifdef __GXX_EXPERIMENTAL_CXX0X__
90       deque(deque&& __x)
91       : _Base(std::forward<deque>(__x)), _Safe_base()
92       { this->_M_swap(__x); }
94       deque(initializer_list<value_type> __l,
95             const allocator_type& __a = allocator_type())
96       : _Base(__l, __a), _Safe_base() { }
97 #endif
99       ~deque() { }
101       deque&
102       operator=(const deque& __x)
103       {
104         *static_cast<_Base*>(this) = __x;
105         this->_M_invalidate_all();
106         return *this;
107       }
109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
110       deque&
111       operator=(deque&& __x)
112       {
113         // NB: DR 675.
114         clear();
115         swap(__x);        
116         return *this;
117       }
119       deque&
120       operator=(initializer_list<value_type> __l)
121       {
122         *static_cast<_Base*>(this) = __l;
123         this->_M_invalidate_all();
124         return *this;
125       }
126 #endif
128       template<class _InputIterator>
129         void
130         assign(_InputIterator __first, _InputIterator __last)
131         {
132           __glibcxx_check_valid_range(__first, __last);
133           _Base::assign(__first, __last);
134           this->_M_invalidate_all();
135         }
137       void
138       assign(size_type __n, const _Tp& __t)
139       {
140         _Base::assign(__n, __t);
141         this->_M_invalidate_all();
142       }
144 #ifdef __GXX_EXPERIMENTAL_CXX0X__
145       void
146       assign(initializer_list<value_type> __l)
147       {
148         _Base::assign(__l);
149         this->_M_invalidate_all();
150       }
151 #endif
153       using _Base::get_allocator;
155       // iterators:
156       iterator
157       begin()
158       { return iterator(_Base::begin(), this); }
160       const_iterator
161       begin() const
162       { return const_iterator(_Base::begin(), this); }
164       iterator
165       end()
166       { return iterator(_Base::end(), this); }
168       const_iterator
169       end() const
170       { return const_iterator(_Base::end(), this); }
172       reverse_iterator
173       rbegin()
174       { return reverse_iterator(end()); }
176       const_reverse_iterator
177       rbegin() const
178       { return const_reverse_iterator(end()); }
180       reverse_iterator
181       rend()
182       { return reverse_iterator(begin()); }
184       const_reverse_iterator
185       rend() const
186       { return const_reverse_iterator(begin()); }
188 #ifdef __GXX_EXPERIMENTAL_CXX0X__
189       const_iterator
190       cbegin() const
191       { return const_iterator(_Base::begin(), this); }
193       const_iterator
194       cend() const
195       { return const_iterator(_Base::end(), this); }
197       const_reverse_iterator
198       crbegin() const
199       { return const_reverse_iterator(end()); }
201       const_reverse_iterator
202       crend() const
203       { return const_reverse_iterator(begin()); }
204 #endif
206       // 23.2.1.2 capacity:
207       using _Base::size;
208       using _Base::max_size;
210       void
211       resize(size_type __sz, _Tp __c = _Tp())
212       {
213         typedef typename _Base::const_iterator _Base_const_iterator;
214         typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
216         bool __invalidate_all = __sz > this->size();
217         if (__sz < this->size())
218           this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
220         _Base::resize(__sz, __c);
222         if (__invalidate_all)
223           this->_M_invalidate_all();
224       }
226       using _Base::empty;
228       // element access:
229       reference
230       operator[](size_type __n)
231       {
232         __glibcxx_check_subscript(__n);
233         return _M_base()[__n];
234       }
236       const_reference
237       operator[](size_type __n) const
238       {
239         __glibcxx_check_subscript(__n);
240         return _M_base()[__n];
241       }
243       using _Base::at;
245       reference
246       front()
247       {
248         __glibcxx_check_nonempty();
249         return _Base::front();
250       }
252       const_reference
253       front() const
254       {
255         __glibcxx_check_nonempty();
256         return _Base::front();
257       }
259       reference
260       back()
261       {
262         __glibcxx_check_nonempty();
263         return _Base::back();
264       }
266       const_reference
267       back() const
268       {
269         __glibcxx_check_nonempty();
270         return _Base::back();
271       }
273       // 23.2.1.3 modifiers:
274       void
275       push_front(const _Tp& __x)
276       {
277         _Base::push_front(__x);
278         this->_M_invalidate_all();
279       }
281       void
282       push_back(const _Tp& __x)
283       {
284         _Base::push_back(__x);
285         this->_M_invalidate_all();
286       }
288 #ifdef __GXX_EXPERIMENTAL_CXX0X__
289       void
290       push_front(_Tp&& __x)
291       { emplace_front(std::move(__x)); }
293       void
294       push_back(_Tp&& __x)
295       { emplace_back(std::move(__x)); }
297       template<typename... _Args>
298         void
299         emplace_front(_Args&&... __args)
300         {
301           _Base::emplace_front(std::forward<_Args>(__args)...);
302           this->_M_invalidate_all();
303         }
305       template<typename... _Args>
306         void
307         emplace_back(_Args&&... __args)
308         {
309           _Base::emplace_back(std::forward<_Args>(__args)...);
310           this->_M_invalidate_all();
311         }
313       template<typename... _Args>
314         iterator
315         emplace(iterator __position, _Args&&... __args)
316         {
317           __glibcxx_check_insert(__position);
318           typename _Base::iterator __res = _Base::emplace(__position.base(),
319                                             std::forward<_Args>(__args)...);
320           this->_M_invalidate_all();
321           return iterator(__res, this);
322         }
323 #endif
325       iterator
326       insert(iterator __position, const _Tp& __x)
327       {
328         __glibcxx_check_insert(__position);
329         typename _Base::iterator __res = _Base::insert(__position.base(), __x);
330         this->_M_invalidate_all();
331         return iterator(__res, this);
332       }
334 #ifdef __GXX_EXPERIMENTAL_CXX0X__
335       iterator
336       insert(iterator __position, _Tp&& __x)
337       { return emplace(__position, std::move(__x)); }
339       void
340       insert(iterator __p, initializer_list<value_type> __l)
341       {
342         _Base::insert(__p, __l);
343         this->_M_invalidate_all();
344       }
345 #endif
347       void
348       insert(iterator __position, size_type __n, const _Tp& __x)
349       {
350         __glibcxx_check_insert(__position);
351         _Base::insert(__position.base(), __n, __x);
352         this->_M_invalidate_all();
353       }
355       template<class _InputIterator>
356         void
357         insert(iterator __position,
358                _InputIterator __first, _InputIterator __last)
359         {
360           __glibcxx_check_insert_range(__position, __first, __last);
361           _Base::insert(__position.base(), __first, __last);
362           this->_M_invalidate_all();
363         }
365       void
366       pop_front()
367       {
368         __glibcxx_check_nonempty();
369         iterator __victim = begin();
370         __victim._M_invalidate();
371         _Base::pop_front();
372       }
374       void
375       pop_back()
376       {
377         __glibcxx_check_nonempty();
378         iterator __victim = end();
379         --__victim;
380         __victim._M_invalidate();
381         _Base::pop_back();
382       }
384       iterator
385       erase(iterator __position)
386       {
387         __glibcxx_check_erase(__position);
388         if (__position == begin() || __position == end()-1)
389           {
390             __position._M_invalidate();
391             return iterator(_Base::erase(__position.base()), this);
392           }
393         else
394           {
395             typename _Base::iterator __res = _Base::erase(__position.base());
396             this->_M_invalidate_all();
397             return iterator(__res, this);
398           }
399       }
401       iterator
402       erase(iterator __first, iterator __last)
403       {
404         // _GLIBCXX_RESOLVE_LIB_DEFECTS
405         // 151. can't currently clear() empty container
406         __glibcxx_check_erase_range(__first, __last);
407         if (__first == begin() || __last == end())
408           {
409             this->_M_detach_singular();
410             for (iterator __position = __first; __position != __last; )
411               {
412                 iterator __victim = __position++;
413                 __victim._M_invalidate();
414               }
415             __try
416               {
417                 return iterator(_Base::erase(__first.base(), __last.base()),
418                                 this);
419               }
420             __catch(...)
421               {
422                 this->_M_revalidate_singular();
423                 __throw_exception_again;
424               }
425           }
426         else
427           {
428             typename _Base::iterator __res = _Base::erase(__first.base(),
429                                                           __last.base());
430             this->_M_invalidate_all();
431             return iterator(__res, this);
432           }
433       }
435       void
436       swap(deque& __x)
437       {
438         _Base::swap(__x);
439         this->_M_swap(__x);
440       }
442       void
443       clear()
444       {
445         _Base::clear();
446         this->_M_invalidate_all();
447       }
449       _Base&
450       _M_base()       { return *this; }
452       const _Base&
453       _M_base() const { return *this; }
454     };
456   template<typename _Tp, typename _Alloc>
457     inline bool
458     operator==(const deque<_Tp, _Alloc>& __lhs,
459                const deque<_Tp, _Alloc>& __rhs)
460     { return __lhs._M_base() == __rhs._M_base(); }
462   template<typename _Tp, typename _Alloc>
463     inline bool
464     operator!=(const deque<_Tp, _Alloc>& __lhs,
465                const deque<_Tp, _Alloc>& __rhs)
466     { return __lhs._M_base() != __rhs._M_base(); }
468   template<typename _Tp, typename _Alloc>
469     inline bool
470     operator<(const deque<_Tp, _Alloc>& __lhs,
471               const deque<_Tp, _Alloc>& __rhs)
472     { return __lhs._M_base() < __rhs._M_base(); }
474   template<typename _Tp, typename _Alloc>
475     inline bool
476     operator<=(const deque<_Tp, _Alloc>& __lhs,
477                const deque<_Tp, _Alloc>& __rhs)
478     { return __lhs._M_base() <= __rhs._M_base(); }
480   template<typename _Tp, typename _Alloc>
481     inline bool
482     operator>=(const deque<_Tp, _Alloc>& __lhs,
483                const deque<_Tp, _Alloc>& __rhs)
484     { return __lhs._M_base() >= __rhs._M_base(); }
486   template<typename _Tp, typename _Alloc>
487     inline bool
488     operator>(const deque<_Tp, _Alloc>& __lhs,
489               const deque<_Tp, _Alloc>& __rhs)
490     { return __lhs._M_base() > __rhs._M_base(); }
492   template<typename _Tp, typename _Alloc>
493     inline void
494     swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
495     { __lhs.swap(__rhs); }
497 } // namespace __debug
498 } // namespace std
500 #endif