]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/libc++/include/stack
Merged libcxxrt and libc++. Now available for testing on 9-stable with
[FreeBSD/stable/9.git] / contrib / libc++ / include / stack
1 // -*- C++ -*-
2 //===---------------------------- stack -----------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_STACK
12 #define _LIBCPP_STACK
13
14 /*
15     stack synopsis
16
17 namespace std
18 {
19
20 template <class T, class Container = deque<T>>
21 class stack
22 {
23 public:
24     typedef Container                                container_type;
25     typedef typename container_type::value_type      value_type;
26     typedef typename container_type::reference       reference;
27     typedef typename container_type::const_reference const_reference;
28     typedef typename container_type::size_type       size_type;
29
30 protected:
31     container_type c;
32
33 public:
34     stack() = default;
35     ~stack() = default;
36
37     stack(const stack& q) = default;
38     stack(stack&& q) = default;
39
40     stack& operator=(const stack& q) = default;
41     stack& operator=(stack&& q) = default;
42
43     explicit stack(const container_type& c);
44     explicit stack(container_type&& c);
45     template <class Alloc> explicit stack(const Alloc& a);
46     template <class Alloc> stack(const container_type& c, const Alloc& a);
47     template <class Alloc> stack(container_type&& c, const Alloc& a);
48     template <class Alloc> stack(const stack& c, const Alloc& a);
49     template <class Alloc> stack(stack&& c, const Alloc& a);
50
51     bool empty() const;
52     size_type size() const;
53     reference top();
54     const_reference top() const;
55
56     void push(const value_type& x);
57     void push(value_type&& x);
58     template <class... Args> void emplace(Args&&... args);
59     void pop();
60
61     void swap(stack& c) noexcept(noexcept(swap(c, q.c)));
62 };
63
64 template <class T, class Container>
65   bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
66 template <class T, class Container>
67   bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
68 template <class T, class Container>
69   bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
70 template <class T, class Container>
71   bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
72 template <class T, class Container>
73   bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
74 template <class T, class Container>
75   bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
76
77 template <class T, class Container>
78   void swap(stack<T, Container>& x, stack<T, Container>& y)
79   noexcept(noexcept(x.swap(y)));
80
81 }  // std
82
83 */
84
85 #include <__config>
86 #include <deque>
87
88 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
89 #pragma GCC system_header
90 #endif
91
92 _LIBCPP_BEGIN_NAMESPACE_STD
93
94 template <class _Tp, class _Container> class stack;
95
96 template <class _Tp, class _Container>
97 bool
98 operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
99
100 template <class _Tp, class _Container>
101 bool
102 operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
103
104 template <class _Tp, class _Container = deque<_Tp> >
105 class _LIBCPP_VISIBLE stack
106 {
107 public:
108     typedef _Container                               container_type;
109     typedef typename container_type::value_type      value_type;
110     typedef typename container_type::reference       reference;
111     typedef typename container_type::const_reference const_reference;
112     typedef typename container_type::size_type       size_type;
113
114 protected:
115     container_type c;
116
117 public:
118     _LIBCPP_INLINE_VISIBILITY
119     stack()
120         _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
121         : c() {}
122
123     _LIBCPP_INLINE_VISIBILITY
124     stack(const stack& __q) : c(__q.c) {}
125
126 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
127     _LIBCPP_INLINE_VISIBILITY
128     stack(stack&& __q)
129         _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
130         : c(_VSTD::move(__q.c)) {}
131 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
132
133     _LIBCPP_INLINE_VISIBILITY
134     stack& operator=(const stack& __q) {c = __q.c; return *this;}
135
136 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
137     _LIBCPP_INLINE_VISIBILITY
138     stack& operator=(stack&& __q)
139         _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
140         {c = _VSTD::move(__q.c); return *this;}
141 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
142
143     _LIBCPP_INLINE_VISIBILITY
144     explicit stack(const container_type& __c) : c(__c) {}
145 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
146     _LIBCPP_INLINE_VISIBILITY
147     explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
148 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
149     template <class _Alloc>
150         _LIBCPP_INLINE_VISIBILITY
151         explicit stack(const _Alloc& __a,
152                        typename enable_if<uses_allocator<container_type,
153                                                          _Alloc>::value>::type* = 0)
154             : c(__a) {}
155     template <class _Alloc>
156         _LIBCPP_INLINE_VISIBILITY
157         stack(const container_type& __c, const _Alloc& __a,
158               typename enable_if<uses_allocator<container_type,
159                                                 _Alloc>::value>::type* = 0)
160             : c(__c, __a) {}
161     template <class _Alloc>
162         _LIBCPP_INLINE_VISIBILITY
163         stack(const stack& __s, const _Alloc& __a,
164               typename enable_if<uses_allocator<container_type,
165                                                 _Alloc>::value>::type* = 0)
166             : c(__s.c, __a) {}
167 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
168     template <class _Alloc>
169         _LIBCPP_INLINE_VISIBILITY
170         stack(container_type&& __c, const _Alloc& __a,
171               typename enable_if<uses_allocator<container_type,
172                                                 _Alloc>::value>::type* = 0)
173             : c(_VSTD::move(__c), __a) {}
174     template <class _Alloc>
175         _LIBCPP_INLINE_VISIBILITY
176         stack(stack&& __s, const _Alloc& __a,
177               typename enable_if<uses_allocator<container_type,
178                                                 _Alloc>::value>::type* = 0)
179             : c(_VSTD::move(__s.c), __a) {}
180 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
181
182     _LIBCPP_INLINE_VISIBILITY
183     bool empty()     const      {return c.empty();}
184     _LIBCPP_INLINE_VISIBILITY
185     size_type size() const      {return c.size();}
186     _LIBCPP_INLINE_VISIBILITY
187     reference top()             {return c.back();}
188     _LIBCPP_INLINE_VISIBILITY
189     const_reference top() const {return c.back();}
190
191     _LIBCPP_INLINE_VISIBILITY
192     void push(const value_type& __v) {c.push_back(__v);}
193 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
194     _LIBCPP_INLINE_VISIBILITY
195     void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
196 #ifndef _LIBCPP_HAS_NO_VARIADICS
197     template <class... _Args>
198         _LIBCPP_INLINE_VISIBILITY
199         void emplace(_Args&&... __args)
200         {c.emplace_back(_VSTD::forward<_Args>(__args)...);}
201 #endif  // _LIBCPP_HAS_NO_VARIADICS
202 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
203     _LIBCPP_INLINE_VISIBILITY
204     void pop() {c.pop_back();}
205
206     _LIBCPP_INLINE_VISIBILITY
207     void swap(stack& __s)
208         _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
209     {
210         using _VSTD::swap;
211         swap(c, __s.c);
212     }
213
214     template <class T1, class _C1>
215     friend
216     bool
217     operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
218
219     template <class T1, class _C1>
220     friend
221     bool
222     operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
223 };
224
225 template <class _Tp, class _Container>
226 inline _LIBCPP_INLINE_VISIBILITY
227 bool
228 operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
229 {
230     return __x.c == __y.c;
231 }
232
233 template <class _Tp, class _Container>
234 inline _LIBCPP_INLINE_VISIBILITY
235 bool
236 operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
237 {
238     return __x.c < __y.c;
239 }
240
241 template <class _Tp, class _Container>
242 inline _LIBCPP_INLINE_VISIBILITY
243 bool
244 operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
245 {
246     return !(__x == __y);
247 }
248
249 template <class _Tp, class _Container>
250 inline _LIBCPP_INLINE_VISIBILITY
251 bool
252 operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
253 {
254     return __y < __x;
255 }
256
257 template <class _Tp, class _Container>
258 inline _LIBCPP_INLINE_VISIBILITY
259 bool
260 operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
261 {
262     return !(__x < __y);
263 }
264
265 template <class _Tp, class _Container>
266 inline _LIBCPP_INLINE_VISIBILITY
267 bool
268 operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
269 {
270     return !(__y < __x);
271 }
272
273 template <class _Tp, class _Container>
274 inline _LIBCPP_INLINE_VISIBILITY
275 void
276 swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
277     _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
278 {
279     __x.swap(__y);
280 }
281
282 template <class _Tp, class _Container, class _Alloc>
283 struct _LIBCPP_VISIBLE uses_allocator<stack<_Tp, _Container>, _Alloc>
284     : public uses_allocator<_Container, _Alloc>
285 {
286 };
287
288 _LIBCPP_END_NAMESPACE_STD
289
290 #endif  // _LIBCPP_STACK