libstdc++
stl_heap.h
Go to the documentation of this file.
00001 // Heap implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
00004 // 2011 Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /*
00027  *
00028  * Copyright (c) 1994
00029  * Hewlett-Packard Company
00030  *
00031  * Permission to use, copy, modify, distribute and sell this software
00032  * and its documentation for any purpose is hereby granted without fee,
00033  * provided that the above copyright notice appear in all copies and
00034  * that both that copyright notice and this permission notice appear
00035  * in supporting documentation.  Hewlett-Packard Company makes no
00036  * representations about the suitability of this software for any
00037  * purpose.  It is provided "as is" without express or implied warranty.
00038  *
00039  * Copyright (c) 1997
00040  * Silicon Graphics Computer Systems, Inc.
00041  *
00042  * Permission to use, copy, modify, distribute and sell this software
00043  * and its documentation for any purpose is hereby granted without fee,
00044  * provided that the above copyright notice appear in all copies and
00045  * that both that copyright notice and this permission notice appear
00046  * in supporting documentation.  Silicon Graphics makes no
00047  * representations about the suitability of this software for any
00048  * purpose.  It is provided "as is" without express or implied warranty.
00049  */
00050 
00051 /** @file bits/stl_heap.h
00052  *  This is an internal header file, included by other library headers.
00053  *  Do not attempt to use it directly. @headername{queue}
00054  */
00055 
00056 #ifndef _STL_HEAP_H
00057 #define _STL_HEAP_H 1
00058 
00059 #include <debug/debug.h>
00060 #include <bits/move.h>
00061 
00062 namespace std _GLIBCXX_VISIBILITY(default)
00063 {
00064 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00065 
00066   /**
00067    * @defgroup heap_algorithms Heap
00068    * @ingroup sorting_algorithms
00069    */
00070 
00071   template<typename _RandomAccessIterator, typename _Distance>
00072     _Distance
00073     __is_heap_until(_RandomAccessIterator __first, _Distance __n)
00074     {
00075       _Distance __parent = 0;
00076       for (_Distance __child = 1; __child < __n; ++__child)
00077     {
00078       if (__first[__parent] < __first[__child])
00079         return __child;
00080       if ((__child & 1) == 0)
00081         ++__parent;
00082     }
00083       return __n;
00084     }
00085 
00086   template<typename _RandomAccessIterator, typename _Distance,
00087        typename _Compare>
00088     _Distance
00089     __is_heap_until(_RandomAccessIterator __first, _Distance __n,
00090             _Compare __comp)
00091     {
00092       _Distance __parent = 0;
00093       for (_Distance __child = 1; __child < __n; ++__child)
00094     {
00095       if (__comp(__first[__parent], __first[__child]))
00096         return __child;
00097       if ((__child & 1) == 0)
00098         ++__parent;
00099     }
00100       return __n;
00101     }
00102 
00103   // __is_heap, a predicate testing whether or not a range is a heap.
00104   // This function is an extension, not part of the C++ standard.
00105   template<typename _RandomAccessIterator, typename _Distance>
00106     inline bool
00107     __is_heap(_RandomAccessIterator __first, _Distance __n)
00108     { return std::__is_heap_until(__first, __n) == __n; }
00109 
00110   template<typename _RandomAccessIterator, typename _Compare,
00111        typename _Distance>
00112     inline bool
00113     __is_heap(_RandomAccessIterator __first, _Compare __comp, _Distance __n)
00114     { return std::__is_heap_until(__first, __n, __comp) == __n; }
00115 
00116   template<typename _RandomAccessIterator>
00117     inline bool
00118     __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
00119     { return std::__is_heap(__first, std::distance(__first, __last)); }
00120 
00121   template<typename _RandomAccessIterator, typename _Compare>
00122     inline bool
00123     __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
00124           _Compare __comp)
00125     { return std::__is_heap(__first, __comp, std::distance(__first, __last)); }
00126 
00127   // Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap,
00128   // + is_heap and is_heap_until in C++0x.
00129 
00130   template<typename _RandomAccessIterator, typename _Distance, typename _Tp>
00131     void
00132     __push_heap(_RandomAccessIterator __first,
00133         _Distance __holeIndex, _Distance __topIndex, _Tp __value)
00134     {
00135       _Distance __parent = (__holeIndex - 1) / 2;
00136       while (__holeIndex > __topIndex && *(__first + __parent) < __value)
00137     {
00138       *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
00139       __holeIndex = __parent;
00140       __parent = (__holeIndex - 1) / 2;
00141     }
00142       *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
00143     }
00144 
00145   /**
00146    *  @brief  Push an element onto a heap.
00147    *  @param  __first  Start of heap.
00148    *  @param  __last   End of heap + element.
00149    *  @ingroup heap_algorithms
00150    *
00151    *  This operation pushes the element at last-1 onto the valid heap
00152    *  over the range [__first,__last-1).  After completion,
00153    *  [__first,__last) is a valid heap.
00154   */
00155   template<typename _RandomAccessIterator>
00156     inline void
00157     push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
00158     {
00159       typedef typename iterator_traits<_RandomAccessIterator>::value_type
00160       _ValueType;
00161       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
00162       _DistanceType;
00163 
00164       // concept requirements
00165       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
00166         _RandomAccessIterator>)
00167       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
00168       __glibcxx_requires_valid_range(__first, __last);
00169       __glibcxx_requires_heap(__first, __last - 1);
00170 
00171       _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
00172       std::__push_heap(__first, _DistanceType((__last - __first) - 1),
00173                _DistanceType(0), _GLIBCXX_MOVE(__value));
00174     }
00175 
00176   template<typename _RandomAccessIterator, typename _Distance, typename _Tp,
00177        typename _Compare>
00178     void
00179     __push_heap(_RandomAccessIterator __first, _Distance __holeIndex,
00180         _Distance __topIndex, _Tp __value, _Compare __comp)
00181     {
00182       _Distance __parent = (__holeIndex - 1) / 2;
00183       while (__holeIndex > __topIndex
00184          && __comp(*(__first + __parent), __value))
00185     {
00186       *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
00187       __holeIndex = __parent;
00188       __parent = (__holeIndex - 1) / 2;
00189     }
00190       *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
00191     }
00192 
00193   /**
00194    *  @brief  Push an element onto a heap using comparison functor.
00195    *  @param  __first  Start of heap.
00196    *  @param  __last   End of heap + element.
00197    *  @param  __comp   Comparison functor.
00198    *  @ingroup heap_algorithms
00199    *
00200    *  This operation pushes the element at __last-1 onto the valid
00201    *  heap over the range [__first,__last-1).  After completion,
00202    *  [__first,__last) is a valid heap.  Compare operations are
00203    *  performed using comp.
00204   */
00205   template<typename _RandomAccessIterator, typename _Compare>
00206     inline void
00207     push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
00208           _Compare __comp)
00209     {
00210       typedef typename iterator_traits<_RandomAccessIterator>::value_type
00211       _ValueType;
00212       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
00213       _DistanceType;
00214 
00215       // concept requirements
00216       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
00217         _RandomAccessIterator>)
00218       __glibcxx_requires_valid_range(__first, __last);
00219       __glibcxx_requires_heap_pred(__first, __last - 1, __comp);
00220 
00221       _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
00222       std::__push_heap(__first, _DistanceType((__last - __first) - 1),
00223                _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
00224     }
00225 
00226   template<typename _RandomAccessIterator, typename _Distance, typename _Tp>
00227     void
00228     __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
00229           _Distance __len, _Tp __value)
00230     {
00231       const _Distance __topIndex = __holeIndex;
00232       _Distance __secondChild = __holeIndex;
00233       while (__secondChild < (__len - 1) / 2)
00234     {
00235       __secondChild = 2 * (__secondChild + 1);
00236       if (*(__first + __secondChild) < *(__first + (__secondChild - 1)))
00237         __secondChild--;
00238       *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
00239       __holeIndex = __secondChild;
00240     }
00241       if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
00242     {
00243       __secondChild = 2 * (__secondChild + 1);
00244       *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
00245                              + (__secondChild - 1)));
00246       __holeIndex = __secondChild - 1;
00247     }
00248       std::__push_heap(__first, __holeIndex, __topIndex,
00249                _GLIBCXX_MOVE(__value));
00250     }
00251 
00252   template<typename _RandomAccessIterator>
00253     inline void
00254     __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
00255            _RandomAccessIterator __result)
00256     {
00257       typedef typename iterator_traits<_RandomAccessIterator>::value_type
00258     _ValueType;
00259       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
00260     _DistanceType;
00261 
00262       _ValueType __value = _GLIBCXX_MOVE(*__result);
00263       *__result = _GLIBCXX_MOVE(*__first);
00264       std::__adjust_heap(__first, _DistanceType(0),
00265              _DistanceType(__last - __first),
00266              _GLIBCXX_MOVE(__value));
00267     }
00268 
00269   /**
00270    *  @brief  Pop an element off a heap.
00271    *  @param  __first  Start of heap.
00272    *  @param  __last   End of heap.
00273    *  @pre    [__first, __last) is a valid, non-empty range.
00274    *  @ingroup heap_algorithms
00275    *
00276    *  This operation pops the top of the heap.  The elements __first
00277    *  and __last-1 are swapped and [__first,__last-1) is made into a
00278    *  heap.
00279   */
00280   template<typename _RandomAccessIterator>
00281     inline void
00282     pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
00283     {
00284       typedef typename iterator_traits<_RandomAccessIterator>::value_type
00285     _ValueType;
00286 
00287       // concept requirements
00288       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
00289         _RandomAccessIterator>)
00290       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
00291       __glibcxx_requires_non_empty_range(__first, __last);
00292       __glibcxx_requires_valid_range(__first, __last);
00293       __glibcxx_requires_heap(__first, __last);
00294 
00295       --__last;
00296       std::__pop_heap(__first, __last, __last);
00297     }
00298 
00299   template<typename _RandomAccessIterator, typename _Distance,
00300        typename _Tp, typename _Compare>
00301     void
00302     __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
00303           _Distance __len, _Tp __value, _Compare __comp)
00304     {
00305       const _Distance __topIndex = __holeIndex;
00306       _Distance __secondChild = __holeIndex;
00307       while (__secondChild < (__len - 1) / 2)
00308     {
00309       __secondChild = 2 * (__secondChild + 1);
00310       if (__comp(*(__first + __secondChild),
00311              *(__first + (__secondChild - 1))))
00312         __secondChild--;
00313       *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
00314       __holeIndex = __secondChild;
00315     }
00316       if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
00317     {
00318       __secondChild = 2 * (__secondChild + 1);
00319       *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
00320                              + (__secondChild - 1)));
00321       __holeIndex = __secondChild - 1;
00322     }
00323       std::__push_heap(__first, __holeIndex, __topIndex, 
00324                _GLIBCXX_MOVE(__value), __comp);      
00325     }
00326 
00327   template<typename _RandomAccessIterator, typename _Compare>
00328     inline void
00329     __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
00330            _RandomAccessIterator __result, _Compare __comp)
00331     {
00332       typedef typename iterator_traits<_RandomAccessIterator>::value_type
00333     _ValueType;
00334       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
00335     _DistanceType;
00336 
00337       _ValueType __value = _GLIBCXX_MOVE(*__result);
00338       *__result = _GLIBCXX_MOVE(*__first);
00339       std::__adjust_heap(__first, _DistanceType(0),
00340              _DistanceType(__last - __first),
00341              _GLIBCXX_MOVE(__value), __comp);
00342     }
00343 
00344   /**
00345    *  @brief  Pop an element off a heap using comparison functor.
00346    *  @param  __first  Start of heap.
00347    *  @param  __last   End of heap.
00348    *  @param  __comp   Comparison functor to use.
00349    *  @ingroup heap_algorithms
00350    *
00351    *  This operation pops the top of the heap.  The elements __first
00352    *  and __last-1 are swapped and [__first,__last-1) is made into a
00353    *  heap.  Comparisons are made using comp.
00354   */
00355   template<typename _RandomAccessIterator, typename _Compare>
00356     inline void
00357     pop_heap(_RandomAccessIterator __first,
00358          _RandomAccessIterator __last, _Compare __comp)
00359     {
00360       // concept requirements
00361       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
00362         _RandomAccessIterator>)
00363       __glibcxx_requires_valid_range(__first, __last);
00364       __glibcxx_requires_non_empty_range(__first, __last);
00365       __glibcxx_requires_heap_pred(__first, __last, __comp);
00366 
00367       --__last;
00368       std::__pop_heap(__first, __last, __last, __comp);
00369     }
00370 
00371   /**
00372    *  @brief  Construct a heap over a range.
00373    *  @param  __first  Start of heap.
00374    *  @param  __last   End of heap.
00375    *  @ingroup heap_algorithms
00376    *
00377    *  This operation makes the elements in [__first,__last) into a heap.
00378   */
00379   template<typename _RandomAccessIterator>
00380     void
00381     make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
00382     {
00383       typedef typename iterator_traits<_RandomAccessIterator>::value_type
00384       _ValueType;
00385       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
00386       _DistanceType;
00387 
00388       // concept requirements
00389       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
00390         _RandomAccessIterator>)
00391       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
00392       __glibcxx_requires_valid_range(__first, __last);
00393 
00394       if (__last - __first < 2)
00395     return;
00396 
00397       const _DistanceType __len = __last - __first;
00398       _DistanceType __parent = (__len - 2) / 2;
00399       while (true)
00400     {
00401       _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
00402       std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value));
00403       if (__parent == 0)
00404         return;
00405       __parent--;
00406     }
00407     }
00408 
00409   /**
00410    *  @brief  Construct a heap over a range using comparison functor.
00411    *  @param  __first  Start of heap.
00412    *  @param  __last   End of heap.
00413    *  @param  __comp   Comparison functor to use.
00414    *  @ingroup heap_algorithms
00415    *
00416    *  This operation makes the elements in [__first,__last) into a heap.
00417    *  Comparisons are made using __comp.
00418   */
00419   template<typename _RandomAccessIterator, typename _Compare>
00420     void
00421     make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
00422           _Compare __comp)
00423     {
00424       typedef typename iterator_traits<_RandomAccessIterator>::value_type
00425       _ValueType;
00426       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
00427       _DistanceType;
00428 
00429       // concept requirements
00430       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
00431         _RandomAccessIterator>)
00432       __glibcxx_requires_valid_range(__first, __last);
00433 
00434       if (__last - __first < 2)
00435     return;
00436 
00437       const _DistanceType __len = __last - __first;
00438       _DistanceType __parent = (__len - 2) / 2;
00439       while (true)
00440     {
00441       _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
00442       std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
00443                  __comp);
00444       if (__parent == 0)
00445         return;
00446       __parent--;
00447     }
00448     }
00449 
00450   /**
00451    *  @brief  Sort a heap.
00452    *  @param  __first  Start of heap.
00453    *  @param  __last   End of heap.
00454    *  @ingroup heap_algorithms
00455    *
00456    *  This operation sorts the valid heap in the range [__first,__last).
00457   */
00458   template<typename _RandomAccessIterator>
00459     void
00460     sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
00461     {
00462       // concept requirements
00463       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
00464         _RandomAccessIterator>)
00465       __glibcxx_function_requires(_LessThanComparableConcept<
00466         typename iterator_traits<_RandomAccessIterator>::value_type>)
00467       __glibcxx_requires_valid_range(__first, __last);
00468       __glibcxx_requires_heap(__first, __last);
00469 
00470       while (__last - __first > 1)
00471     {
00472       --__last;
00473       std::__pop_heap(__first, __last, __last);
00474     }
00475     }
00476 
00477   /**
00478    *  @brief  Sort a heap using comparison functor.
00479    *  @param  __first  Start of heap.
00480    *  @param  __last   End of heap.
00481    *  @param  __comp   Comparison functor to use.
00482    *  @ingroup heap_algorithms
00483    *
00484    *  This operation sorts the valid heap in the range [__first,__last).
00485    *  Comparisons are made using __comp.
00486   */
00487   template<typename _RandomAccessIterator, typename _Compare>
00488     void
00489     sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
00490           _Compare __comp)
00491     {
00492       // concept requirements
00493       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
00494         _RandomAccessIterator>)
00495       __glibcxx_requires_valid_range(__first, __last);
00496       __glibcxx_requires_heap_pred(__first, __last, __comp);
00497 
00498       while (__last - __first > 1)
00499     {
00500       --__last;
00501       std::__pop_heap(__first, __last, __last, __comp);
00502     }
00503     }
00504 
00505 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00506   /**
00507    *  @brief  Search the end of a heap.
00508    *  @param  __first  Start of range.
00509    *  @param  __last   End of range.
00510    *  @return  An iterator pointing to the first element not in the heap.
00511    *  @ingroup heap_algorithms
00512    *
00513    *  This operation returns the last iterator i in [__first, __last) for which
00514    *  the range [__first, i) is a heap.
00515   */
00516   template<typename _RandomAccessIterator>
00517     inline _RandomAccessIterator
00518     is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
00519     {
00520       // concept requirements
00521       __glibcxx_function_requires(_RandomAccessIteratorConcept<
00522         _RandomAccessIterator>)
00523       __glibcxx_function_requires(_LessThanComparableConcept<
00524         typename iterator_traits<_RandomAccessIterator>::value_type>)
00525       __glibcxx_requires_valid_range(__first, __last);
00526 
00527       return __first + std::__is_heap_until(__first, std::distance(__first,
00528                                    __last));
00529     }
00530 
00531   /**
00532    *  @brief  Search the end of a heap using comparison functor.
00533    *  @param  __first  Start of range.
00534    *  @param  __last   End of range.
00535    *  @param  __comp   Comparison functor to use.
00536    *  @return  An iterator pointing to the first element not in the heap.
00537    *  @ingroup heap_algorithms
00538    *
00539    *  This operation returns the last iterator i in [__first, __last) for which
00540    *  the range [__first, i) is a heap.  Comparisons are made using __comp.
00541   */
00542   template<typename _RandomAccessIterator, typename _Compare>
00543     inline _RandomAccessIterator
00544     is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
00545           _Compare __comp)
00546     {
00547       // concept requirements
00548       __glibcxx_function_requires(_RandomAccessIteratorConcept<
00549         _RandomAccessIterator>)
00550       __glibcxx_requires_valid_range(__first, __last);
00551 
00552       return __first + std::__is_heap_until(__first, std::distance(__first,
00553                                    __last),
00554                         __comp);
00555     }
00556 
00557   /**
00558    *  @brief  Determines whether a range is a heap.
00559    *  @param  __first  Start of range.
00560    *  @param  __last   End of range.
00561    *  @return  True if range is a heap, false otherwise.
00562    *  @ingroup heap_algorithms
00563   */
00564   template<typename _RandomAccessIterator>
00565     inline bool
00566     is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
00567     { return std::is_heap_until(__first, __last) == __last; }
00568 
00569   /**
00570    *  @brief  Determines whether a range is a heap using comparison functor.
00571    *  @param  __first  Start of range.
00572    *  @param  __last   End of range.
00573    *  @param  __comp   Comparison functor to use.
00574    *  @return  True if range is a heap, false otherwise.
00575    *  @ingroup heap_algorithms
00576   */
00577   template<typename _RandomAccessIterator, typename _Compare>
00578     inline bool
00579     is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
00580         _Compare __comp)
00581     { return std::is_heap_until(__first, __last, __comp) == __last; }
00582 #endif
00583 
00584 _GLIBCXX_END_NAMESPACE_VERSION
00585 } // namespace
00586 
00587 #endif /* _STL_HEAP_H */