libstdc++
stl_queue.h
Go to the documentation of this file.
00001 // Queue implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
00004 // 2010, 2011, 2012
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /*
00028  *
00029  * Copyright (c) 1994
00030  * Hewlett-Packard Company
00031  *
00032  * Permission to use, copy, modify, distribute and sell this software
00033  * and its documentation for any purpose is hereby granted without fee,
00034  * provided that the above copyright notice appear in all copies and
00035  * that both that copyright notice and this permission notice appear
00036  * in supporting documentation.  Hewlett-Packard Company makes no
00037  * representations about the suitability of this software for any
00038  * purpose.  It is provided "as is" without express or implied warranty.
00039  *
00040  *
00041  * Copyright (c) 1996,1997
00042  * Silicon Graphics Computer Systems, Inc.
00043  *
00044  * Permission to use, copy, modify, distribute and sell this software
00045  * and its documentation for any purpose is hereby granted without fee,
00046  * provided that the above copyright notice appear in all copies and
00047  * that both that copyright notice and this permission notice appear
00048  * in supporting documentation.  Silicon Graphics makes no
00049  * representations about the suitability of this software for any
00050  * purpose.  It is provided "as is" without express or implied warranty.
00051  */
00052 
00053 /** @file bits/stl_queue.h
00054  *  This is an internal header file, included by other library headers.
00055  *  Do not attempt to use it directly. @headername{queue}
00056  */
00057 
00058 #ifndef _STL_QUEUE_H
00059 #define _STL_QUEUE_H 1
00060 
00061 #include <bits/concept_check.h>
00062 #include <debug/debug.h>
00063 
00064 namespace std _GLIBCXX_VISIBILITY(default)
00065 {
00066 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00067 
00068   /**
00069    *  @brief  A standard container giving FIFO behavior.
00070    *
00071    *  @ingroup sequences
00072    *
00073    *  @tparam _Tp  Type of element.
00074    *  @tparam _Sequence  Type of underlying sequence, defaults to deque<_Tp>.
00075    *
00076    *  Meets many of the requirements of a
00077    *  <a href="tables.html#65">container</a>,
00078    *  but does not define anything to do with iterators.  Very few of the
00079    *  other standard container interfaces are defined.
00080    *
00081    *  This is not a true container, but an @e adaptor.  It holds another
00082    *  container, and provides a wrapper interface to that container.  The
00083    *  wrapper is what enforces strict first-in-first-out %queue behavior.
00084    *
00085    *  The second template parameter defines the type of the underlying
00086    *  sequence/container.  It defaults to std::deque, but it can be any type
00087    *  that supports @c front, @c back, @c push_back, and @c pop_front,
00088    *  such as std::list or an appropriate user-defined type.
00089    *
00090    *  Members not found in @a normal containers are @c container_type,
00091    *  which is a typedef for the second Sequence parameter, and @c push and
00092    *  @c pop, which are standard %queue/FIFO operations.
00093   */
00094   template<typename _Tp, typename _Sequence = deque<_Tp> >
00095     class queue
00096     {
00097       // concept requirements
00098       typedef typename _Sequence::value_type _Sequence_value_type;
00099       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00100       __glibcxx_class_requires(_Sequence, _FrontInsertionSequenceConcept)
00101       __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
00102       __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
00103 
00104       template<typename _Tp1, typename _Seq1>
00105         friend bool
00106         operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
00107 
00108       template<typename _Tp1, typename _Seq1>
00109         friend bool
00110         operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
00111 
00112     public:
00113       typedef typename _Sequence::value_type                value_type;
00114       typedef typename _Sequence::reference                 reference;
00115       typedef typename _Sequence::const_reference           const_reference;
00116       typedef typename _Sequence::size_type                 size_type;
00117       typedef          _Sequence                            container_type;
00118 
00119     protected:
00120       /**
00121        *  'c' is the underlying container.  Maintainers wondering why
00122        *  this isn't uglified as per style guidelines should note that
00123        *  this name is specified in the standard, [23.2.3.1].  (Why?
00124        *  Presumably for the same reason that it's protected instead
00125        *  of private: to allow derivation.  But none of the other
00126        *  containers allow for derivation.  Odd.)
00127        */
00128       _Sequence c;
00129 
00130     public:
00131       /**
00132        *  @brief  Default constructor creates no elements.
00133        */
00134 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00135       explicit
00136       queue(const _Sequence& __c = _Sequence())
00137       : c(__c) { }
00138 #else
00139       explicit
00140       queue(const _Sequence& __c)
00141       : c(__c) { }
00142 
00143       explicit
00144       queue(_Sequence&& __c = _Sequence())
00145       : c(std::move(__c)) { }
00146 #endif
00147 
00148       /**
00149        *  Returns true if the %queue is empty.
00150        */
00151       bool
00152       empty() const
00153       { return c.empty(); }
00154 
00155       /**  Returns the number of elements in the %queue.  */
00156       size_type
00157       size() const
00158       { return c.size(); }
00159 
00160       /**
00161        *  Returns a read/write reference to the data at the first
00162        *  element of the %queue.
00163        */
00164       reference
00165       front()
00166       {
00167     __glibcxx_requires_nonempty();
00168     return c.front();
00169       }
00170 
00171       /**
00172        *  Returns a read-only (constant) reference to the data at the first
00173        *  element of the %queue.
00174        */
00175       const_reference
00176       front() const
00177       {
00178     __glibcxx_requires_nonempty();
00179     return c.front();
00180       }
00181 
00182       /**
00183        *  Returns a read/write reference to the data at the last
00184        *  element of the %queue.
00185        */
00186       reference
00187       back()
00188       {
00189     __glibcxx_requires_nonempty();
00190     return c.back();
00191       }
00192 
00193       /**
00194        *  Returns a read-only (constant) reference to the data at the last
00195        *  element of the %queue.
00196        */
00197       const_reference
00198       back() const
00199       {
00200     __glibcxx_requires_nonempty();
00201     return c.back();
00202       }
00203 
00204       /**
00205        *  @brief  Add data to the end of the %queue.
00206        *  @param  __x  Data to be added.
00207        *
00208        *  This is a typical %queue operation.  The function creates an
00209        *  element at the end of the %queue and assigns the given data
00210        *  to it.  The time complexity of the operation depends on the
00211        *  underlying sequence.
00212        */
00213       void
00214       push(const value_type& __x)
00215       { c.push_back(__x); }
00216 
00217 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00218       void
00219       push(value_type&& __x)
00220       { c.push_back(std::move(__x)); }
00221 
00222       template<typename... _Args>
00223         void
00224         emplace(_Args&&... __args)
00225     { c.emplace_back(std::forward<_Args>(__args)...); }
00226 #endif
00227 
00228       /**
00229        *  @brief  Removes first element.
00230        *
00231        *  This is a typical %queue operation.  It shrinks the %queue by one.
00232        *  The time complexity of the operation depends on the underlying
00233        *  sequence.
00234        *
00235        *  Note that no data is returned, and if the first element's
00236        *  data is needed, it should be retrieved before pop() is
00237        *  called.
00238        */
00239       void
00240       pop()
00241       {
00242     __glibcxx_requires_nonempty();
00243     c.pop_front();
00244       }
00245 
00246 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00247       void
00248       swap(queue& __q)
00249       noexcept(noexcept(swap(c, __q.c)))
00250       {
00251     using std::swap;
00252     swap(c, __q.c);
00253       }
00254 #endif
00255     };
00256 
00257   /**
00258    *  @brief  Queue equality comparison.
00259    *  @param  __x  A %queue.
00260    *  @param  __y  A %queue of the same type as @a __x.
00261    *  @return  True iff the size and elements of the queues are equal.
00262    *
00263    *  This is an equivalence relation.  Complexity and semantics depend on the
00264    *  underlying sequence type, but the expected rules are:  this relation is
00265    *  linear in the size of the sequences, and queues are considered equivalent
00266    *  if their sequences compare equal.
00267   */
00268   template<typename _Tp, typename _Seq>
00269     inline bool
00270     operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
00271     { return __x.c == __y.c; }
00272 
00273   /**
00274    *  @brief  Queue ordering relation.
00275    *  @param  __x  A %queue.
00276    *  @param  __y  A %queue of the same type as @a x.
00277    *  @return  True iff @a __x is lexicographically less than @a __y.
00278    *
00279    *  This is an total ordering relation.  Complexity and semantics
00280    *  depend on the underlying sequence type, but the expected rules
00281    *  are: this relation is linear in the size of the sequences, the
00282    *  elements must be comparable with @c <, and
00283    *  std::lexicographical_compare() is usually used to make the
00284    *  determination.
00285   */
00286   template<typename _Tp, typename _Seq>
00287     inline bool
00288     operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
00289     { return __x.c < __y.c; }
00290 
00291   /// Based on operator==
00292   template<typename _Tp, typename _Seq>
00293     inline bool
00294     operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
00295     { return !(__x == __y); }
00296 
00297   /// Based on operator<
00298   template<typename _Tp, typename _Seq>
00299     inline bool
00300     operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
00301     { return __y < __x; }
00302 
00303   /// Based on operator<
00304   template<typename _Tp, typename _Seq>
00305     inline bool
00306     operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
00307     { return !(__y < __x); }
00308 
00309   /// Based on operator<
00310   template<typename _Tp, typename _Seq>
00311     inline bool
00312     operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
00313     { return !(__x < __y); }
00314 
00315 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00316   template<typename _Tp, typename _Seq>
00317     inline void
00318     swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y)
00319     noexcept(noexcept(__x.swap(__y)))
00320     { __x.swap(__y); }
00321 
00322   template<typename _Tp, typename _Seq, typename _Alloc>
00323     struct uses_allocator<queue<_Tp, _Seq>, _Alloc>
00324     : public uses_allocator<_Seq, _Alloc>::type { };
00325 #endif
00326 
00327   /**
00328    *  @brief  A standard container automatically sorting its contents.
00329    *
00330    *  @ingroup sequences
00331    *
00332    *  @tparam _Tp  Type of element.
00333    *  @tparam _Sequence  Type of underlying sequence, defaults to vector<_Tp>.
00334    *  @tparam _Compare  Comparison function object type, defaults to 
00335    *                    less<_Sequence::value_type>.
00336    *
00337    *  This is not a true container, but an @e adaptor.  It holds
00338    *  another container, and provides a wrapper interface to that
00339    *  container.  The wrapper is what enforces priority-based sorting 
00340    *  and %queue behavior.  Very few of the standard container/sequence
00341    *  interface requirements are met (e.g., iterators).
00342    *
00343    *  The second template parameter defines the type of the underlying
00344    *  sequence/container.  It defaults to std::vector, but it can be
00345    *  any type that supports @c front(), @c push_back, @c pop_back,
00346    *  and random-access iterators, such as std::deque or an
00347    *  appropriate user-defined type.
00348    *
00349    *  The third template parameter supplies the means of making
00350    *  priority comparisons.  It defaults to @c less<value_type> but
00351    *  can be anything defining a strict weak ordering.
00352    *
00353    *  Members not found in @a normal containers are @c container_type,
00354    *  which is a typedef for the second Sequence parameter, and @c
00355    *  push, @c pop, and @c top, which are standard %queue operations.
00356    *
00357    *  @note No equality/comparison operators are provided for
00358    *  %priority_queue.
00359    *
00360    *  @note Sorting of the elements takes place as they are added to,
00361    *  and removed from, the %priority_queue using the
00362    *  %priority_queue's member functions.  If you access the elements
00363    *  by other means, and change their data such that the sorting
00364    *  order would be different, the %priority_queue will not re-sort
00365    *  the elements for you.  (How could it know to do so?)
00366   */
00367   template<typename _Tp, typename _Sequence = vector<_Tp>,
00368        typename _Compare  = less<typename _Sequence::value_type> >
00369     class priority_queue
00370     {
00371       // concept requirements
00372       typedef typename _Sequence::value_type _Sequence_value_type;
00373       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00374       __glibcxx_class_requires(_Sequence, _SequenceConcept)
00375       __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept)
00376       __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
00377       __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp,
00378                 _BinaryFunctionConcept)
00379 
00380     public:
00381       typedef typename _Sequence::value_type                value_type;
00382       typedef typename _Sequence::reference                 reference;
00383       typedef typename _Sequence::const_reference           const_reference;
00384       typedef typename _Sequence::size_type                 size_type;
00385       typedef          _Sequence                            container_type;
00386 
00387     protected:
00388       //  See queue::c for notes on these names.
00389       _Sequence  c;
00390       _Compare   comp;
00391 
00392     public:
00393       /**
00394        *  @brief  Default constructor creates no elements.
00395        */
00396 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00397       explicit
00398       priority_queue(const _Compare& __x = _Compare(),
00399              const _Sequence& __s = _Sequence())
00400       : c(__s), comp(__x)
00401       { std::make_heap(c.begin(), c.end(), comp); }
00402 #else
00403       explicit
00404       priority_queue(const _Compare& __x,
00405              const _Sequence& __s)
00406       : c(__s), comp(__x)
00407       { std::make_heap(c.begin(), c.end(), comp); }
00408 
00409       explicit
00410       priority_queue(const _Compare& __x = _Compare(),
00411              _Sequence&& __s = _Sequence())
00412       : c(std::move(__s)), comp(__x)
00413       { std::make_heap(c.begin(), c.end(), comp); }
00414 #endif
00415 
00416       /**
00417        *  @brief  Builds a %queue from a range.
00418        *  @param  __first  An input iterator.
00419        *  @param  __last  An input iterator.
00420        *  @param  __x  A comparison functor describing a strict weak ordering.
00421        *  @param  __s  An initial sequence with which to start.
00422        *
00423        *  Begins by copying @a __s, inserting a copy of the elements
00424        *  from @a [first,last) into the copy of @a __s, then ordering
00425        *  the copy according to @a __x.
00426        *
00427        *  For more information on function objects, see the
00428        *  documentation on @link functors functor base
00429        *  classes@endlink.
00430        */
00431 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00432       template<typename _InputIterator>
00433         priority_queue(_InputIterator __first, _InputIterator __last,
00434                const _Compare& __x = _Compare(),
00435                const _Sequence& __s = _Sequence())
00436     : c(__s), comp(__x)
00437         {
00438       __glibcxx_requires_valid_range(__first, __last);
00439       c.insert(c.end(), __first, __last);
00440       std::make_heap(c.begin(), c.end(), comp);
00441     }
00442 #else
00443       template<typename _InputIterator>
00444         priority_queue(_InputIterator __first, _InputIterator __last,
00445                const _Compare& __x,
00446                const _Sequence& __s)
00447     : c(__s), comp(__x)
00448         {
00449       __glibcxx_requires_valid_range(__first, __last);
00450       c.insert(c.end(), __first, __last);
00451       std::make_heap(c.begin(), c.end(), comp);
00452     }
00453 
00454       template<typename _InputIterator>
00455         priority_queue(_InputIterator __first, _InputIterator __last,
00456                const _Compare& __x = _Compare(),
00457                _Sequence&& __s = _Sequence())
00458     : c(std::move(__s)), comp(__x)
00459         {
00460       __glibcxx_requires_valid_range(__first, __last);
00461       c.insert(c.end(), __first, __last);
00462       std::make_heap(c.begin(), c.end(), comp);
00463     }
00464 #endif
00465 
00466       /**
00467        *  Returns true if the %queue is empty.
00468        */
00469       bool
00470       empty() const
00471       { return c.empty(); }
00472 
00473       /**  Returns the number of elements in the %queue.  */
00474       size_type
00475       size() const
00476       { return c.size(); }
00477 
00478       /**
00479        *  Returns a read-only (constant) reference to the data at the first
00480        *  element of the %queue.
00481        */
00482       const_reference
00483       top() const
00484       {
00485     __glibcxx_requires_nonempty();
00486     return c.front();
00487       }
00488 
00489       /**
00490        *  @brief  Add data to the %queue.
00491        *  @param  __x  Data to be added.
00492        *
00493        *  This is a typical %queue operation.
00494        *  The time complexity of the operation depends on the underlying
00495        *  sequence.
00496        */
00497       void
00498       push(const value_type& __x)
00499       {
00500     c.push_back(__x);
00501     std::push_heap(c.begin(), c.end(), comp);
00502       }
00503 
00504 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00505       void
00506       push(value_type&& __x)
00507       {
00508     c.push_back(std::move(__x));
00509     std::push_heap(c.begin(), c.end(), comp);
00510       }
00511 
00512       template<typename... _Args>
00513         void
00514         emplace(_Args&&... __args)
00515     {
00516       c.emplace_back(std::forward<_Args>(__args)...);
00517       std::push_heap(c.begin(), c.end(), comp);
00518     }
00519 #endif
00520 
00521       /**
00522        *  @brief  Removes first element.
00523        *
00524        *  This is a typical %queue operation.  It shrinks the %queue
00525        *  by one.  The time complexity of the operation depends on the
00526        *  underlying sequence.
00527        *
00528        *  Note that no data is returned, and if the first element's
00529        *  data is needed, it should be retrieved before pop() is
00530        *  called.
00531        */
00532       void
00533       pop()
00534       {
00535     __glibcxx_requires_nonempty();
00536     std::pop_heap(c.begin(), c.end(), comp);
00537     c.pop_back();
00538       }
00539 
00540 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00541       void
00542       swap(priority_queue& __pq)
00543       noexcept(noexcept(swap(c, __pq.c)) && noexcept(swap(comp, __pq.comp)))
00544       {
00545     using std::swap;
00546     swap(c, __pq.c);
00547     swap(comp, __pq.comp);
00548       }
00549 #endif
00550     };
00551 
00552   // No equality/comparison operators are provided for priority_queue.
00553 
00554 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00555   template<typename _Tp, typename _Sequence, typename _Compare>
00556     inline void
00557     swap(priority_queue<_Tp, _Sequence, _Compare>& __x,
00558      priority_queue<_Tp, _Sequence, _Compare>& __y)
00559     noexcept(noexcept(__x.swap(__y)))
00560     { __x.swap(__y); }
00561 
00562   template<typename _Tp, typename _Sequence, typename _Compare,
00563        typename _Alloc>
00564     struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc>
00565     : public uses_allocator<_Sequence, _Alloc>::type { };
00566 #endif
00567 
00568 _GLIBCXX_END_NAMESPACE_VERSION
00569 } // namespace
00570 
00571 #endif /* _STL_QUEUE_H */