blob: c247d86457f04c1186acc19d550a2f81ee8a9c16 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- stack -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// 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:168//
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:0795class _LIBCPP_VISIBLE stack
Howard Hinnantbc8d3f92010-05-11 19:42:1696{
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:07108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16109 stack() : c() {}
Howard Hinnant8d7a9552010-09-23 17:31:07110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16111 explicit stack(const container_type& __c) : c(__c) {}
Howard Hinnant73d21a42010-09-04 23:28:19112#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16114 explicit stack(container_type&& __c) : c(_STD::move(__c)) {}
Howard Hinnant8d7a9552010-09-23 17:31:07115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16116 stack(stack&& __s) : c(_STD::move(__s.c)) {}
Howard Hinnant8d7a9552010-09-23 17:31:07117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16118 stack& operator=(stack&& __s) {c = _STD::move(__s.c); return *this;}
Howard Hinnant73d21a42010-09-04 23:28:19119#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16120 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16122 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:07127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16128 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:07133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16134 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:19138#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16139 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16141 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:07146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16147 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:19151#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16152
Howard Hinnant8d7a9552010-09-23 17:31:07153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16154 bool empty() const {return c.empty();}
Howard Hinnant8d7a9552010-09-23 17:31:07155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16156 size_type size() const {return c.size();}
Howard Hinnant8d7a9552010-09-23 17:31:07157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16158 reference top() {return c.back();}
Howard Hinnant8d7a9552010-09-23 17:31:07159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16160 const_reference top() const {return c.back();}
161
Howard Hinnant8d7a9552010-09-23 17:31:07162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16163 void push(const value_type& __v) {c.push_back(__v);}
Howard Hinnant73d21a42010-09-04 23:28:19164#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16166 void push(value_type&& __v) {c.push_back(_STD::move(__v));}
Howard Hinnant73d21a42010-09-04 23:28:19167#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant8d7a9552010-09-23 17:31:07168 template <class... _Args>
169 _LIBCPP_INLINE_VISIBILITY
170 void emplace(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16171 {c.emplace_back(_STD::forward<_Args>(__args)...);}
Howard Hinnant73d21a42010-09-04 23:28:19172#endif // _LIBCPP_HAS_NO_VARIADICS
173#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16175 void pop() {c.pop_back();}
176
Howard Hinnant8d7a9552010-09-23 17:31:07177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16178 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:43188
Howard Hinnantbc8d3f92010-05-11 19:42:16189 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:07196inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16197bool
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:07204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16205bool
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:07212inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16213bool
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:07220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16221bool
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:07228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16229bool
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:07236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16237bool
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:07244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16245void
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:07252struct _LIBCPP_VISIBLE uses_allocator<stack<_Tp, _Container>, _Alloc>
Howard Hinnantbc8d3f92010-05-11 19:42:16253 : public uses_allocator<_Container, _Alloc>
254{
255};
256
257_LIBCPP_END_NAMESPACE_STD
258
259#endif // _LIBCPP_STACK