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       {
00221     using std::swap;
00222     swap(c, __s.c);
00223       }
00224 #endif
00225     };
00226 
00227   /**
00228    *  @brief  Stack equality comparison.
00229    *  @param  x  A %stack.
00230    *  @param  y  A %stack of the same type as @a x.
00231    *  @return  True iff the size and elements of the stacks are equal.
00232    *
00233    *  This is an equivalence relation.  Complexity and semantics
00234    *  depend on the underlying sequence type, but the expected rules
00235    *  are: this relation is linear in the size of the sequences, and
00236    *  stacks are considered equivalent if their sequences compare
00237    *  equal.
00238   */
00239   template<typename _Tp, typename _Seq>
00240     inline bool
00241     operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00242     { return __x.c == __y.c; }
00243 
00244   /**
00245    *  @brief  Stack ordering relation.
00246    *  @param  x  A %stack.
00247    *  @param  y  A %stack of the same type as @a x.
00248    *  @return  True iff @a x is lexicographically less than @a y.
00249    *
00250    *  This is an total ordering relation.  Complexity and semantics
00251    *  depend on the underlying sequence type, but the expected rules
00252    *  are: this relation is linear in the size of the sequences, the
00253    *  elements must be comparable with @c <, and
00254    *  std::lexicographical_compare() is usually used to make the
00255    *  determination.
00256   */
00257   template<typename _Tp, typename _Seq>
00258     inline bool
00259     operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00260     { return __x.c < __y.c; }
00261 
00262   /// Based on operator==
00263   template<typename _Tp, typename _Seq>
00264     inline bool
00265     operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00266     { return !(__x == __y); }
00267 
00268   /// Based on operator<
00269   template<typename _Tp, typename _Seq>
00270     inline bool
00271     operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00272     { return __y < __x; }
00273 
00274   /// Based on operator<
00275   template<typename _Tp, typename _Seq>
00276     inline bool
00277     operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00278     { return !(__y < __x); }
00279 
00280   /// Based on operator<
00281   template<typename _Tp, typename _Seq>
00282     inline bool
00283     operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00284     { return !(__x < __y); }
00285 
00286 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00287   template<typename _Tp, typename _Seq>
00288     inline void
00289     swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
00290     { __x.swap(__y); }
00291 
00292   template<typename _Tp, typename _Seq, typename _Alloc>
00293     struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
00294     : public uses_allocator<_Seq, _Alloc>::type { };
00295 #endif
00296 
00297 _GLIBCXX_END_NAMESPACE_VERSION
00298 } // namespace
00299 
00300 #endif /* _STL_STACK_H */