xref: /llvm-project-15.0.7/libcxx/include/stack (revision 4a47ac7d)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STACK
11#define _LIBCPP_STACK
12
13/*
14    stack synopsis
15
16namespace std
17{
18
19template <class T, class Container = deque<T>>
20class stack
21{
22public:
23    typedef Container                                container_type;
24    typedef typename container_type::value_type      value_type;
25    typedef typename container_type::reference       reference;
26    typedef typename container_type::const_reference const_reference;
27    typedef typename container_type::size_type       size_type;
28
29protected:
30    container_type c;
31
32public:
33    stack() = default;
34    ~stack() = default;
35
36    stack(const stack& q) = default;
37    stack(stack&& q) = default;
38
39    stack& operator=(const stack& q) = default;
40    stack& operator=(stack&& q) = default;
41
42    explicit stack(const container_type& c);
43    explicit stack(container_type&& c);
44    template <class Alloc> explicit stack(const Alloc& a);
45    template <class Alloc> stack(const container_type& c, const Alloc& a);
46    template <class Alloc> stack(container_type&& c, const Alloc& a);
47    template <class Alloc> stack(const stack& c, const Alloc& a);
48    template <class Alloc> stack(stack&& c, const Alloc& a);
49
50    bool empty() const;
51    size_type size() const;
52    reference top();
53    const_reference top() const;
54
55    void push(const value_type& x);
56    void push(value_type&& x);
57    template <class... Args> reference emplace(Args&&... args); // reference in C++17
58    void pop();
59
60    void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
61};
62
63template<class Container>
64  stack(Container) -> stack<typename Container::value_type, Container>;  // C++17
65
66template<class Container, class Allocator>
67  stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
68
69template <class T, class Container>
70  bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
71template <class T, class Container>
72  bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
73template <class T, class Container>
74  bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
75template <class T, class Container>
76  bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
77template <class T, class Container>
78  bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
79template <class T, class Container>
80  bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
81
82template <class T, class Container>
83  void swap(stack<T, Container>& x, stack<T, Container>& y)
84  noexcept(noexcept(x.swap(y)));
85
86}  // std
87
88*/
89
90#include <__config>
91#include <__memory/uses_allocator.h>
92#include <__utility/forward.h>
93#include <deque>
94#include <version>
95
96#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
97#pragma GCC system_header
98#endif
99
100_LIBCPP_BEGIN_NAMESPACE_STD
101
102template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
103
104template <class _Tp, class _Container>
105_LIBCPP_INLINE_VISIBILITY
106bool
107operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
108
109template <class _Tp, class _Container>
110_LIBCPP_INLINE_VISIBILITY
111bool
112operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
113
114template <class _Tp, class _Container /*= deque<_Tp>*/>
115class _LIBCPP_TEMPLATE_VIS stack
116{
117public:
118    typedef _Container                               container_type;
119    typedef typename container_type::value_type      value_type;
120    typedef typename container_type::reference       reference;
121    typedef typename container_type::const_reference const_reference;
122    typedef typename container_type::size_type       size_type;
123    static_assert((is_same<_Tp, value_type>::value), "" );
124
125protected:
126    container_type c;
127
128public:
129    _LIBCPP_INLINE_VISIBILITY
130    stack()
131        _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
132        : c() {}
133
134    _LIBCPP_INLINE_VISIBILITY
135    stack(const stack& __q) : c(__q.c) {}
136
137    _LIBCPP_INLINE_VISIBILITY
138    stack& operator=(const stack& __q) {c = __q.c; return *this;}
139
140
141#ifndef _LIBCPP_CXX03_LANG
142    _LIBCPP_INLINE_VISIBILITY
143    stack(stack&& __q)
144        _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
145        : c(_VSTD::move(__q.c)) {}
146
147    _LIBCPP_INLINE_VISIBILITY
148    stack& operator=(stack&& __q)
149        _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
150        {c = _VSTD::move(__q.c); return *this;}
151
152    _LIBCPP_INLINE_VISIBILITY
153    explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
154#endif // _LIBCPP_CXX03_LANG
155
156    _LIBCPP_INLINE_VISIBILITY
157    explicit stack(const container_type& __c) : c(__c) {}
158
159    template <class _Alloc>
160        _LIBCPP_INLINE_VISIBILITY
161        explicit stack(const _Alloc& __a,
162                       __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
163            : c(__a) {}
164    template <class _Alloc>
165        _LIBCPP_INLINE_VISIBILITY
166        stack(const container_type& __c, const _Alloc& __a,
167              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
168            : c(__c, __a) {}
169    template <class _Alloc>
170        _LIBCPP_INLINE_VISIBILITY
171        stack(const stack& __s, const _Alloc& __a,
172              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
173            : c(__s.c, __a) {}
174#ifndef _LIBCPP_CXX03_LANG
175    template <class _Alloc>
176        _LIBCPP_INLINE_VISIBILITY
177        stack(container_type&& __c, const _Alloc& __a,
178              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
179            : c(_VSTD::move(__c), __a) {}
180    template <class _Alloc>
181        _LIBCPP_INLINE_VISIBILITY
182        stack(stack&& __s, const _Alloc& __a,
183              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
184            : c(_VSTD::move(__s.c), __a) {}
185#endif // _LIBCPP_CXX03_LANG
186
187    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
188    bool empty()     const      {return c.empty();}
189    _LIBCPP_INLINE_VISIBILITY
190    size_type size() const      {return c.size();}
191    _LIBCPP_INLINE_VISIBILITY
192    reference top()             {return c.back();}
193    _LIBCPP_INLINE_VISIBILITY
194    const_reference top() const {return c.back();}
195
196    _LIBCPP_INLINE_VISIBILITY
197    void push(const value_type& __v) {c.push_back(__v);}
198#ifndef _LIBCPP_CXX03_LANG
199    _LIBCPP_INLINE_VISIBILITY
200    void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
201
202    template <class... _Args>
203        _LIBCPP_INLINE_VISIBILITY
204#if _LIBCPP_STD_VER > 14
205        decltype(auto) emplace(_Args&&... __args)
206        { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
207#else
208        void      emplace(_Args&&... __args)
209        {        c.emplace_back(_VSTD::forward<_Args>(__args)...);}
210#endif
211#endif // _LIBCPP_CXX03_LANG
212
213    _LIBCPP_INLINE_VISIBILITY
214    void pop() {c.pop_back();}
215
216    _LIBCPP_INLINE_VISIBILITY
217    void swap(stack& __s)
218        _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
219    {
220        using _VSTD::swap;
221        swap(c, __s.c);
222    }
223
224    template <class T1, class _C1>
225    friend
226    bool
227    operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
228
229    template <class T1, class _C1>
230    friend
231    bool
232    operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
233};
234
235#if _LIBCPP_STD_VER >= 17
236template<class _Container,
237         class = enable_if_t<!__is_allocator<_Container>::value>
238>
239stack(_Container)
240    -> stack<typename _Container::value_type, _Container>;
241
242template<class _Container,
243         class _Alloc,
244         class = enable_if_t<!__is_allocator<_Container>::value>,
245         class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
246         >
247stack(_Container, _Alloc)
248    -> stack<typename _Container::value_type, _Container>;
249#endif
250
251template <class _Tp, class _Container>
252inline _LIBCPP_INLINE_VISIBILITY
253bool
254operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
255{
256    return __x.c == __y.c;
257}
258
259template <class _Tp, class _Container>
260inline _LIBCPP_INLINE_VISIBILITY
261bool
262operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
263{
264    return __x.c < __y.c;
265}
266
267template <class _Tp, class _Container>
268inline _LIBCPP_INLINE_VISIBILITY
269bool
270operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
271{
272    return !(__x == __y);
273}
274
275template <class _Tp, class _Container>
276inline _LIBCPP_INLINE_VISIBILITY
277bool
278operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
279{
280    return __y < __x;
281}
282
283template <class _Tp, class _Container>
284inline _LIBCPP_INLINE_VISIBILITY
285bool
286operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
287{
288    return !(__x < __y);
289}
290
291template <class _Tp, class _Container>
292inline _LIBCPP_INLINE_VISIBILITY
293bool
294operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
295{
296    return !(__y < __x);
297}
298
299template <class _Tp, class _Container>
300inline _LIBCPP_INLINE_VISIBILITY
301__enable_if_t<__is_swappable<_Container>::value, void>
302swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
303    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
304{
305    __x.swap(__y);
306}
307
308template <class _Tp, class _Container, class _Alloc>
309struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
310    : public uses_allocator<_Container, _Alloc>
311{
312};
313
314_LIBCPP_END_NAMESPACE_STD
315
316#endif // _LIBCPP_STACK
317