-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_heap.h
258 lines (195 loc) · 6.29 KB
/
ml_heap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* @file ml_heap.h
* @brief
* @author malin [email protected]
* @date 2015年05月10日 星期日 11时12分49秒
* @version
* @note
*/
#ifndef __HEAP_INTERNAL_H
#define __HEAP_INTERNAL_H
#include "ml_config.h"
#include "ml_iterator_base.h"
__ML_BEGIN_NAMESPACE
// swim operation
template<typename RandomAccessIterator, typename Distance, typename T>
void __push_heap(RandomAccessIterator first, Distance top, Distance holdIndex, T value)
{
Distance parent = (holdIndex - 1) / 2;
while (holdIndex > top && *(first + parent) < value)
{
*(first + holdIndex) = *(first + parent);
holdIndex = parent;
parent = (holdIndex - 1) / 2;
}
*(first + holdIndex) = value;
}
template<typename RandomAccessIterator, typename Distance, typename T>
inline void __push_heap_aux(RandomAccessIterator first, RandomAccessIterator last, Distance*, T*)
{
Distance holdIndex = (last - first) - 1;
__push_heap(first, 0, holdIndex, *(last - 1));
}
template<typename RandomAccessIterator>
inline void push_heap(RandomAccessIterator first, RandomAccessIterator last)
{
__push_heap_aux(first, last, distance_type(first), value_type(first));
}
// swim operation
template<typename RandomAccessIterator, typename Compare, typename Distance, typename T>
void __push_heap(RandomAccessIterator first, Distance top, Distance holdIndex, T value, Compare comp)
{
Distance parent = (holdIndex - 1) / 2;
while (holdIndex > top && comp(*(first + parent), value))
{
*(first + holdIndex) = *(first + parent);
holdIndex = parent;
parent = (holdIndex - 1) / 2;
}
*(first + holdIndex) = value;
}
template<typename RandomAccessIterator, typename Compare, typename Distance, typename T>
inline void __push_heap_aux(RandomAccessIterator first, RandomAccessIterator last, Compare comp, Distance*, T*)
{
Distance holdIndex = (last - first) - 1;
__push_heap(first, 0, holdIndex, *(last - 1), comp);
}
template<typename RandomAccessIterator, typename Compare>
inline void push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
{
__push_heap_aux(first, last, comp, distance_type(first), value_type(first));
}
// sink operation
template<typename RandomAccessIterator, typename Distance, typename T>
void __pop_heap(RandomAccessIterator first, Distance holdIndex, Distance len, const T &value)
{
Distance rightchild = 2 * holdIndex + 2;
while (rightchild < len)
{
if (*(first + rightchild) < *(first + (rightchild - 1))) --rightchild;
if (value < *(first + rightchild))
{
*(first + holdIndex) = *(first + rightchild);
holdIndex = rightchild;
rightchild = 2 * holdIndex + 2;
}
else break;
}
if (rightchild == len)
{
--rightchild;
if (value < *(first + rightchild))
{
*(first + holdIndex) = *(first + rightchild);
holdIndex = rightchild;
}
}
*(first + holdIndex) = value;
}
template<typename RandomAccessIterator, typename Distance, typename T>
inline void __pop_heap_aux(RandomAccessIterator first, RandomAccessIterator last, Distance*, T*)
{
T value = *(last - 1);
*(last - 1) = *first;
Distance len = last - first - 1;
__pop_heap(first, 0, len, value);
}
template<typename RandomAccessIterator>
inline void pop_heap(RandomAccessIterator first, RandomAccessIterator last)
{
__pop_heap_aux(first, last, distance_type(first), value_type(first));
}
// sink operation
template<typename RandomAccessIterator, typename Compare, typename Distance, typename T>
void __pop_heap(RandomAccessIterator first, Distance holdIndex, Distance len, const T &value, Compare comp)
{
Distance rightchild = 2 * holdIndex + 2;
while (rightchild < len)
{
if (comp(*(first + rightchild), *(first + (rightchild - 1)))) --rightchild;
if (comp(value, *(first + rightchild)))
{
*(first + holdIndex) = *(first + rightchild);
holdIndex = rightchild;
rightchild = 2 * holdIndex + 2;
}
else break;
}
if (rightchild == len)
{
--rightchild;
if (comp(value, *(first + rightchild)))
{
*(first + holdIndex) = *(first + rightchild);
holdIndex = rightchild;
}
}
*(first + holdIndex) = value;
}
template<typename RandomAccessIterator, typename Compare, typename Distance, typename T>
inline void __pop_heap_aux(RandomAccessIterator first, RandomAccessIterator last, Compare comp, Distance*, T*)
{
T value = *(last - 1);
*(last - 1) = *first;
Distance len = last - first - 1;
__pop_heap(first, 0, len, value, comp);
}
template<typename RandomAccessIterator, typename Compare>
inline void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
{
__pop_heap_aux(first, last, comp, distance_type(first), value_type(first));
}
// the input interval must be a heap
template<typename RandomAccessIterator>
inline void sort_heap(RandomAccessIterator first, RandomAccessIterator last)
{
while (last - first > 1)
pop_heap(first, last--);
}
// the input interval must be a heap
template<typename RandomAccessIterator, typename Compare>
inline void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
{
while (last - first > 1)
pop_heap(first, last--, comp);
}
template<typename RandomAccessIterator, typename Distance, typename T>
void __make_heap_aux(RandomAccessIterator first, RandomAccessIterator last, Distance*, T*)
{
Distance len = last - first;
if (len < 2) return;
Distance holdIndex = (len - 2) / 2;
while (true)
{
T value = *(first + holdIndex);
__pop_heap(first, holdIndex, len, value);
if (holdIndex == 0) break;
--holdIndex;
}
}
template<typename RandomAccessIterator>
inline void make_heap(RandomAccessIterator first, RandomAccessIterator last)
{
__make_heap_aux(first, last, distance_type(first), value_type(first));
}
template<typename RandomAccessIterator, typename Compare, typename Distance, typename T>
void __make_heap_aux(RandomAccessIterator first, RandomAccessIterator last, Compare comp, Distance*, T*)
{
Distance len = last - first;
if (len < 2) return;
Distance holdIndex = (len - 2) / 2;
while (true)
{
T value = *(first + holdIndex);
__pop_heap(first, holdIndex, len, value, comp);
if (holdIndex == 0) break;
--holdIndex;
}
}
template<typename RandomAccessIterator, typename Compare>
inline void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
{
__make_heap_aux(first, last, comp, distance_type(first), value_type(first));
}
__ML_END_NAMESPACE
#endif // __HEAP_INTERNAL_H