| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #ifndef LIBCPP_BITS_ADT_STACK
|
|---|
| 8 | #define LIBCPP_BITS_ADT_STACK
|
|---|
| 9 |
|
|---|
| 10 | #include <deque>
|
|---|
| 11 | #include <initializer_list>
|
|---|
| 12 | #include <utility>
|
|---|
| 13 |
|
|---|
| 14 | namespace std
|
|---|
| 15 | {
|
|---|
| 16 | /**
|
|---|
| 17 | * 23.5.6.2, stack:
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | template<class T, class Container = deque<T>>
|
|---|
| 21 | class stack
|
|---|
| 22 | {
|
|---|
| 23 | public:
|
|---|
| 24 | using container_type = Container;
|
|---|
| 25 | using value_type = typename container_type::value_type;
|
|---|
| 26 | using reference = typename container_type::reference;
|
|---|
| 27 | using const_reference = typename container_type::const_reference;
|
|---|
| 28 | using size_type = typename container_type::size_type;
|
|---|
| 29 |
|
|---|
| 30 | explicit stack(container_type& cont)
|
|---|
| 31 | : c{cont}
|
|---|
| 32 | { /* DUMMY BODY */ }
|
|---|
| 33 |
|
|---|
| 34 | explicit stack(container_type&& cont = container_type{})
|
|---|
| 35 | : c{move(cont)}
|
|---|
| 36 | { /* DUMMY BODY */ }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * TODO: The allocator constructor should use enable_if
|
|---|
| 40 | * as a last parameter that checks if uses_allocator
|
|---|
| 41 | * from <memory> holds.
|
|---|
| 42 | */
|
|---|
| 43 | template<class Alloc>
|
|---|
| 44 | explicit stack(Alloc& alloc)
|
|---|
| 45 | : c{alloc}
|
|---|
| 46 | { /* DUMMY BODY */ }
|
|---|
| 47 |
|
|---|
| 48 | template<class Alloc>
|
|---|
| 49 | stack(const container_type& cont, const Alloc& alloc)
|
|---|
| 50 | : c{cont, alloc}
|
|---|
| 51 | { /* DUMMY BODY */ }
|
|---|
| 52 |
|
|---|
| 53 | template<class Alloc>
|
|---|
| 54 | stack(container_type&& cont, const Alloc& alloc)
|
|---|
| 55 | : c{move(cont), alloc}
|
|---|
| 56 | { /* DUMMY BODY */ }
|
|---|
| 57 |
|
|---|
| 58 | template<class Alloc>
|
|---|
| 59 | stack(const stack& other, const Alloc& alloc)
|
|---|
| 60 | : c{other.c, alloc}
|
|---|
| 61 | { /* DUMMY BODY */ }
|
|---|
| 62 |
|
|---|
| 63 | template<class Alloc>
|
|---|
| 64 | stack(stack&& other, const Alloc& alloc)
|
|---|
| 65 | : c{move(other.c), alloc}
|
|---|
| 66 | { /* DUMMY BODY */ }
|
|---|
| 67 |
|
|---|
| 68 | bool empty()
|
|---|
| 69 | {
|
|---|
| 70 | return c.empty();
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | size_type size()
|
|---|
| 74 | {
|
|---|
| 75 | return c.size();
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | reference top()
|
|---|
| 79 | {
|
|---|
| 80 | return c.back();
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | const_reference top() const
|
|---|
| 84 | {
|
|---|
| 85 | return c.back();
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void push(const value_type& val)
|
|---|
| 89 | {
|
|---|
| 90 | c.push_back(val);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void push(value_type&& val)
|
|---|
| 94 | {
|
|---|
| 95 | c.push_back(move(val));
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | template<class... Args>
|
|---|
| 99 | void emplace(Args&&... args)
|
|---|
| 100 | {
|
|---|
| 101 | c.emplace_back(forward<Args>(args)...);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void pop()
|
|---|
| 105 | {
|
|---|
| 106 | c.pop_back();
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void swap(stack& other) // We cannot use c in the noexcept :/
|
|---|
| 110 | noexcept(noexcept(declval<container_type>().swap(declval<container_type&>())))
|
|---|
| 111 | {
|
|---|
| 112 | std::swap(c, other.c);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | protected:
|
|---|
| 116 | container_type c;
|
|---|
| 117 | };
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * 23.6.5.5, stack operators:
|
|---|
| 121 | */
|
|---|
| 122 |
|
|---|
| 123 | template<class T, class Container>
|
|---|
| 124 | bool operator==(const stack<T, Container>& lhs,
|
|---|
| 125 | const stack<T, Container>& rhs)
|
|---|
| 126 | {
|
|---|
| 127 | return lhs.c == rhs.c;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | template<class T, class Container>
|
|---|
| 131 | bool operator!=(const stack<T, Container>& lhs,
|
|---|
| 132 | const stack<T, Container>& rhs)
|
|---|
| 133 | {
|
|---|
| 134 | return lhs.c != rhs.c;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | template<class T, class Container>
|
|---|
| 138 | bool operator<(const stack<T, Container>& lhs,
|
|---|
| 139 | const stack<T, Container>& rhs)
|
|---|
| 140 | {
|
|---|
| 141 | return lhs.c < rhs.c;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | template<class T, class Container>
|
|---|
| 145 | bool operator<=(const stack<T, Container>& lhs,
|
|---|
| 146 | const stack<T, Container>& rhs)
|
|---|
| 147 | {
|
|---|
| 148 | return lhs.c <= rhs.c;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | template<class T, class Container>
|
|---|
| 152 | bool operator>(const stack<T, Container>& lhs,
|
|---|
| 153 | const stack<T, Container>& rhs)
|
|---|
| 154 | {
|
|---|
| 155 | return lhs.c > rhs.c;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | template<class T, class Container>
|
|---|
| 159 | bool operator>=(const stack<T, Container>& lhs,
|
|---|
| 160 | const stack<T, Container>& rhs)
|
|---|
| 161 | {
|
|---|
| 162 | return lhs.c >= rhs.c;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /**
|
|---|
| 166 | * 23.6.5.6, stack specialized algorithms:
|
|---|
| 167 | */
|
|---|
| 168 |
|
|---|
| 169 | template<class T, class Container>
|
|---|
| 170 | void swap(stack<T, Container>& lhs, stack<T, Container>& rhs)
|
|---|
| 171 | noexcept(noexcept(lhs.swap(rhs)))
|
|---|
| 172 | {
|
|---|
| 173 | lhs.swap(rhs);
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | #endif
|
|---|