stl_stack.h

Go to the documentation of this file.
00001 // Stack implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002 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 2, 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 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  *
00032  * Copyright (c) 1994
00033  * Hewlett-Packard Company
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Hewlett-Packard Company makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  *
00043  *
00044  * Copyright (c) 1996,1997
00045  * Silicon Graphics Computer Systems, Inc.
00046  *
00047  * Permission to use, copy, modify, distribute and sell this software
00048  * and its documentation for any purpose is hereby granted without fee,
00049  * provided that the above copyright notice appear in all copies and
00050  * that both that copyright notice and this permission notice appear
00051  * in supporting documentation.  Silicon Graphics makes no
00052  * representations about the suitability of this software for any
00053  * purpose.  It is provided "as is" without express or implied warranty.
00054  */
00055 
00056 /** @file stl_stack.h
00057  *  This is an internal header file, included by other library headers.
00058  *  You should not attempt to use it directly.
00059  */
00060 
00061 #ifndef __GLIBCPP_INTERNAL_STACK_H
00062 #define __GLIBCPP_INTERNAL_STACK_H
00063 
00064 #include <bits/concept_check.h>
00065 
00066 namespace std
00067 {
00068   // Forward declarations of operators == and <, needed for friend declaration.
00069   
00070   template <typename _Tp, typename _Sequence = deque<_Tp> >
00071   class stack;
00072   
00073   template <typename _Tp, typename _Seq>
00074   inline bool operator==(const stack<_Tp,_Seq>& __x,
00075                      const stack<_Tp,_Seq>& __y);
00076   
00077   template <typename _Tp, typename _Seq>
00078   inline bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y);
00079   
00080   
00081   /**
00082    *  @brief  A standard container giving FILO behavior.
00083    *
00084    *  @ingroup Containers
00085    *  @ingroup Sequences
00086    *
00087    *  Meets many of the requirements of a
00088    *  <a href="tables.html#65">container</a>,
00089    *  but does not define anything to do with iterators.  Very few of the
00090    *  other standard container interfaces are defined.
00091    *
00092    *  This is not a true container, but an @e adaptor.  It holds another
00093    *  container, and provides a wrapper interface to that container.  The
00094    *  wrapper is what enforces strict first-in-last-out %stack behavior.
00095    *
00096    *  The second template parameter defines the type of the underlying
00097    *  sequence/container.  It defaults to std::deque, but it can be any type
00098    *  that supports @c back, @c push_back, and @c pop_front, such as
00099    *  std::list, std::vector, or an appropriate user-defined type.
00100    *
00101    *  Members not found in "normal" containers are @c container_type,
00102    *  which is a typedef for the second Sequence parameter, and @c push,
00103    *  @c pop, and @c top, which are standard %stack/FILO operations.
00104   */
00105   template <typename _Tp, typename _Sequence>
00106     class stack
00107   {
00108     // concept requirements
00109     typedef typename _Sequence::value_type _Sequence_value_type;
00110     __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
00111     __glibcpp_class_requires(_Sequence, _BackInsertionSequenceConcept)
00112     __glibcpp_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
00113   
00114     template <typename _Tp1, typename _Seq1>
00115     friend bool operator== (const stack<_Tp1, _Seq1>&,
00116                             const stack<_Tp1, _Seq1>&);
00117     template <typename _Tp1, typename _Seq1>
00118     friend bool operator< (const stack<_Tp1, _Seq1>&,
00119                            const stack<_Tp1, _Seq1>&);
00120   
00121   public:
00122     typedef typename _Sequence::value_type                value_type;
00123     typedef typename _Sequence::reference                 reference;
00124     typedef typename _Sequence::const_reference           const_reference;
00125     typedef typename _Sequence::size_type                 size_type;
00126     typedef          _Sequence                            container_type;
00127   
00128   protected:
00129     //  See queue::c for notes on this name.
00130     _Sequence c;
00131   
00132   public:
00133     // XXX removed old def ctor, added def arg to this one to match 14882
00134     /**
00135      *  @brief  Default constructor creates no elements.
00136     */
00137     explicit
00138     stack(const _Sequence& __c = _Sequence())
00139     : c(__c) {}
00140   
00141     /**
00142      *  Returns true if the %stack is empty.
00143     */
00144     bool
00145     empty() const { return c.empty(); }
00146   
00147     /**  Returns the number of elements in the %stack.  */
00148     size_type
00149     size() const { return c.size(); }
00150   
00151     /**
00152      *  Returns a read/write reference to the data at the first element of the
00153      *  %stack.
00154     */
00155     reference
00156     top() { return c.back(); }
00157   
00158     /**
00159      *  Returns a read-only (constant) reference to the data at the first
00160      *  element of the %stack.
00161     */
00162     const_reference
00163     top() const { return c.back(); }
00164   
00165     /**
00166      *  @brief  Add data to the top of the %stack.
00167      *  @param  x  Data to be added.
00168      *
00169      *  This is a typical %stack operation.  The function creates an element at
00170      *  the top of the %stack and assigns the given data to it.
00171      *  The time complexity of the operation depends on the underlying
00172      *  sequence.
00173     */
00174     void
00175     push(const value_type& __x) { c.push_back(__x); }
00176   
00177     /**
00178      *  @brief  Removes first element.
00179      *
00180      *  This is a typical %stack operation.  It shrinks the %stack by one.
00181      *  The time complexity of the operation depends on the underlying
00182      *  sequence.
00183      *
00184      *  Note that no data is returned, and if the first element's data is
00185      *  needed, it should be retrieved before pop() is called.
00186     */
00187     void
00188     pop() { c.pop_back(); }
00189   };
00190   
00191   
00192   /**
00193    *  @brief  Stack equality comparison.
00194    *  @param  x  A %stack.
00195    *  @param  y  A %stack of the same type as @a x.
00196    *  @return  True iff the size and elements of the stacks are equal.
00197    *
00198    *  This is an equivalence relation.  Complexity and semantics depend on the
00199    *  underlying sequence type, but the expected rules are:  this relation is
00200    *  linear in the size of the sequences, and stacks are considered equivalent
00201    *  if their sequences compare equal.
00202   */
00203   template <typename _Tp, typename _Seq>
00204     inline bool
00205     operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
00206     { return __x.c == __y.c; }
00207   
00208   /**
00209    *  @brief  Stack ordering relation.
00210    *  @param  x  A %stack.
00211    *  @param  y  A %stack of the same type as @a x.
00212    *  @return  True iff @a x is lexographically less than @a y.
00213    *
00214    *  This is an total ordering relation.  Complexity and semantics depend on
00215    *  the underlying sequence type, but the expected rules are:  this relation
00216    *  is linear in the size of the sequences, the elements must be comparable
00217    *  with @c <, and std::lexographical_compare() is usually used to make the
00218    *  determination.
00219   */
00220   template <typename _Tp, typename _Seq>
00221     inline bool
00222     operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
00223     { return __x.c < __y.c; }
00224   
00225   /// Based on operator==
00226   template <typename _Tp, typename _Seq>
00227     inline bool
00228     operator!=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
00229     { return !(__x == __y); }
00230   
00231   /// Based on operator<
00232   template <typename _Tp, typename _Seq>
00233     inline bool
00234     operator>(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
00235     { return __y < __x; }
00236   
00237   /// Based on operator<
00238   template <typename _Tp, typename _Seq>
00239     inline bool
00240     operator<=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
00241     { return !(__y < __x); }
00242   
00243   /// Based on operator<
00244   template <typename _Tp, typename _Seq>
00245     inline bool
00246     operator>=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
00247     { return !(__x < __y); }
00248 } // namespace std
00249 
00250 #endif /* __GLIBCPP_INTERNAL_STACK_H */

Generated on Thu Feb 10 23:22:59 2005 for libstdc++-v3 Source by  doxygen 1.4.0