1 // Heap implementation -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Silicon Graphics Computer Systems, Inc.
46 * Permission to use, copy, modify, distribute and sell this software
47 * and its documentation for any purpose is hereby granted without fee,
48 * provided that the above copyright notice appear in all copies and
49 * that both that copyright notice and this permission notice appear
50 * in supporting documentation. Silicon Graphics makes no
51 * representations about the suitability of this software for any
52 * purpose. It is provided "as is" without express or implied warranty.
55 /* NOTE: This is an internal header file, included by other STL headers.
56 * You should not attempt to use it directly.
59 #ifndef _CPP_BITS_STL_HEAP_H
60 #define _CPP_BITS_STL_HEAP_H 1
65 // Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap.
67 template<typename _RandomAccessIterator, typename _Distance, typename _Tp>
69 __push_heap(_RandomAccessIterator __first,
70 _Distance __holeIndex, _Distance __topIndex, _Tp __value)
72 _Distance __parent = (__holeIndex - 1) / 2;
73 while (__holeIndex > __topIndex && *(__first + __parent) < __value) {
74 *(__first + __holeIndex) = *(__first + __parent);
75 __holeIndex = __parent;
76 __parent = (__holeIndex - 1) / 2;
78 *(__first + __holeIndex) = __value;
81 template<typename _RandomAccessIterator>
83 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
85 typedef typename iterator_traits<_RandomAccessIterator>::value_type
87 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
90 // concept requirements
91 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
92 _RandomAccessIterator>)
93 __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
95 __push_heap(__first, _DistanceType((__last - __first) - 1), _DistanceType(0),
96 _ValueType(*(__last - 1)));
99 template<typename _RandomAccessIterator, typename _Distance, typename _Tp,
102 __push_heap(_RandomAccessIterator __first, _Distance __holeIndex,
103 _Distance __topIndex, _Tp __value, _Compare __comp)
105 _Distance __parent = (__holeIndex - 1) / 2;
106 while (__holeIndex > __topIndex && __comp(*(__first + __parent), __value)) {
107 *(__first + __holeIndex) = *(__first + __parent);
108 __holeIndex = __parent;
109 __parent = (__holeIndex - 1) / 2;
111 *(__first + __holeIndex) = __value;
114 template<typename _RandomAccessIterator, typename _Compare>
116 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
119 typedef typename iterator_traits<_RandomAccessIterator>::value_type
121 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
124 // concept requirements
125 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
126 _RandomAccessIterator>)
128 __push_heap(__first, _DistanceType((__last - __first) - 1), _DistanceType(0),
129 _ValueType(*(__last - 1)), __comp);
132 template<typename _RandomAccessIterator, typename _Distance, typename _Tp>
134 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
135 _Distance __len, _Tp __value)
137 _Distance __topIndex = __holeIndex;
138 _Distance __secondChild = 2 * __holeIndex + 2;
139 while (__secondChild < __len) {
140 if (*(__first + __secondChild) < *(__first + (__secondChild - 1)))
142 *(__first + __holeIndex) = *(__first + __secondChild);
143 __holeIndex = __secondChild;
144 __secondChild = 2 * (__secondChild + 1);
146 if (__secondChild == __len) {
147 *(__first + __holeIndex) = *(__first + (__secondChild - 1));
148 __holeIndex = __secondChild - 1;
150 __push_heap(__first, __holeIndex, __topIndex, __value);
153 template<typename _RandomAccessIterator, typename _Tp>
155 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
156 _RandomAccessIterator __result, _Tp __value)
158 typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
159 *__result = *__first;
160 __adjust_heap(__first, _Distance(0), _Distance(__last - __first), __value);
163 template<typename _RandomAccessIterator>
165 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
167 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
169 // concept requirements
170 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
171 _RandomAccessIterator>)
172 __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
174 __pop_heap(__first, __last - 1, __last - 1, _ValueType(*(__last - 1)));
177 template<typename _RandomAccessIterator, typename _Distance,
178 typename _Tp, typename _Compare>
180 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
181 _Distance __len, _Tp __value, _Compare __comp)
183 _Distance __topIndex = __holeIndex;
184 _Distance __secondChild = 2 * __holeIndex + 2;
185 while (__secondChild < __len) {
186 if (__comp(*(__first + __secondChild), *(__first + (__secondChild - 1))))
188 *(__first + __holeIndex) = *(__first + __secondChild);
189 __holeIndex = __secondChild;
190 __secondChild = 2 * (__secondChild + 1);
192 if (__secondChild == __len) {
193 *(__first + __holeIndex) = *(__first + (__secondChild - 1));
194 __holeIndex = __secondChild - 1;
196 __push_heap(__first, __holeIndex, __topIndex, __value, __comp);
199 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
201 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
202 _RandomAccessIterator __result, _Tp __value, _Compare __comp)
204 typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
205 *__result = *__first;
206 __adjust_heap(__first, _Distance(0), _Distance(__last - __first),
210 template<typename _RandomAccessIterator, typename _Compare>
212 pop_heap(_RandomAccessIterator __first,
213 _RandomAccessIterator __last, _Compare __comp)
215 // concept requirements
216 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
217 _RandomAccessIterator>)
219 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
220 __pop_heap(__first, __last - 1, __last - 1, _ValueType(*(__last - 1)), __comp);
223 template<typename _RandomAccessIterator>
225 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
227 typedef typename iterator_traits<_RandomAccessIterator>::value_type
229 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
232 // concept requirements
233 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
234 _RandomAccessIterator>)
235 __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
237 if (__last - __first < 2) return;
238 _DistanceType __len = __last - __first;
239 _DistanceType __parent = (__len - 2)/2;
242 __adjust_heap(__first, __parent, __len, _ValueType(*(__first + __parent)));
243 if (__parent == 0) return;
248 template<typename _RandomAccessIterator, typename _Compare>
250 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
253 typedef typename iterator_traits<_RandomAccessIterator>::value_type
255 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
258 // concept requirements
259 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
260 _RandomAccessIterator>)
262 if (__last - __first < 2) return;
263 _DistanceType __len = __last - __first;
264 _DistanceType __parent = (__len - 2)/2;
267 __adjust_heap(__first, __parent, __len,
268 _ValueType(*(__first + __parent)), __comp);
269 if (__parent == 0) return;
274 template<typename _RandomAccessIterator>
276 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
278 // concept requirements
279 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
280 _RandomAccessIterator>)
281 __glibcpp_function_requires(_LessThanComparableConcept<
282 typename iterator_traits<_RandomAccessIterator>::value_type>)
284 while (__last - __first > 1)
285 pop_heap(__first, __last--);
288 template<typename _RandomAccessIterator, typename _Compare>
290 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
293 // concept requirements
294 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
295 _RandomAccessIterator>)
297 while (__last - __first > 1)
298 pop_heap(__first, __last--, __comp);
303 #endif /* _CPP_BITS_STL_HEAP_H */