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