# stl_stack.h

Go to the documentation of this file.

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