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 
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 
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
00142       explicit
00143       stack(const _Sequence& __c = _Sequence())
00144       : c(__c) {}
00145 
00149       bool
00150       empty() const
00151       { return c.empty(); }
00152 
00154       size_type
00155       size() const
00156       { return c.size(); }
00157 
00162       reference
00163       top()
00164       {
00165     __glibcxx_requires_nonempty();
00166     return c.back();
00167       }
00168 
00173       const_reference
00174       top() const
00175       {
00176     __glibcxx_requires_nonempty();
00177     return c.back();
00178       }
00179 
00189       void
00190       push(const value_type& __x)
00191       { c.push_back(__x); }
00192 
00204       void
00205       pop()
00206       {
00207     __glibcxx_requires_nonempty();
00208     c.pop_back();
00209       }
00210     };
00211 
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 
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 
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 
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 
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 
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 */

Generated on Tue Jan 30 17:31:55 2007 for GNU C++ STL by doxygen 1.3.6