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