5
* Hewlett-Packard Company
7
* Copyright (c) 1996,1997
8
* Silicon Graphics Computer Systems, Inc.
11
* Moscow Center for SPARC Technology
16
* This material is provided "as is", with absolutely no warranty expressed
17
* or implied. Any use is at your own risk.
19
* Permission to use or copy this software for any purpose is hereby granted
20
* without fee, provided the above notices are retained on all copies.
21
* Permission to modify the code and to distribute modified code is granted,
22
* provided the above notices are retained, and a notice that the code was
23
* modified is included with the above copyright notice.
29
#ifndef _STLP_INTERNAL_HEAP_H
30
# include <stl/_heap.h>
33
#ifndef _STLP_INTERNAL_ITERATOR_BASE_H
34
# include <stl/_iterator_base.h>
39
template <class _RandomAccessIterator, class _Distance, class _Tp>
42
__push_heap(_RandomAccessIterator __first,
43
_Distance __holeIndex, _Distance __topIndex, _Tp __val)
45
_Distance __parent = (__holeIndex - 1) / 2;
46
while (__holeIndex > __topIndex && *(__first + __parent) < __val) {
47
*(__first + __holeIndex) = *(__first + __parent);
48
__holeIndex = __parent;
49
__parent = (__holeIndex - 1) / 2;
51
*(__first + __holeIndex) = __val;
54
template <class _RandomAccessIterator, class _Distance, class _Tp>
56
__push_heap_aux(_RandomAccessIterator __first,
57
_RandomAccessIterator __last, _Distance*, _Tp*)
59
__push_heap(__first, _Distance((__last - __first) - 1), _Distance(0),
63
template <class _RandomAccessIterator>
65
push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
67
__push_heap_aux(__first, __last,
68
_STLP_DISTANCE_TYPE(__first, _RandomAccessIterator), _STLP_VALUE_TYPE(__first, _RandomAccessIterator));
72
template <class _RandomAccessIterator, class _Distance, class _Tp,
76
__push_heap(_RandomAccessIterator __first, _Distance __holeIndex,
77
_Distance __topIndex, _Tp __val, _Compare __comp)
79
_Distance __parent = (__holeIndex - 1) / 2;
80
while (__holeIndex > __topIndex && __comp(*(__first + __parent), __val)) {
81
*(__first + __holeIndex) = *(__first + __parent);
82
__holeIndex = __parent;
83
__parent = (__holeIndex - 1) / 2;
85
*(__first + __holeIndex) = __val;
88
template <class _RandomAccessIterator, class _Compare,
89
class _Distance, class _Tp>
91
__push_heap_aux(_RandomAccessIterator __first,
92
_RandomAccessIterator __last, _Compare __comp,
95
__push_heap(__first, _Distance((__last - __first) - 1), _Distance(0),
96
_Tp(*(__last - 1)), __comp);
99
template <class _RandomAccessIterator, class _Compare>
101
push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
104
__push_heap_aux(__first, __last, __comp,
105
_STLP_DISTANCE_TYPE(__first, _RandomAccessIterator), _STLP_VALUE_TYPE(__first, _RandomAccessIterator));
108
template <class _RandomAccessIterator, class _Distance, class _Tp>
110
__adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
111
_Distance __len, _Tp __val) {
112
_Distance __topIndex = __holeIndex;
113
_Distance __secondChild = 2 * __holeIndex + 2;
114
while (__secondChild < __len) {
115
if (*(__first + __secondChild) < *(__first + (__secondChild - 1)))
117
*(__first + __holeIndex) = *(__first + __secondChild);
118
__holeIndex = __secondChild;
119
__secondChild = 2 * (__secondChild + 1);
121
if (__secondChild == __len) {
122
*(__first + __holeIndex) = *(__first + (__secondChild - 1));
123
__holeIndex = __secondChild - 1;
125
__push_heap(__first, __holeIndex, __topIndex, __val);
129
template <class _RandomAccessIterator, class _Tp>
131
__pop_heap_aux(_RandomAccessIterator __first, _RandomAccessIterator __last, _Tp*) {
132
__pop_heap(__first, __last - 1, __last - 1,
133
_Tp(*(__last - 1)), _STLP_DISTANCE_TYPE(__first, _RandomAccessIterator));
136
template <class _RandomAccessIterator>
137
void pop_heap(_RandomAccessIterator __first,
138
_RandomAccessIterator __last) {
139
__pop_heap_aux(__first, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIterator));
142
template <class _RandomAccessIterator, class _Distance,
143
class _Tp, class _Compare>
145
__adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
146
_Distance __len, _Tp __val, _Compare __comp)
148
_Distance __topIndex = __holeIndex;
149
_Distance __secondChild = 2 * __holeIndex + 2;
150
while (__secondChild < __len) {
151
if (__comp(*(__first + __secondChild), *(__first + (__secondChild - 1))))
153
*(__first + __holeIndex) = *(__first + __secondChild);
154
__holeIndex = __secondChild;
155
__secondChild = 2 * (__secondChild + 1);
157
if (__secondChild == __len) {
158
*(__first + __holeIndex) = *(__first + (__secondChild - 1));
159
__holeIndex = __secondChild - 1;
161
__push_heap(__first, __holeIndex, __topIndex, __val, __comp);
165
template <class _RandomAccessIterator, class _Tp, class _Compare>
167
__pop_heap_aux(_RandomAccessIterator __first,
168
_RandomAccessIterator __last, _Tp*, _Compare __comp)
170
__pop_heap(__first, __last - 1, __last - 1, _Tp(*(__last - 1)), __comp,
171
_STLP_DISTANCE_TYPE(__first, _RandomAccessIterator));
175
template <class _RandomAccessIterator, class _Compare>
177
pop_heap(_RandomAccessIterator __first,
178
_RandomAccessIterator __last, _Compare __comp)
180
__pop_heap_aux(__first, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIterator), __comp);
183
template <class _RandomAccessIterator, class _Tp, class _Distance>
186
__make_heap(_RandomAccessIterator __first,
187
_RandomAccessIterator __last, _Tp*, _Distance*)
189
if (__last - __first < 2) return;
190
_Distance __len = __last - __first;
191
_Distance __parent = (__len - 2)/2;
194
__adjust_heap(__first, __parent, __len, _Tp(*(__first + __parent)));
195
if (__parent == 0) return;
200
template <class _RandomAccessIterator>
202
make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
204
__make_heap(__first, __last,
205
_STLP_VALUE_TYPE(__first, _RandomAccessIterator), _STLP_DISTANCE_TYPE(__first, _RandomAccessIterator));
208
template <class _RandomAccessIterator, class _Compare,
209
class _Tp, class _Distance>
212
__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
213
_Compare __comp, _Tp*, _Distance*)
215
if (__last - __first < 2) return;
216
_Distance __len = __last - __first;
217
_Distance __parent = (__len - 2)/2;
220
__adjust_heap(__first, __parent, __len, _Tp(*(__first + __parent)),
222
if (__parent == 0) return;
227
template <class _RandomAccessIterator, class _Compare>
229
make_heap(_RandomAccessIterator __first,
230
_RandomAccessIterator __last, _Compare __comp)
232
__make_heap(__first, __last, __comp,
233
_STLP_VALUE_TYPE(__first, _RandomAccessIterator), _STLP_DISTANCE_TYPE(__first, _RandomAccessIterator));
238
#endif /* _STLP_HEAP_C */