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

Generated on Thu Jul 23 21:16:22 2009 for libstdc++ by  doxygen 1.5.8