xref: /llvm-project-15.0.7/libcxx/include/stack (revision 0ab5e2cd)
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
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 _LIBCPP_VISIBLE 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    _LIBCPP_INLINE_VISIBILITY
109    stack() : c() {}
110    _LIBCPP_INLINE_VISIBILITY
111    explicit stack(const container_type& __c) : c(__c) {}
112#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
113    _LIBCPP_INLINE_VISIBILITY
114    explicit stack(container_type&& __c) : c(_STD::move(__c)) {}
115    _LIBCPP_INLINE_VISIBILITY
116    stack(stack&& __s) : c(_STD::move(__s.c)) {}
117    _LIBCPP_INLINE_VISIBILITY
118    stack& operator=(stack&& __s) {c = _STD::move(__s.c); return *this;}
119#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
120    template <class _Alloc>
121        _LIBCPP_INLINE_VISIBILITY
122        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>
127        _LIBCPP_INLINE_VISIBILITY
128        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>
133        _LIBCPP_INLINE_VISIBILITY
134        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) {}
138#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
139    template <class _Alloc>
140        _LIBCPP_INLINE_VISIBILITY
141        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>
146        _LIBCPP_INLINE_VISIBILITY
147        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) {}
151#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
152
153    _LIBCPP_INLINE_VISIBILITY
154    bool empty()     const      {return c.empty();}
155    _LIBCPP_INLINE_VISIBILITY
156    size_type size() const      {return c.size();}
157    _LIBCPP_INLINE_VISIBILITY
158    reference top()             {return c.back();}
159    _LIBCPP_INLINE_VISIBILITY
160    const_reference top() const {return c.back();}
161
162    _LIBCPP_INLINE_VISIBILITY
163    void push(const value_type& __v) {c.push_back(__v);}
164#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
165    _LIBCPP_INLINE_VISIBILITY
166    void push(value_type&& __v) {c.push_back(_STD::move(__v));}
167#ifndef _LIBCPP_HAS_NO_VARIADICS
168    template <class... _Args>
169        _LIBCPP_INLINE_VISIBILITY
170        void emplace(_Args&&... __args)
171        {c.emplace_back(_STD::forward<_Args>(__args)...);}
172#endif  // _LIBCPP_HAS_NO_VARIADICS
173#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
174    _LIBCPP_INLINE_VISIBILITY
175    void pop() {c.pop_back();}
176
177    _LIBCPP_INLINE_VISIBILITY
178    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);
188
189    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>
196inline _LIBCPP_INLINE_VISIBILITY
197bool
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>
204inline _LIBCPP_INLINE_VISIBILITY
205bool
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>
212inline _LIBCPP_INLINE_VISIBILITY
213bool
214operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
215{
216    return !(__x == __y);
217}
218
219template <class _Tp, class _Container>
220inline _LIBCPP_INLINE_VISIBILITY
221bool
222operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
223{
224    return __y < __x;
225}
226
227template <class _Tp, class _Container>
228inline _LIBCPP_INLINE_VISIBILITY
229bool
230operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
231{
232    return !(__x < __y);
233}
234
235template <class _Tp, class _Container>
236inline _LIBCPP_INLINE_VISIBILITY
237bool
238operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
239{
240    return !(__y < __x);
241}
242
243template <class _Tp, class _Container>
244inline _LIBCPP_INLINE_VISIBILITY
245void
246swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
247{
248    __x.swap(__y);
249}
250
251template <class _Tp, class _Container, class _Alloc>
252struct _LIBCPP_VISIBLE uses_allocator<stack<_Tp, _Container>, _Alloc>
253    : public uses_allocator<_Container, _Alloc>
254{
255};
256
257_LIBCPP_END_NAMESPACE_STD
258
259#endif  // _LIBCPP_STACK
260