[45147a4c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_STACK
|
---|
| 30 | #define LIBCPP_STACK
|
---|
| 31 |
|
---|
| 32 | #include <initializer_list>
|
---|
| 33 | #include <utility>
|
---|
| 34 | #include <vector>
|
---|
| 35 |
|
---|
| 36 | namespace std
|
---|
| 37 | {
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * 23.5.6.2, stack:
|
---|
| 41 | */
|
---|
| 42 |
|
---|
| 43 | // TODO: the default container should be deque<T>
|
---|
| 44 | template<class T, class Container = vector<T>>
|
---|
| 45 | class stack
|
---|
| 46 | {
|
---|
| 47 | public:
|
---|
| 48 | using container_type = Container;
|
---|
| 49 | using value_type = typename container_type::value_type;
|
---|
[de53138] | 50 | using reference = typename container_type::reference;
|
---|
[45147a4c] | 51 | using const_reference = typename container_type::const_reference;
|
---|
| 52 | using size_type = typename container_type::size_type;
|
---|
| 53 |
|
---|
| 54 | explicit stack(container_type& cont)
|
---|
| 55 | : c{cont}
|
---|
| 56 | { /* DUMMY BODY */ }
|
---|
| 57 |
|
---|
| 58 | explicit stack(container_type&& cont = container_type{})
|
---|
| 59 | : c{move(cont)}
|
---|
| 60 | { /* DUMMY BODY */ }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * TODO: The allocator constructor should use enable_if
|
---|
| 64 | * as a last parameter that checks if uses_allocator
|
---|
| 65 | * from <memory> holds.
|
---|
| 66 | */
|
---|
| 67 | template<class Alloc>
|
---|
| 68 | explicit stack(Alloc& alloc)
|
---|
| 69 | : c{alloc}
|
---|
| 70 | { /* DUMMY BODY */ }
|
---|
| 71 |
|
---|
| 72 | template<class Alloc>
|
---|
| 73 | stack(const container_type& cont, const Alloc& alloc)
|
---|
| 74 | : c{cont, alloc}
|
---|
| 75 | { /* DUMMY BODY */ }
|
---|
| 76 |
|
---|
| 77 | template<class Alloc>
|
---|
| 78 | stack(container_type&& cont, const Alloc& alloc)
|
---|
| 79 | : c{move(cont), alloc}
|
---|
| 80 | { /* DUMMY BODY */ }
|
---|
| 81 |
|
---|
| 82 | template<class Alloc>
|
---|
| 83 | stack(const stack& other, const Alloc& alloc)
|
---|
| 84 | : c{other.c, alloc}
|
---|
| 85 | { /* DUMMY BODY */ }
|
---|
| 86 |
|
---|
| 87 | template<class Alloc>
|
---|
| 88 | stack(stack&& other, const Alloc& alloc)
|
---|
| 89 | : c{move(other.c), alloc}
|
---|
| 90 | { /* DUMMY BODY */ }
|
---|
| 91 |
|
---|
| 92 | bool empty()
|
---|
| 93 | {
|
---|
| 94 | return c.empty();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | size_type size()
|
---|
| 98 | {
|
---|
| 99 | return c.size();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | reference top()
|
---|
| 103 | {
|
---|
| 104 | return c.back();
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | const_reference top() const
|
---|
| 108 | {
|
---|
| 109 | return c.back();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | void push(const value_type& val)
|
---|
| 113 | {
|
---|
| 114 | c.push_back(val);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | void push(value_type&& val)
|
---|
| 118 | {
|
---|
| 119 | c.push_back(move(val));
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | template<class... Args>
|
---|
| 123 | void emplace(Args&&... args)
|
---|
| 124 | {
|
---|
| 125 | c.emplace_back(forward<Args>(args)...);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void pop()
|
---|
| 129 | {
|
---|
| 130 | c.pop_back();
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | void swap(stack& other)
|
---|
[de53138] | 134 | /* noexcept(noexcept(swap(c, other.c))) */
|
---|
[45147a4c] | 135 | {
|
---|
| 136 | std::swap(c, other.c);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | protected:
|
---|
| 140 | container_type c;
|
---|
| 141 | };
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * 23.6.5.5, stack operators:
|
---|
| 145 | */
|
---|
| 146 |
|
---|
| 147 | template<class T, class Container>
|
---|
| 148 | bool operator==(const stack<T, Container>& lhs,
|
---|
| 149 | const stack<T, Container>& rhs)
|
---|
| 150 | {
|
---|
| 151 | return lhs.c == rhs.c;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | template<class T, class Container>
|
---|
| 155 | bool operator!=(const stack<T, Container>& lhs,
|
---|
| 156 | const stack<T, Container>& rhs)
|
---|
| 157 | {
|
---|
| 158 | return lhs.c != rhs.c;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | template<class T, class Container>
|
---|
| 162 | bool operator<(const stack<T, Container>& lhs,
|
---|
| 163 | const stack<T, Container>& rhs)
|
---|
| 164 | {
|
---|
| 165 | return lhs.c < rhs.c;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | template<class T, class Container>
|
---|
| 169 | bool operator<=(const stack<T, Container>& lhs,
|
---|
| 170 | const stack<T, Container>& rhs)
|
---|
| 171 | {
|
---|
| 172 | return lhs.c <= rhs.c;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | template<class T, class Container>
|
---|
| 176 | bool operator>(const stack<T, Container>& lhs,
|
---|
| 177 | const stack<T, Container>& rhs)
|
---|
| 178 | {
|
---|
| 179 | return lhs.c > rhs.c;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | template<class T, class Container>
|
---|
| 183 | bool operator>=(const stack<T, Container>& lhs,
|
---|
| 184 | const stack<T, Container>& rhs)
|
---|
| 185 | {
|
---|
| 186 | return lhs.c >= rhs.c;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * 23.6.5.6, stack specialized algorithms:
|
---|
| 191 | */
|
---|
| 192 |
|
---|
| 193 | template<class T, class Container>
|
---|
| 194 | void swap(stack<T, Container>& lhs, stack<T, Container>& rhs)
|
---|
| 195 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
| 196 | {
|
---|
| 197 | lhs.swap(rhs);
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | #endif
|
---|