70 template <
typename Iterator,
typename Compare>
75 template <
typename Iterator,
typename Compare>
81 template <
typename Iterator,
typename Compare>
85 template <
typename Iterator,
typename Compare>
90 template <
typename Iterator,
typename Compare>
93 typename std::iterator_traits<Iterator>::difference_type index,
97 template <
typename Iterator,
typename Compare>
100 typename std::iterator_traits<Iterator>::difference_type index,
104 template <
typename Iterator,
typename Compare>
108 template <
typename Iterator,
typename Compare>
112 template <
typename Iterator,
typename Compare>
117 template <
typename T>
127 namespace interval_heap_internal {
129 template <
typename Iterator,
typename Compare>
131 void sift_leaf (Iterator first, Iterator last,
132 typename std::iterator_traits<Iterator>::difference_type index,
135 template <
bool left_bound,
typename Iterator,
typename Offset,
typename Compare>
137 void sift_up (Iterator first, Offset index, Compare compare,Offset limit_child);
139 template <
typename Iterator,
typename Offset,
typename Compare>
141 void sift_leaf_max (Iterator first, Iterator last, Offset index,
142 Compare compare, Offset limit_child);
144 template <
typename Iterator,
typename Offset,
typename Compare>
146 void sift_leaf_min (Iterator first, Iterator last, Offset index,
147 Compare compare, Offset limit_child);
149 template <
bool left_bound,
typename Iterator,
typename Offset,
typename Compare>
151 void sift_down (Iterator first, Iterator last, Offset index, Compare compare,
169 template <
typename Iterator,
typename Compare>
171 typename std::iterator_traits<Iterator>::difference_type index,
178 sift_down<false, Iterator, Offset, Compare>(first, last, index,
compare,2);
180 sift_down<true, Iterator, Offset, Compare>(first, last, index,
compare, 2);
194 template <
typename Iterator,
typename Compare>
197 sift_leaf<Iterator, Compare>(first, last, (last - first) - 1, compare);
215 template <
typename Iterator,
typename Compare>
217 typename std::iterator_traits<Iterator>::difference_type index,
222 swap(*(first + index), *last);
224 update_interval_heap<Iterator, Compare>(first, last, index,
compare);
240 template <
typename Iterator,
typename Compare>
248 sift_down<true, Iterator, Offset, Compare>(first, last, 0,
compare, 2);
264 template <
typename Iterator,
typename Compare>
270 if (last - first <= 2)
273 swap(*(first + 1), *last);
275 sift_down<false, Iterator, Offset, Compare>(first, last, 1,
compare, 2);
288 template <
typename Iterator,
typename Compare>
290 Iterator cursor = last;
291 while (cursor != first) {
293 pop_interval_heap_max<Iterator, Compare>(first, cursor,
compare);
300 template <
typename Iterator,
typename Compare>
306 Offset index =
static_cast<Offset
>(0);
309 Offset index_end = last - first;
310 while (index < index_end) {
311 Iterator cursor = first + index;
313 if ((index & 1) &&
compare(*cursor, *(cursor - 1)))
317 Iterator
parent = first + ((index / 2 - 1) | 1);
324 if (
compare(*cursor, *(parent - 1)))
331 return first + index;
338 template <
typename Iterator,
typename Compare>
341 return (is_interval_heap_until<Iterator,Compare>(first, last, compare)
356 template <
typename Iterator,
typename Compare>
362 Offset index_end = last - first;
367 const Offset end_parent = index_end / 2 - 1;
369 Offset index = index_end ^ (index_end & 1);
372 const Offset stop = (index <= end_parent) ? (index * 2 + 2) : index_end;
375 if (
compare(*(first + index + 1), *(first + index)))
376 swap(*(first + index + 1), *(first + index));
378 sift_down<false, Iterator, Offset, Compare>(first, last, index + 1,
380 sift_down<true , Iterator, Offset, Compare>(first, last, index,
383 }
while (index >= 2);
386 namespace interval_heap_internal {
389 template <
bool left_bound,
typename Iterator,
typename Offset,
typename Compare>
390 void sift_up (Iterator first, Offset origin, Compare compare, Offset limit_child)
396 Offset index = origin;
398 while (index >= limit_child) {
399 const Offset
parent = ((index / 2 - 1) | 1) ^ (left_bound ? 1 : 0);
401 if (
compare(*(first + (left_bound ? index : parent)),
402 *(first + (left_bound ? parent : index)))) {
403 swap(*(first + index), *(first + parent));
412 template <
typename Iterator,
typename Offset,
typename Compare>
414 Compare compare, Offset limit_child)
418 const Offset index_end = last - first;
421 const Offset co_index = (index * 2 < index_end) ? (index * 2) : (index ^ 1);
422 if (
compare(*(first + index), *(first + co_index))) {
423 swap(*(first + index), *(first + co_index));
425 sift_up<true, Iterator, Offset, Compare>(first, co_index,
compare,limit_child);
427 sift_up<false, Iterator, Offset, Compare>(first, index,
compare,
432 template <
typename Iterator,
typename Offset,
typename Compare>
434 Compare compare, Offset limit_child)
438 const Offset index_end = last - first;
441 Offset co_index = index | 1;
443 if (co_index >= index_end) {
447 co_index = (co_index / 2 - 1) | 1;
449 if (
compare(*(first + co_index), *(first + index))) {
450 swap(*(first + index), *(first + co_index));
452 sift_up<false, Iterator, Offset, Compare>(first, co_index,
compare, limit_child);
454 sift_up<true, Iterator, Offset, Compare>(first, index,
compare, limit_child);
458 template <
bool left_bound,
typename Iterator,
typename Offset,
typename Compare>
459 void sift_down (Iterator first, Iterator last, Offset origin, Compare compare,
466 Offset index = origin;
468 const Offset index_end = last - first;
470 const Offset end_parent = index_end / 2 -
471 ((left_bound && ((index_end & 3) == 0)) ? 2 : 1);
473 while (index < end_parent) {
474 Offset child = index * 2 + (left_bound ? 2 : 1);
476 if (
compare(*(first + child + (left_bound ? 2 : 0)),
477 *(first + child + (left_bound ? 0 : 2))))
480 swap(*(first + index), *(first + child));
484 if (index <= end_parent + (left_bound ? 0 : 1)) {
485 Offset child = index * 2 + (left_bound ? 2 : 1);
486 if (child < index_end) {
488 if (!left_bound && (child + 1 < index_end)) {
490 bool swap_required =
compare(*(first + child), *(first + child + 1));
495 swap(*(first + index), *(first + child));
499 sift_leaf_min<Iterator, Offset, Compare>(first, last, index,
505 swap(*(first + index), *(first + child));
511 sift_leaf_min<Iterator, Offset, Compare>(first, last, index,
compare,
514 sift_leaf_max<Iterator, Offset, Compare>(first, last, index,
compare,
519 template <
typename Iterator,
typename Compare>
521 typename std::iterator_traits<Iterator>::difference_type index,
527 sift_leaf_max<Iterator, Offset, Compare>(first, last, index,
compare, 2);
529 sift_leaf_min<Iterator, Offset, Compare>(first, last, index,
compare, 2);