# 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
    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
    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
    00137     explicit
    00138     stack(const _Sequence& __c = _Sequence())
    00139     : c(__c) {}
    00140
    00144     bool
    00145     empty() const { return c.empty(); }
    00146
    00148     size_type
    00149     size() const { return c.size(); }
    00150
    00155     reference
    00156     top() { return c.back(); }
    00157
    00162     const_reference
    00163     top() const { return c.back(); }
    00164
    00174     void
    00175     push(const value_type& __x) { c.push_back(__x); }
    00176
    00187     void
    00188     pop() { c.pop_back(); }
    00189   };
    00190
    00191
    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
    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
    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
    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
    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
    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 */