stl_queue.h

Go to the documentation of this file.
00001 // Queue implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 _QUEUE_H
00062 #define _QUEUE_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 declaration.
00070   template<typename _Tp, typename _Sequence = deque<_Tp> >
00071     class queue;
00072 
00073   template<typename _Tp, typename _Seq>
00074     inline bool
00075     operator==(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&);
00076 
00077   template<typename _Tp, typename _Seq>
00078     inline bool
00079     operator<(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&);
00080 
00105   template<typename _Tp, typename _Sequence>
00106     class queue
00107     {
00108       // concept requirements
00109       typedef typename _Sequence::value_type _Sequence_value_type;
00110       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00111       __glibcxx_class_requires(_Sequence, _FrontInsertionSequenceConcept)
00112       __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
00113       __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
00114 
00115       template<typename _Tp1, typename _Seq1>
00116         friend bool
00117         operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
00118 
00119       template<typename _Tp1, typename _Seq1>
00120         friend bool
00121         operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
00122 
00123     public:
00124       typedef typename _Sequence::value_type                value_type;
00125       typedef typename _Sequence::reference                 reference;
00126       typedef typename _Sequence::const_reference           const_reference;
00127       typedef typename _Sequence::size_type                 size_type;
00128       typedef          _Sequence                            container_type;
00129 
00130     protected:
00139       _Sequence c;
00140 
00141     public:
00145       explicit
00146       queue(const _Sequence& __c = _Sequence()) : c(__c) {}
00147 
00151       bool
00152       empty() const
00153       { return c.empty(); }
00154 
00156       size_type
00157       size() const
00158       { return c.size(); }
00159 
00164       reference
00165       front()
00166       {
00167     __glibcxx_requires_nonempty();
00168     return c.front();
00169       }
00170 
00175       const_reference
00176       front() const
00177       {
00178     __glibcxx_requires_nonempty();
00179     return c.front();
00180       }
00181 
00186       reference
00187       back()
00188       {
00189     __glibcxx_requires_nonempty();
00190     return c.back();
00191       }
00192 
00197       const_reference
00198       back() const
00199       {
00200     __glibcxx_requires_nonempty();
00201     return c.back();
00202       }
00203 
00213       void
00214       push(const value_type& __x)
00215       { c.push_back(__x); }
00216 
00228       void
00229       pop()
00230       {
00231     __glibcxx_requires_nonempty();
00232     c.pop_front();
00233       }
00234     };
00235 
00236 
00248   template<typename _Tp, typename _Sequence>
00249     inline bool
00250     operator==(const queue<_Tp,_Sequence>& __x,
00251            const queue<_Tp,_Sequence>& __y)
00252     { return __x.c == __y.c; }
00253 
00267   template<typename _Tp, typename _Sequence>
00268     inline bool
00269     operator<(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y)
00270     { return __x.c < __y.c; }
00271 
00273   template<typename _Tp, typename _Sequence>
00274     inline bool
00275     operator!=(const queue<_Tp,_Sequence>& __x,
00276            const queue<_Tp,_Sequence>& __y)
00277     { return !(__x == __y); }
00278 
00280   template<typename _Tp, typename _Sequence>
00281     inline bool
00282     operator>(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y)
00283     { return __y < __x; }
00284 
00286   template<typename _Tp, typename _Sequence>
00287     inline bool
00288     operator<=(const queue<_Tp,_Sequence>& __x,
00289            const queue<_Tp,_Sequence>& __y)
00290     { return !(__y < __x); }
00291 
00293   template<typename _Tp, typename _Sequence>
00294     inline bool
00295     operator>=(const queue<_Tp,_Sequence>& __x,
00296            const queue<_Tp,_Sequence>& __y)
00297     { return !(__x < __y); }
00298 
00335   template<typename _Tp, typename _Sequence = vector<_Tp>,
00336        typename _Compare  = less<typename _Sequence::value_type> >
00337     class priority_queue
00338     {
00339       // concept requirements
00340       typedef typename _Sequence::value_type _Sequence_value_type;
00341       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00342       __glibcxx_class_requires(_Sequence, _SequenceConcept)
00343       __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept)
00344       __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
00345       __glibcxx_class_requires4(_Compare, bool, _Tp,_Tp,_BinaryFunctionConcept)
00346 
00347     public:
00348       typedef typename _Sequence::value_type                value_type;
00349       typedef typename _Sequence::reference                 reference;
00350       typedef typename _Sequence::const_reference           const_reference;
00351       typedef typename _Sequence::size_type                 size_type;
00352       typedef          _Sequence                            container_type;
00353 
00354     protected:
00355       //  See queue::c for notes on these names.
00356       _Sequence  c;
00357       _Compare   comp;
00358 
00359     public:
00363       explicit
00364       priority_queue(const _Compare& __x = _Compare(),
00365              const _Sequence& __s = _Sequence())
00366       : c(__s), comp(__x)
00367       { std::make_heap(c.begin(), c.end(), comp); }
00368 
00384       template<typename _InputIterator>
00385         priority_queue(_InputIterator __first, _InputIterator __last,
00386                const _Compare& __x = _Compare(),
00387                const _Sequence& __s = _Sequence())
00388     : c(__s), comp(__x)
00389         {
00390       __glibcxx_requires_valid_range(__first, __last);
00391       c.insert(c.end(), __first, __last);
00392       std::make_heap(c.begin(), c.end(), comp);
00393     }
00394 
00398       bool
00399       empty() const { return c.empty(); }
00400 
00402       size_type
00403       size() const { return c.size(); }
00404 
00409       const_reference
00410       top() const
00411       {
00412     __glibcxx_requires_nonempty();
00413     return c.front();
00414       }
00415 
00424       void
00425       push(const value_type& __x)
00426       {
00427     try
00428         {
00429           c.push_back(__x);
00430           std::push_heap(c.begin(), c.end(), comp);
00431         }
00432     catch(...)
00433         {
00434           c.clear();
00435           __throw_exception_again;
00436         }
00437       }
00438 
00450       void
00451       pop()
00452       {
00453     __glibcxx_requires_nonempty();
00454     try
00455         {
00456           std::pop_heap(c.begin(), c.end(), comp);
00457           c.pop_back();
00458         }
00459     catch(...)
00460         {
00461           c.clear();
00462           __throw_exception_again;
00463         }
00464       }
00465     };
00466 
00467   // No equality/comparison operators are provided for priority_queue.
00468 } // namespace std
00469 
00470 #endif /* _QUEUE_H */

Generated on Tue Feb 2 16:56:40 2010 for GNU C++ STL by  doxygen 1.4.7