blob: 7737379547f3ce21a9456d4b90b833bc7169f134 [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//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
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> >
95class stack
96{
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:
108 stack() : c() {}
109 explicit stack(const container_type& __c) : c(__c) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000110#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000111 explicit stack(container_type&& __c) : c(_STD::move(__c)) {}
112 stack(stack&& __s) : c(_STD::move(__s.c)) {}
113 stack& operator=(stack&& __s) {c = _STD::move(__s.c); return *this;}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000114#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115 template <class _Alloc>
116 explicit stack(const _Alloc& __a,
117 typename enable_if<uses_allocator<container_type,
118 _Alloc>::value>::type* = 0)
119 : c(__a) {}
120 template <class _Alloc>
121 stack(const container_type& __c, const _Alloc& __a,
122 typename enable_if<uses_allocator<container_type,
123 _Alloc>::value>::type* = 0)
124 : c(__c, __a) {}
125 template <class _Alloc>
126 stack(const stack& __s, const _Alloc& __a,
127 typename enable_if<uses_allocator<container_type,
128 _Alloc>::value>::type* = 0)
129 : c(__s.c, __a) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000130#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131 template <class _Alloc>
132 stack(container_type&& __c, const _Alloc& __a,
133 typename enable_if<uses_allocator<container_type,
134 _Alloc>::value>::type* = 0)
135 : c(_STD::move(__c), __a) {}
136 template <class _Alloc>
137 stack(stack&& __s, const _Alloc& __a,
138 typename enable_if<uses_allocator<container_type,
139 _Alloc>::value>::type* = 0)
140 : c(_STD::move(__s.c), __a) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000141#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000142
143 bool empty() const {return c.empty();}
144 size_type size() const {return c.size();}
145 reference top() {return c.back();}
146 const_reference top() const {return c.back();}
147
148 void push(const value_type& __v) {c.push_back(__v);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000149#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150 void push(value_type&& __v) {c.push_back(_STD::move(__v));}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000151#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152 template <class... _Args> void emplace(_Args&&... __args)
153 {c.emplace_back(_STD::forward<_Args>(__args)...);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000154#endif // _LIBCPP_HAS_NO_VARIADICS
155#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000156 void pop() {c.pop_back();}
157
158 void swap(stack& __s)
159 {
160 using _STD::swap;
161 swap(c, __s.c);
162 }
163
164 template <class T1, class _C1>
165 friend
166 bool
167 operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +0000168
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 template <class T1, class _C1>
170 friend
171 bool
172 operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
173};
174
175template <class _Tp, class _Container>
176inline
177bool
178operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
179{
180 return __x.c == __y.c;
181}
182
183template <class _Tp, class _Container>
184inline
185bool
186operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
187{
188 return __x.c < __y.c;
189}
190
191template <class _Tp, class _Container>
192inline
193bool
194operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
195{
196 return !(__x == __y);
197}
198
199template <class _Tp, class _Container>
200inline
201bool
202operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
203{
204 return __y < __x;
205}
206
207template <class _Tp, class _Container>
208inline
209bool
210operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
211{
212 return !(__x < __y);
213}
214
215template <class _Tp, class _Container>
216inline
217bool
218operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
219{
220 return !(__y < __x);
221}
222
223template <class _Tp, class _Container>
224inline
225void
226swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
227{
228 __x.swap(__y);
229}
230
231template <class _Tp, class _Container, class _Alloc>
232struct uses_allocator<stack<_Tp, _Container>, _Alloc>
233 : public uses_allocator<_Container, _Alloc>
234{
235};
236
237_LIBCPP_END_NAMESPACE_STD
238
239#endif // _LIBCPP_STACK