blob: c247d86457f04c1186acc19d550a2f81ee8a9c16 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- stack -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_STACK
12#define _LIBCPP_STACK
13
14/*
15 stack synopsis
16
17namespace std
18{
19
20template <class T, class Container = deque<T>>
21class stack
22{
23public:
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
30protected:
31 container_type c;
32
33public:
34 explicit stack();
35 explicit stack(const container_type& c);
36 explicit stack(container_type&& c);
37 stack(stack&& s);
38 stack& operator=(stack&& s);
39 template <class Alloc> explicit stack(const Alloc& a);
40 template <class Alloc> stack(const container_type& c, const Alloc& a);
41 template <class Alloc> stack(container_type&& c, const Alloc& a);
42 template <class Alloc> stack(stack&& c, const Alloc& a);
43
44 bool empty() const;
45 size_type size() const;
46 reference top();
47 const_reference top() const;
48
49 void push(const value_type& x);
50 void push(value_type&& x);
51 template <class... Args> void emplace(Args&&... args);
52 void pop();
53
54 void swap(stack& c);
55};
56
57template <class T, class Container>
58 bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
59template <class T, class Container>
60 bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
61template <class T, class Container>
62 bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
63template <class T, class Container>
64 bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
65template <class T, class Container>
66 bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
67template <class T, class Container>
68 bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
69
70template <class T, class Container>
71 void swap(stack<T, Container>& x, stack<T, Container>& y);
72
73} // std
74
75*/
76
77#include <__config>
78#include <deque>
79
80#pragma GCC system_header
81
82_LIBCPP_BEGIN_NAMESPACE_STD
83
84template <class _Tp, class _Container> class stack;
85
86template <class _Tp, class _Container>
87bool
88operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
89
90template <class _Tp, class _Container>
91bool
92operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
93
94template <class _Tp, class _Container = deque<_Tp> >
Howard Hinnant8d7a9552010-09-23 17:31:07 +000095class _LIBCPP_VISIBLE stack
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096{
97public:
98 typedef _Container container_type;
99 typedef typename container_type::value_type value_type;
100 typedef typename container_type::reference reference;
101 typedef typename container_type::const_reference const_reference;
102 typedef typename container_type::size_type size_type;
103
104protected:
105 container_type c;
106
107public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109 stack() : c() {}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000111 explicit stack(const container_type& __c) : c(__c) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000112#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114 explicit stack(container_type&& __c) : c(_STD::move(__c)) {}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116 stack(stack&& __s) : c(_STD::move(__s.c)) {}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118 stack& operator=(stack&& __s) {c = _STD::move(__s.c); return *this;}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000119#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122 explicit stack(const _Alloc& __a,
123 typename enable_if<uses_allocator<container_type,
124 _Alloc>::value>::type* = 0)
125 : c(__a) {}
126 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128 stack(const container_type& __c, const _Alloc& __a,
129 typename enable_if<uses_allocator<container_type,
130 _Alloc>::value>::type* = 0)
131 : c(__c, __a) {}
132 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000134 stack(const stack& __s, const _Alloc& __a,
135 typename enable_if<uses_allocator<container_type,
136 _Alloc>::value>::type* = 0)
137 : c(__s.c, __a) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000138#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141 stack(container_type&& __c, const _Alloc& __a,
142 typename enable_if<uses_allocator<container_type,
143 _Alloc>::value>::type* = 0)
144 : c(_STD::move(__c), __a) {}
145 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147 stack(stack&& __s, const _Alloc& __a,
148 typename enable_if<uses_allocator<container_type,
149 _Alloc>::value>::type* = 0)
150 : c(_STD::move(__s.c), __a) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000151#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000154 bool empty() const {return c.empty();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000156 size_type size() const {return c.size();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000158 reference top() {return c.back();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 const_reference top() const {return c.back();}
161
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163 void push(const value_type& __v) {c.push_back(__v);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000164#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000166 void push(value_type&& __v) {c.push_back(_STD::move(__v));}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000167#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000168 template <class... _Args>
169 _LIBCPP_INLINE_VISIBILITY
170 void emplace(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171 {c.emplace_back(_STD::forward<_Args>(__args)...);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000172#endif // _LIBCPP_HAS_NO_VARIADICS
173#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000175 void pop() {c.pop_back();}
176
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000178 void swap(stack& __s)
179 {
180 using _STD::swap;
181 swap(c, __s.c);
182 }
183
184 template <class T1, class _C1>
185 friend
186 bool
187 operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +0000188
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189 template <class T1, class _C1>
190 friend
191 bool
192 operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
193};
194
195template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000196inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197bool
198operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
199{
200 return __x.c == __y.c;
201}
202
203template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205bool
206operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
207{
208 return __x.c < __y.c;
209}
210
211template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000212inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213bool
214operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
215{
216 return !(__x == __y);
217}
218
219template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221bool
222operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
223{
224 return __y < __x;
225}
226
227template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229bool
230operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
231{
232 return !(__x < __y);
233}
234
235template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237bool
238operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
239{
240 return !(__y < __x);
241}
242
243template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245void
246swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
247{
248 __x.swap(__y);
249}
250
251template <class _Tp, class _Container, class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000252struct _LIBCPP_VISIBLE uses_allocator<stack<_Tp, _Container>, _Alloc>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253 : public uses_allocator<_Container, _Alloc>
254{
255};
256
257_LIBCPP_END_NAMESPACE_STD
258
259#endif // _LIBCPP_STACK