libstdc++
stl_stack.h
Go to the documentation of this file.
00001 // Stack implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2016 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_front,
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       // concept requirements
00102       typedef typename _Sequence::value_type _Sequence_value_type;
00103 #if __cplusplus < 201103L
00104       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00105       __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
00106 #endif
00107       __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
00108 
00109       template<typename _Tp1, typename _Seq1>
00110         friend bool
00111         operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
00112 
00113       template<typename _Tp1, typename _Seq1>
00114         friend bool
00115         operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
00116 
00117 #if __cplusplus >= 201103L
00118       template<typename _Alloc>
00119         using _Uses = typename
00120           enable_if<uses_allocator<_Sequence, _Alloc>::value>::type;
00121 #endif
00122 
00123     public:
00124       typedef typename _Sequence::value_type                value_type;
00125       typedef typename _Sequence::reference                 reference;
00126       typedef typename _Sequence::const_reference           const_reference;
00127       typedef typename _Sequence::size_type                 size_type;
00128       typedef          _Sequence                            container_type;
00129 
00130     protected:
00131       //  See queue::c for notes on this name.
00132       _Sequence c;
00133 
00134     public:
00135       // XXX removed old def ctor, added def arg to this one to match 14882
00136       /**
00137        *  @brief  Default constructor creates no elements.
00138        */
00139 #if __cplusplus < 201103L
00140       explicit
00141       stack(const _Sequence& __c = _Sequence())
00142       : c(__c) { }
00143 #else
00144       explicit
00145       stack(const _Sequence& __c)
00146       : c(__c) { }
00147 
00148       explicit
00149       stack(_Sequence&& __c = _Sequence())
00150       : c(std::move(__c)) { }
00151 
00152       template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
00153         explicit
00154         stack(const _Alloc& __a)
00155         : c(__a) { }
00156 
00157       template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
00158         stack(const _Sequence& __c, const _Alloc& __a)
00159         : c(__c, __a) { }
00160 
00161       template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
00162         stack(_Sequence&& __c, const _Alloc& __a)
00163         : c(std::move(__c), __a) { }
00164 
00165       template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
00166         stack(const stack& __q, const _Alloc& __a)
00167         : c(__q.c, __a) { }
00168 
00169       template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
00170         stack(stack&& __q, const _Alloc& __a)
00171         : c(std::move(__q.c), __a) { }
00172 #endif
00173 
00174       /**
00175        *  Returns true if the %stack is empty.
00176        */
00177       bool
00178       empty() const
00179       { return c.empty(); }
00180 
00181       /**  Returns the number of elements in the %stack.  */
00182       size_type
00183       size() const
00184       { return c.size(); }
00185 
00186       /**
00187        *  Returns a read/write reference to the data at the first
00188        *  element of the %stack.
00189        */
00190       reference
00191       top()
00192       {
00193         __glibcxx_requires_nonempty();
00194         return c.back();
00195       }
00196 
00197       /**
00198        *  Returns a read-only (constant) reference to the data at the first
00199        *  element of the %stack.
00200        */
00201       const_reference
00202       top() const
00203       {
00204         __glibcxx_requires_nonempty();
00205         return c.back();
00206       }
00207 
00208       /**
00209        *  @brief  Add data to the top of the %stack.
00210        *  @param  __x  Data to be added.
00211        *
00212        *  This is a typical %stack operation.  The function creates an
00213        *  element at the top of the %stack and assigns the given data
00214        *  to it.  The time complexity of the operation depends on the
00215        *  underlying sequence.
00216        */
00217       void
00218       push(const value_type& __x)
00219       { c.push_back(__x); }
00220 
00221 #if __cplusplus >= 201103L
00222       void
00223       push(value_type&& __x)
00224       { c.push_back(std::move(__x)); }
00225 
00226       template<typename... _Args>
00227         void
00228         emplace(_Args&&... __args)
00229         { c.emplace_back(std::forward<_Args>(__args)...); }
00230 #endif
00231 
00232       /**
00233        *  @brief  Removes first element.
00234        *
00235        *  This is a typical %stack operation.  It shrinks the %stack
00236        *  by one.  The time complexity of the operation depends on the
00237        *  underlying sequence.
00238        *
00239        *  Note that no data is returned, and if the first element's
00240        *  data is needed, it should be retrieved before pop() is
00241        *  called.
00242        */
00243       void
00244       pop()
00245       {
00246         __glibcxx_requires_nonempty();
00247         c.pop_back();
00248       }
00249 
00250 #if __cplusplus >= 201103L
00251       void
00252       swap(stack& __s)
00253       noexcept(__is_nothrow_swappable<_Tp>::value)
00254       {
00255         using std::swap;
00256         swap(c, __s.c);
00257       }
00258 #endif
00259     };
00260 
00261   /**
00262    *  @brief  Stack equality comparison.
00263    *  @param  __x  A %stack.
00264    *  @param  __y  A %stack of the same type as @a __x.
00265    *  @return  True iff the size and elements of the stacks are equal.
00266    *
00267    *  This is an equivalence relation.  Complexity and semantics
00268    *  depend on the underlying sequence type, but the expected rules
00269    *  are: this relation is linear in the size of the sequences, and
00270    *  stacks are considered equivalent if their sequences compare
00271    *  equal.
00272   */
00273   template<typename _Tp, typename _Seq>
00274     inline bool
00275     operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00276     { return __x.c == __y.c; }
00277 
00278   /**
00279    *  @brief  Stack ordering relation.
00280    *  @param  __x  A %stack.
00281    *  @param  __y  A %stack of the same type as @a x.
00282    *  @return  True iff @a x is lexicographically less than @a __y.
00283    *
00284    *  This is an total ordering relation.  Complexity and semantics
00285    *  depend on the underlying sequence type, but the expected rules
00286    *  are: this relation is linear in the size of the sequences, the
00287    *  elements must be comparable with @c <, and
00288    *  std::lexicographical_compare() is usually used to make the
00289    *  determination.
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   /// Based on operator==
00297   template<typename _Tp, typename _Seq>
00298     inline bool
00299     operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00300     { return !(__x == __y); }
00301 
00302   /// Based on operator<
00303   template<typename _Tp, typename _Seq>
00304     inline bool
00305     operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00306     { return __y < __x; }
00307 
00308   /// Based on operator<
00309   template<typename _Tp, typename _Seq>
00310     inline bool
00311     operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00312     { return !(__y < __x); }
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 #if __cplusplus >= 201103L
00321   template<typename _Tp, typename _Seq>
00322     inline void
00323     swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
00324     noexcept(noexcept(__x.swap(__y)))
00325     { __x.swap(__y); }
00326 
00327   template<typename _Tp, typename _Seq, typename _Alloc>
00328     struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
00329     : public uses_allocator<_Seq, _Alloc>::type { };
00330 #endif
00331 
00332 _GLIBCXX_END_NAMESPACE_VERSION
00333 } // namespace
00334 
00335 #endif /* _STL_STACK_H */