1// -*- C++ -*- 2//===---------------------------- stack -----------------------------------===// 3// 4// The LLVM Compiler Infrastructure 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 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 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 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 173template <class _Tp, class _Container> 174inline 175bool 176operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 177{ 178 return __x.c == __y.c; 179} 180 181template <class _Tp, class _Container> 182inline 183bool 184operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 185{ 186 return __x.c < __y.c; 187} 188 189template <class _Tp, class _Container> 190inline 191bool 192operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 193{ 194 return !(__x == __y); 195} 196 197template <class _Tp, class _Container> 198inline 199bool 200operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 201{ 202 return __y < __x; 203} 204 205template <class _Tp, class _Container> 206inline 207bool 208operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 209{ 210 return !(__x < __y); 211} 212 213template <class _Tp, class _Container> 214inline 215bool 216operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 217{ 218 return !(__y < __x); 219} 220 221template <class _Tp, class _Container> 222inline 223void 224swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y) 225{ 226 __x.swap(__y); 227} 228 229template <class _Tp, class _Container, class _Alloc> 230struct 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 238