libstdc++
stl_stack.h
Go to the documentation of this file.
00001 // Stack implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2013 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 
00062 namespace std _GLIBCXX_VISIBILITY(default)
00063 {
00064 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00065 
00066   /**
00067    *  @brief  A standard container giving FILO behavior.
00068    *
00069    *  @ingroup sequences
00070    *
00071    *  @tparam _Tp  Type of element.
00072    *  @tparam _Sequence  Type of underlying sequence, defaults to deque<_Tp>.
00073    *
00074    *  Meets many of the requirements of a
00075    *  <a href="tables.html#65">container</a>,
00076    *  but does not define anything to do with iterators.  Very few of the
00077    *  other standard container interfaces are defined.
00078    *
00079    *  This is not a true container, but an @e adaptor.  It holds
00080    *  another container, and provides a wrapper interface to that
00081    *  container.  The wrapper is what enforces strict
00082    *  first-in-last-out %stack behavior.
00083    *
00084    *  The second template parameter defines the type of the underlying
00085    *  sequence/container.  It defaults to std::deque, but it can be
00086    *  any type that supports @c back, @c push_back, and @c pop_front,
00087    *  such as std::list, std::vector, or an appropriate user-defined
00088    *  type.
00089    *
00090    *  Members not found in @a normal containers are @c container_type,
00091    *  which is a typedef for the second Sequence parameter, and @c
00092    *  push, @c pop, and @c top, which are standard %stack/FILO
00093    *  operations.
00094   */
00095   template<typename _Tp, typename _Sequence = deque<_Tp> >
00096     class stack
00097     {
00098       // concept requirements
00099       typedef typename _Sequence::value_type _Sequence_value_type;
00100       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00101       __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
00102       __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
00103 
00104       template<typename _Tp1, typename _Seq1>
00105         friend bool
00106         operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
00107 
00108       template<typename _Tp1, typename _Seq1>
00109         friend bool
00110         operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
00111 
00112     public:
00113       typedef typename _Sequence::value_type                value_type;
00114       typedef typename _Sequence::reference                 reference;
00115       typedef typename _Sequence::const_reference           const_reference;
00116       typedef typename _Sequence::size_type                 size_type;
00117       typedef          _Sequence                            container_type;
00118 
00119     protected:
00120       //  See queue::c for notes on this name.
00121       _Sequence c;
00122 
00123     public:
00124       // XXX removed old def ctor, added def arg to this one to match 14882
00125       /**
00126        *  @brief  Default constructor creates no elements.
00127        */
00128 #if __cplusplus < 201103L
00129       explicit
00130       stack(const _Sequence& __c = _Sequence())
00131       : c(__c) { }
00132 #else
00133       explicit
00134       stack(const _Sequence& __c)
00135       : c(__c) { }
00136 
00137       explicit
00138       stack(_Sequence&& __c = _Sequence())
00139       : c(std::move(__c)) { }
00140 #endif
00141 
00142       /**
00143        *  Returns true if the %stack is empty.
00144        */
00145       bool
00146       empty() const
00147       { return c.empty(); }
00148 
00149       /**  Returns the number of elements in the %stack.  */
00150       size_type
00151       size() const
00152       { return c.size(); }
00153 
00154       /**
00155        *  Returns a read/write reference to the data at the first
00156        *  element of the %stack.
00157        */
00158       reference
00159       top()
00160       {
00161     __glibcxx_requires_nonempty();
00162     return c.back();
00163       }
00164 
00165       /**
00166        *  Returns a read-only (constant) reference to the data at the first
00167        *  element of the %stack.
00168        */
00169       const_reference
00170       top() const
00171       {
00172     __glibcxx_requires_nonempty();
00173     return c.back();
00174       }
00175 
00176       /**
00177        *  @brief  Add data to the top of the %stack.
00178        *  @param  __x  Data to be added.
00179        *
00180        *  This is a typical %stack operation.  The function creates an
00181        *  element at the top of the %stack and assigns the given data
00182        *  to it.  The time complexity of the operation depends on the
00183        *  underlying sequence.
00184        */
00185       void
00186       push(const value_type& __x)
00187       { c.push_back(__x); }
00188 
00189 #if __cplusplus >= 201103L
00190       void
00191       push(value_type&& __x)
00192       { c.push_back(std::move(__x)); }
00193 
00194       template<typename... _Args>
00195         void
00196         emplace(_Args&&... __args)
00197     { c.emplace_back(std::forward<_Args>(__args)...); }
00198 #endif
00199 
00200       /**
00201        *  @brief  Removes first element.
00202        *
00203        *  This is a typical %stack operation.  It shrinks the %stack
00204        *  by one.  The time complexity of the operation depends on the
00205        *  underlying sequence.
00206        *
00207        *  Note that no data is returned, and if the first element's
00208        *  data is needed, it should be retrieved before pop() is
00209        *  called.
00210        */
00211       void
00212       pop()
00213       {
00214     __glibcxx_requires_nonempty();
00215     c.pop_back();
00216       }
00217 
00218 #if __cplusplus >= 201103L
00219       void
00220       swap(stack& __s)
00221       noexcept(noexcept(swap(c, __s.c)))
00222       {
00223     using std::swap;
00224     swap(c, __s.c);
00225       }
00226 #endif
00227     };
00228 
00229   /**
00230    *  @brief  Stack equality comparison.
00231    *  @param  __x  A %stack.
00232    *  @param  __y  A %stack of the same type as @a __x.
00233    *  @return  True iff the size and elements of the stacks are equal.
00234    *
00235    *  This is an equivalence relation.  Complexity and semantics
00236    *  depend on the underlying sequence type, but the expected rules
00237    *  are: this relation is linear in the size of the sequences, and
00238    *  stacks are considered equivalent if their sequences compare
00239    *  equal.
00240   */
00241   template<typename _Tp, typename _Seq>
00242     inline bool
00243     operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00244     { return __x.c == __y.c; }
00245 
00246   /**
00247    *  @brief  Stack ordering relation.
00248    *  @param  __x  A %stack.
00249    *  @param  __y  A %stack of the same type as @a x.
00250    *  @return  True iff @a x is lexicographically less than @a __y.
00251    *
00252    *  This is an total ordering relation.  Complexity and semantics
00253    *  depend on the underlying sequence type, but the expected rules
00254    *  are: this relation is linear in the size of the sequences, the
00255    *  elements must be comparable with @c <, and
00256    *  std::lexicographical_compare() is usually used to make the
00257    *  determination.
00258   */
00259   template<typename _Tp, typename _Seq>
00260     inline bool
00261     operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00262     { return __x.c < __y.c; }
00263 
00264   /// Based on operator==
00265   template<typename _Tp, typename _Seq>
00266     inline bool
00267     operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00268     { return !(__x == __y); }
00269 
00270   /// Based on operator<
00271   template<typename _Tp, typename _Seq>
00272     inline bool
00273     operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00274     { return __y < __x; }
00275 
00276   /// Based on operator<
00277   template<typename _Tp, typename _Seq>
00278     inline bool
00279     operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00280     { return !(__y < __x); }
00281 
00282   /// Based on operator<
00283   template<typename _Tp, typename _Seq>
00284     inline bool
00285     operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00286     { return !(__x < __y); }
00287 
00288 #if __cplusplus >= 201103L
00289   template<typename _Tp, typename _Seq>
00290     inline void
00291     swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
00292     noexcept(noexcept(__x.swap(__y)))
00293     { __x.swap(__y); }
00294 
00295   template<typename _Tp, typename _Seq, typename _Alloc>
00296     struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
00297     : public uses_allocator<_Seq, _Alloc>::type { };
00298 #endif
00299 
00300 _GLIBCXX_END_NAMESPACE_VERSION
00301 } // namespace
00302 
00303 #endif /* _STL_STACK_H */