stl_queue.h

Go to the documentation of this file.
00001 // Queue 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_QUEUE_H
00062 #define __GLIBCPP_INTERNAL_QUEUE_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 queue;
00072   
00073   template <typename _Tp, typename _Seq>
00074   inline bool operator==(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&);
00075   
00076   template <typename _Tp, typename _Seq>
00077   inline bool operator<(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&);
00078   
00079   
00104   template <typename _Tp, typename _Sequence>
00105     class queue
00106   {
00107     // concept requirements
00108     typedef typename _Sequence::value_type _Sequence_value_type;
00109     __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
00110     __glibcpp_class_requires(_Sequence, _FrontInsertionSequenceConcept)
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 queue<_Tp1, _Seq1>&,
00116                             const queue<_Tp1, _Seq1>&);
00117     template <typename _Tp1, typename _Seq1>
00118     friend bool operator< (const queue<_Tp1, _Seq1>&,
00119                            const queue<_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:
00136     _Sequence c;
00137   
00138   public:
00142     explicit
00143     queue(const _Sequence& __c = _Sequence())
00144     : c(__c) {}
00145   
00149     bool
00150     empty() const { return c.empty(); }
00151   
00153     size_type
00154     size() const { return c.size(); }
00155   
00160     reference
00161     front() { return c.front(); }
00162   
00167     const_reference
00168     front() const { return c.front(); }
00169   
00174     reference
00175     back() { return c.back(); }
00176   
00181     const_reference
00182     back() const { return c.back(); }
00183   
00193     void
00194     push(const value_type& __x) { c.push_back(__x); }
00195   
00206     void
00207     pop() { c.pop_front(); }
00208   };
00209   
00210   
00222   template <typename _Tp, typename _Sequence>
00223     inline bool 
00224     operator==(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y)
00225     { return __x.c == __y.c; }
00226   
00239   template <typename _Tp, typename _Sequence>
00240     inline bool
00241     operator<(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y)
00242     { return __x.c < __y.c; }
00243   
00245   template <typename _Tp, typename _Sequence>
00246     inline bool
00247     operator!=(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y)
00248     { return !(__x == __y); }
00249   
00251   template <typename _Tp, typename _Sequence>
00252     inline bool 
00253     operator>(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y)
00254     { return __y < __x; }
00255   
00257   template <typename _Tp, typename _Sequence>
00258     inline bool 
00259     operator<=(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y)
00260     { return !(__y < __x); }
00261   
00263   template <typename _Tp, typename _Sequence>
00264     inline bool 
00265     operator>=(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y)
00266     { return !(__x < __y); }
00267   
00268   
00303   template <typename _Tp, typename _Sequence = vector<_Tp>,
00304             typename _Compare  = less<typename _Sequence::value_type> >
00305     class priority_queue
00306   {
00307     // concept requirements
00308     typedef typename _Sequence::value_type _Sequence_value_type;
00309     __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
00310     __glibcpp_class_requires(_Sequence, _SequenceConcept)
00311     __glibcpp_class_requires(_Sequence, _RandomAccessContainerConcept)
00312     __glibcpp_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
00313     __glibcpp_class_requires4(_Compare, bool, _Tp, _Tp, _BinaryFunctionConcept)
00314   
00315   public:
00316     typedef typename _Sequence::value_type                value_type;
00317     typedef typename _Sequence::reference                 reference;
00318     typedef typename _Sequence::const_reference           const_reference;
00319     typedef typename _Sequence::size_type                 size_type;
00320     typedef          _Sequence                            container_type;
00321   
00322   protected:
00323     //  See queue::c for notes on these names.
00324     _Sequence  c;
00325     _Compare   comp;
00326   
00327   public:
00331     explicit
00332     priority_queue(const _Compare& __x = _Compare(),
00333                    const _Sequence& __s = _Sequence()) 
00334     : c(__s), comp(__x) 
00335     { make_heap(c.begin(), c.end(), comp); }
00336   
00351     template <typename _InputIterator>
00352       priority_queue(_InputIterator __first, _InputIterator __last,
00353                      const _Compare& __x = _Compare(),
00354                      const _Sequence& __s = _Sequence())
00355       : c(__s), comp(__x)
00356       { 
00357         c.insert(c.end(), __first, __last);
00358         make_heap(c.begin(), c.end(), comp);
00359       }
00360   
00364     bool
00365     empty() const { return c.empty(); }
00366   
00368     size_type
00369     size() const { return c.size(); }
00370   
00375     const_reference
00376     top() const { return c.front(); }
00377   
00386     void 
00387     push(const value_type& __x) 
00388     {
00389       try 
00390         {
00391           c.push_back(__x); 
00392           push_heap(c.begin(), c.end(), comp);
00393         }
00394       catch(...)
00395         {
00396           c.clear();
00397           __throw_exception_again; 
00398         }
00399     }
00400   
00411     void 
00412     pop() 
00413     {
00414       try 
00415         {
00416           pop_heap(c.begin(), c.end(), comp);
00417           c.pop_back();
00418         }
00419       catch(...)
00420         {
00421           c.clear();
00422           __throw_exception_again; 
00423         }
00424     }
00425   };
00426   
00427   // No equality/comparison operators are provided for priority_queue.
00428 } // namespace std
00429 
00430 #endif /* __GLIBCPP_INTERNAL_QUEUE_H */

Generated on Tue Apr 29 20:16:23 2003 for libstdc++-v3 Source by doxygen1.3