|
libstdc++
|
00001 // Stack implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 00004 // 2010, 2011, 2012 00005 // Free Software Foundation, Inc. 00006 // 00007 // This file is part of the GNU ISO C++ Library. This library is free 00008 // software; you can redistribute it and/or modify it under the 00009 // terms of the GNU General Public License as published by the 00010 // Free Software Foundation; either version 3, or (at your option) 00011 // any later version. 00012 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 00018 // Under Section 7 of GPL version 3, you are granted additional 00019 // permissions described in the GCC Runtime Library Exception, version 00020 // 3.1, as published by the Free Software Foundation. 00021 00022 // You should have received a copy of the GNU General Public License and 00023 // a copy of the GCC Runtime Library Exception along with this program; 00024 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00025 // <http://www.gnu.org/licenses/>. 00026 00027 /* 00028 * 00029 * Copyright (c) 1994 00030 * Hewlett-Packard Company 00031 * 00032 * Permission to use, copy, modify, distribute and sell this software 00033 * and its documentation for any purpose is hereby granted without fee, 00034 * provided that the above copyright notice appear in all copies and 00035 * that both that copyright notice and this permission notice appear 00036 * in supporting documentation. Hewlett-Packard Company makes no 00037 * representations about the suitability of this software for any 00038 * purpose. It is provided "as is" without express or implied warranty. 00039 * 00040 * 00041 * Copyright (c) 1996,1997 00042 * Silicon Graphics Computer Systems, Inc. 00043 * 00044 * Permission to use, copy, modify, distribute and sell this software 00045 * and its documentation for any purpose is hereby granted without fee, 00046 * provided that the above copyright notice appear in all copies and 00047 * that both that copyright notice and this permission notice appear 00048 * in supporting documentation. Silicon Graphics makes no 00049 * representations about the suitability of this software for any 00050 * purpose. It is provided "as is" without express or implied warranty. 00051 */ 00052 00053 /** @file bits/stl_stack.h 00054 * This is an internal header file, included by other library headers. 00055 * Do not attempt to use it directly. @headername{stack} 00056 */ 00057 00058 #ifndef _STL_STACK_H 00059 #define _STL_STACK_H 1 00060 00061 #include <bits/concept_check.h> 00062 #include <debug/debug.h> 00063 00064 namespace std _GLIBCXX_VISIBILITY(default) 00065 { 00066 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00067 00068 /** 00069 * @brief A standard container giving FILO behavior. 00070 * 00071 * @ingroup sequences 00072 * 00073 * @tparam _Tp Type of element. 00074 * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>. 00075 * 00076 * Meets many of the requirements of a 00077 * <a href="tables.html#65">container</a>, 00078 * but does not define anything to do with iterators. Very few of the 00079 * other standard container interfaces are defined. 00080 * 00081 * This is not a true container, but an @e adaptor. It holds 00082 * another container, and provides a wrapper interface to that 00083 * container. The wrapper is what enforces strict 00084 * first-in-last-out %stack behavior. 00085 * 00086 * The second template parameter defines the type of the underlying 00087 * sequence/container. It defaults to std::deque, but it can be 00088 * any type that supports @c back, @c push_back, and @c pop_front, 00089 * such as std::list, std::vector, or an appropriate user-defined 00090 * type. 00091 * 00092 * Members not found in @a normal containers are @c container_type, 00093 * which is a typedef for the second Sequence parameter, and @c 00094 * push, @c pop, and @c top, which are standard %stack/FILO 00095 * operations. 00096 */ 00097 template<typename _Tp, typename _Sequence = deque<_Tp> > 00098 class stack 00099 { 00100 // concept requirements 00101 typedef typename _Sequence::value_type _Sequence_value_type; 00102 __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 00103 __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept) 00104 __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) 00105 00106 template<typename _Tp1, typename _Seq1> 00107 friend bool 00108 operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); 00109 00110 template<typename _Tp1, typename _Seq1> 00111 friend bool 00112 operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); 00113 00114 public: 00115 typedef typename _Sequence::value_type value_type; 00116 typedef typename _Sequence::reference reference; 00117 typedef typename _Sequence::const_reference const_reference; 00118 typedef typename _Sequence::size_type size_type; 00119 typedef _Sequence container_type; 00120 00121 protected: 00122 // See queue::c for notes on this name. 00123 _Sequence c; 00124 00125 public: 00126 // XXX removed old def ctor, added def arg to this one to match 14882 00127 /** 00128 * @brief Default constructor creates no elements. 00129 */ 00130 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00131 explicit 00132 stack(const _Sequence& __c = _Sequence()) 00133 : c(__c) { } 00134 #else 00135 explicit 00136 stack(const _Sequence& __c) 00137 : c(__c) { } 00138 00139 explicit 00140 stack(_Sequence&& __c = _Sequence()) 00141 : c(std::move(__c)) { } 00142 #endif 00143 00144 /** 00145 * Returns true if the %stack is empty. 00146 */ 00147 bool 00148 empty() const 00149 { return c.empty(); } 00150 00151 /** Returns the number of elements in the %stack. */ 00152 size_type 00153 size() const 00154 { return c.size(); } 00155 00156 /** 00157 * Returns a read/write reference to the data at the first 00158 * element of the %stack. 00159 */ 00160 reference 00161 top() 00162 { 00163 __glibcxx_requires_nonempty(); 00164 return c.back(); 00165 } 00166 00167 /** 00168 * Returns a read-only (constant) reference to the data at the first 00169 * element of the %stack. 00170 */ 00171 const_reference 00172 top() const 00173 { 00174 __glibcxx_requires_nonempty(); 00175 return c.back(); 00176 } 00177 00178 /** 00179 * @brief Add data to the top of the %stack. 00180 * @param __x Data to be added. 00181 * 00182 * This is a typical %stack operation. The function creates an 00183 * element at the top of the %stack and assigns the given data 00184 * to it. The time complexity of the operation depends on the 00185 * underlying sequence. 00186 */ 00187 void 00188 push(const value_type& __x) 00189 { c.push_back(__x); } 00190 00191 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00192 void 00193 push(value_type&& __x) 00194 { c.push_back(std::move(__x)); } 00195 00196 template<typename... _Args> 00197 void 00198 emplace(_Args&&... __args) 00199 { c.emplace_back(std::forward<_Args>(__args)...); } 00200 #endif 00201 00202 /** 00203 * @brief Removes first element. 00204 * 00205 * This is a typical %stack operation. It shrinks the %stack 00206 * by one. The time complexity of the operation depends on the 00207 * underlying sequence. 00208 * 00209 * Note that no data is returned, and if the first element's 00210 * data is needed, it should be retrieved before pop() is 00211 * called. 00212 */ 00213 void 00214 pop() 00215 { 00216 __glibcxx_requires_nonempty(); 00217 c.pop_back(); 00218 } 00219 00220 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00221 void 00222 swap(stack& __s) 00223 noexcept(noexcept(swap(c, __s.c))) 00224 { 00225 using std::swap; 00226 swap(c, __s.c); 00227 } 00228 #endif 00229 }; 00230 00231 /** 00232 * @brief Stack equality comparison. 00233 * @param __x A %stack. 00234 * @param __y A %stack of the same type as @a __x. 00235 * @return True iff the size and elements of the stacks are equal. 00236 * 00237 * This is an equivalence relation. Complexity and semantics 00238 * depend on the underlying sequence type, but the expected rules 00239 * are: this relation is linear in the size of the sequences, and 00240 * stacks are considered equivalent if their sequences compare 00241 * equal. 00242 */ 00243 template<typename _Tp, typename _Seq> 00244 inline bool 00245 operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 00246 { return __x.c == __y.c; } 00247 00248 /** 00249 * @brief Stack ordering relation. 00250 * @param __x A %stack. 00251 * @param __y A %stack of the same type as @a x. 00252 * @return True iff @a x is lexicographically less than @a __y. 00253 * 00254 * This is an total ordering relation. Complexity and semantics 00255 * depend on the underlying sequence type, but the expected rules 00256 * are: this relation is linear in the size of the sequences, the 00257 * elements must be comparable with @c <, and 00258 * std::lexicographical_compare() is usually used to make the 00259 * determination. 00260 */ 00261 template<typename _Tp, typename _Seq> 00262 inline bool 00263 operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 00264 { return __x.c < __y.c; } 00265 00266 /// Based on operator== 00267 template<typename _Tp, typename _Seq> 00268 inline bool 00269 operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 00270 { return !(__x == __y); } 00271 00272 /// Based on operator< 00273 template<typename _Tp, typename _Seq> 00274 inline bool 00275 operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 00276 { return __y < __x; } 00277 00278 /// Based on operator< 00279 template<typename _Tp, typename _Seq> 00280 inline bool 00281 operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 00282 { return !(__y < __x); } 00283 00284 /// Based on operator< 00285 template<typename _Tp, typename _Seq> 00286 inline bool 00287 operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 00288 { return !(__x < __y); } 00289 00290 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00291 template<typename _Tp, typename _Seq> 00292 inline void 00293 swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y) 00294 noexcept(noexcept(__x.swap(__y))) 00295 { __x.swap(__y); } 00296 00297 template<typename _Tp, typename _Seq, typename _Alloc> 00298 struct uses_allocator<stack<_Tp, _Seq>, _Alloc> 00299 : public uses_allocator<_Seq, _Alloc>::type { }; 00300 #endif 00301 00302 _GLIBCXX_END_NAMESPACE_VERSION 00303 } // namespace 00304 00305 #endif /* _STL_STACK_H */