-
Notifications
You must be signed in to change notification settings - Fork 37
/
heap.hpp
190 lines (157 loc) · 4.84 KB
/
heap.hpp
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
#ifndef _HEAP_H_
#define _HEAP_H_
namespace mystl {
template <typename RandomIterator, typename Comp>
void push_heap( RandomIterator beg, RandomIterator end, Comp comp )
{
if( end - beg < 2 )
{
return;
}
auto elem = std::move( *( end - 1 ) );
auto elemIndex = end - beg - 1;
const decltype(elemIndex) rootIndex = 0;
auto parentIndex = ( elemIndex - 1 ) / 2;
while( elemIndex > rootIndex && comp( *( beg + parentIndex ), elem ) )
{
*( beg + elemIndex ) = std::move( *( beg + parentIndex ) );
elemIndex = parentIndex;
parentIndex = ( elemIndex - 1 ) / 2;
}
*( beg + elemIndex ) = std::move( elem );
}
template <typename RandomIterator>
void push_heap( RandomIterator beg, RandomIterator end )
{
mystl::push_heap( beg, end, std::less<decltype(*beg)>() );
}
template <typename RandomIterator, typename Distance, typename Comp>
void fixDown( RandomIterator beg, Distance size, Distance startIndex, Comp comp )
{
auto elem = std::move( *( beg + startIndex ) );
auto nodeIndex = startIndex;
auto rightChild = startIndex;
while( rightChild * 2 + 2 < size )
{
rightChild = 2 * rightChild + 2;
if( comp( *( beg + rightChild ), *( beg + rightChild - 1 ) ) )
{
--rightChild;
}
if( !comp( elem, *( beg + rightChild ) ) )
{
break;
}
*( beg + nodeIndex ) = std::move( *( beg + rightChild ) );
nodeIndex = rightChild;
}
if ( rightChild * 2 + 2 == size )
{
rightChild = 2 * rightChild + 1;
if( comp( elem, *( beg + rightChild ) ) )
{
*(beg + nodeIndex ) = std::move( *( beg + rightChild ) );
nodeIndex = rightChild;
}
}
*( beg + nodeIndex ) = std::move( elem );
}
template <typename RandomIterator, typename Distance>
void fixDown( RandomIterator beg, Distance size, Distance startIndex )
{
mystl::fixDown( beg, size, startIndex, std::less<decltype(*beg)>() );
}
template <typename RandomIterator, typename Comp>
void pop_heap( RandomIterator beg, RandomIterator end, Comp comp )
{
if( end - beg < 2 )
{
return;
}
std::swap( *beg, *( --end ) );
auto size = end - beg;
decltype(size) index = 0;
mystl::fixDown( beg, size, index, comp );
}
template <typename RandomIterator>
void pop_heap( RandomIterator beg, RandomIterator end )
{
mystl::pop_heap( beg, end, std::less<decltype(*beg)>() );
}
template <typename RandomIterator, typename Comp>
void make_heap( RandomIterator beg, RandomIterator end, Comp comp )
{
if( end - beg < 2 ) {
return;
}
const auto size = end - beg;
const auto lastIndex = size - 1;
for( auto index = ( lastIndex - 1 ) / 2; index >= 0; --index )
{
mystl::fixDown( beg, size, index, comp );
}
}
template <typename RandomIterator>
void make_heap( RandomIterator beg, RandomIterator end )
{
mystl::make_heap( beg, end, std::less<decltype(*beg)>() );
}
template<typename RandomIterator, typename Comp>
void sort_heap( RandomIterator beg, RandomIterator end, Comp comp )
{
while( end - beg > 1 )
{
mystl::pop_heap(beg, end--, comp );
}
}
template<typename RandomIterator>
void sort_heap( RandomIterator beg, RandomIterator end )
{
mystl::sort_heap( beg, end, std::less<decltype(*beg)>() );
}
template <typename RandomIterator, typename Comp>
void heap_sort( RandomIterator beg, RandomIterator end, Comp comp )
{
mystl::make_heap( beg, end, comp );
mystl::sort_heap( beg, end, comp );
}
template <typename RandomIterator>
void heap_sort( RandomIterator beg, RandomIterator end )
{
mystl::heap_sort( beg, end, std::less<decltype(*beg)>() );
}
template <typename RandomIterator, typename BinaryPredicate>
RandomIterator is_heap_until( RandomIterator beg, RandomIterator end, BinaryPredicate predicate )
{
if( end - beg < 2 )
{
return end;
}
auto size = end - beg;
for( decltype(size) index = 1; index < size; ++index )
{
auto parentIndex = ( index - 1 ) / 2;
if( predicate( *( beg + parentIndex ), *( beg + index ) ) )
{
return beg + index;
}
}
return end;
}
template <typename RandomIterator>
RandomIterator is_heap_until( RandomIterator beg, RandomIterator end )
{
return mystl::is_heap_until( beg, end, std::less<decltype(*beg)>{} );
}
template <typename RandomIterator, typename BinaryPredicate>
bool is_heap( RandomIterator beg, RandomIterator end, BinaryPredicate predicate )
{
return mystl::is_heap_until( beg, end, predicate ) == end;
}
template <typename RandomIterator>
bool is_heap( RandomIterator beg, RandomIterator end )
{
return mystl::is_heap( beg, end, std::less<decltype(*beg)>{} );
}
}; // namespace mystl
#endif /* _HEAP_H_ */