Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- stack -----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame^] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_STACK |
| 12 | #define _LIBCPP_STACK |
| 13 | |
| 14 | /* |
| 15 | stack synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | template <class T, class Container = deque<T>> |
| 21 | class stack |
| 22 | { |
| 23 | public: |
| 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 | |
| 30 | protected: |
| 31 | container_type c; |
| 32 | |
| 33 | public: |
| 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 | |
| 57 | template <class T, class Container> |
| 58 | bool operator==(const stack<T, Container>& x, const stack<T, Container>& y); |
| 59 | template <class T, class Container> |
| 60 | bool operator< (const stack<T, Container>& x, const stack<T, Container>& y); |
| 61 | template <class T, class Container> |
| 62 | bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 63 | template <class T, class Container> |
| 64 | bool operator> (const stack<T, Container>& x, const stack<T, Container>& y); |
| 65 | template <class T, class Container> |
| 66 | bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 67 | template <class T, class Container> |
| 68 | bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 69 | |
| 70 | template <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 | |
| 84 | template <class _Tp, class _Container> class stack; |
| 85 | |
| 86 | template <class _Tp, class _Container> |
| 87 | bool |
| 88 | operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); |
| 89 | |
| 90 | template <class _Tp, class _Container> |
| 91 | bool |
| 92 | operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); |
| 93 | |
| 94 | template <class _Tp, class _Container = deque<_Tp> > |
| 95 | class stack |
| 96 | { |
| 97 | public: |
| 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 | |
| 104 | protected: |
| 105 | container_type c; |
| 106 | |
| 107 | public: |
| 108 | stack() : c() {} |
| 109 | explicit stack(const container_type& __c) : c(__c) {} |
| 110 | #ifdef _LIBCPP_MOVE |
| 111 | explicit stack(container_type&& __c) : c(_STD::move(__c)) {} |
| 112 | stack(stack&& __s) : c(_STD::move(__s.c)) {} |
| 113 | stack& operator=(stack&& __s) {c = _STD::move(__s.c); return *this;} |
| 114 | #endif |
| 115 | template <class _Alloc> |
| 116 | explicit stack(const _Alloc& __a, |
| 117 | typename enable_if<uses_allocator<container_type, |
| 118 | _Alloc>::value>::type* = 0) |
| 119 | : c(__a) {} |
| 120 | template <class _Alloc> |
| 121 | stack(const container_type& __c, const _Alloc& __a, |
| 122 | typename enable_if<uses_allocator<container_type, |
| 123 | _Alloc>::value>::type* = 0) |
| 124 | : c(__c, __a) {} |
| 125 | template <class _Alloc> |
| 126 | stack(const stack& __s, const _Alloc& __a, |
| 127 | typename enable_if<uses_allocator<container_type, |
| 128 | _Alloc>::value>::type* = 0) |
| 129 | : c(__s.c, __a) {} |
| 130 | #ifdef _LIBCPP_MOVE |
| 131 | template <class _Alloc> |
| 132 | stack(container_type&& __c, const _Alloc& __a, |
| 133 | typename enable_if<uses_allocator<container_type, |
| 134 | _Alloc>::value>::type* = 0) |
| 135 | : c(_STD::move(__c), __a) {} |
| 136 | template <class _Alloc> |
| 137 | stack(stack&& __s, const _Alloc& __a, |
| 138 | typename enable_if<uses_allocator<container_type, |
| 139 | _Alloc>::value>::type* = 0) |
| 140 | : c(_STD::move(__s.c), __a) {} |
| 141 | #endif |
| 142 | |
| 143 | bool empty() const {return c.empty();} |
| 144 | size_type size() const {return c.size();} |
| 145 | reference top() {return c.back();} |
| 146 | const_reference top() const {return c.back();} |
| 147 | |
| 148 | void push(const value_type& __v) {c.push_back(__v);} |
| 149 | #ifdef _LIBCPP_MOVE |
| 150 | void push(value_type&& __v) {c.push_back(_STD::move(__v));} |
| 151 | template <class... _Args> void emplace(_Args&&... __args) |
| 152 | {c.emplace_back(_STD::forward<_Args>(__args)...);} |
| 153 | #endif |
| 154 | void pop() {c.pop_back();} |
| 155 | |
| 156 | void swap(stack& __s) |
| 157 | { |
| 158 | using _STD::swap; |
| 159 | swap(c, __s.c); |
| 160 | } |
| 161 | |
| 162 | template <class T1, class _C1> |
| 163 | friend |
| 164 | bool |
| 165 | operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); |
| 166 | |
| 167 | template <class T1, class _C1> |
| 168 | friend |
| 169 | bool |
| 170 | operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); |
| 171 | }; |
| 172 | |
| 173 | template <class _Tp, class _Container> |
| 174 | inline |
| 175 | bool |
| 176 | operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 177 | { |
| 178 | return __x.c == __y.c; |
| 179 | } |
| 180 | |
| 181 | template <class _Tp, class _Container> |
| 182 | inline |
| 183 | bool |
| 184 | operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 185 | { |
| 186 | return __x.c < __y.c; |
| 187 | } |
| 188 | |
| 189 | template <class _Tp, class _Container> |
| 190 | inline |
| 191 | bool |
| 192 | operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 193 | { |
| 194 | return !(__x == __y); |
| 195 | } |
| 196 | |
| 197 | template <class _Tp, class _Container> |
| 198 | inline |
| 199 | bool |
| 200 | operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 201 | { |
| 202 | return __y < __x; |
| 203 | } |
| 204 | |
| 205 | template <class _Tp, class _Container> |
| 206 | inline |
| 207 | bool |
| 208 | operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 209 | { |
| 210 | return !(__x < __y); |
| 211 | } |
| 212 | |
| 213 | template <class _Tp, class _Container> |
| 214 | inline |
| 215 | bool |
| 216 | operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 217 | { |
| 218 | return !(__y < __x); |
| 219 | } |
| 220 | |
| 221 | template <class _Tp, class _Container> |
| 222 | inline |
| 223 | void |
| 224 | swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y) |
| 225 | { |
| 226 | __x.swap(__y); |
| 227 | } |
| 228 | |
| 229 | template <class _Tp, class _Container, class _Alloc> |
| 230 | struct uses_allocator<stack<_Tp, _Container>, _Alloc> |
| 231 | : public uses_allocator<_Container, _Alloc> |
| 232 | { |
| 233 | }; |
| 234 | |
| 235 | _LIBCPP_END_NAMESPACE_STD |
| 236 | |
| 237 | #endif // _LIBCPP_STACK |