-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval_heap.hpp
783 lines (735 loc) · 31.5 KB
/
interval_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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*----------------------------------------------------------------------------*\
| Copyright (C) 2013 Nathaniel McClatchey |
| Released under the Boost Software License Version 1.0, which may be found |
| at http://www.boost.org/LICENSE_1_0.txt |
\*----------------------------------------------------------------------------*/
/*! @file interval_heap.hpp
// interval_heap.hpp provides functions for creating interval heaps by
// rearranging a range of elements. A valid interval heap has the following
// properties:
// Given elements A and B, A is less than B iff compare(A,B), where compare
// is a binary functor describing a strict weak ordering.
// Left and right bounds are those with even and odd indices, respectively.
// A right bound is never less than its corresponding left bound.
// A left bound is never less than the left bound of its parent.
// A right bound is never less than the right bounds of its children.
// @par Exception safety:
// 1. Pre-C++11: If the comparison and swap don't throw, neither do any of the
// interval-heap functions. \n
// 2. C++11: If comparison, move-assign, and move-construct do not throw,
// neither do any of the interval-heap functions.
// @par Thread safety:
// No static variables are modified by any operation. \n
*/
#ifndef BOOST_HEAP_INTERVAL_HEAP_HPP_
#define BOOST_HEAP_INTERVAL_HEAP_HPP_
#ifndef __cplusplus
#error interval_heap.hpp requires a C++ compiler.
#endif
// Grab std::swap and std::move (if available), while avoiding extra baggage.
#if (__cplusplus >= 201103L)
#include <utility>
#else
#include <algorithm>
#endif
// Grab iterator data, for better use of templates.
#include <iterator>
// Bulk-loading operation is well-suited to threading.
#ifndef BOOST_HEAP_INTERVAL_HEAP_USE_STD_THREAD
#define BOOST_HEAP_INTERVAL_HEAP_USE_STD_THREAD false
#endif
#if (BOOST_HEAP_INTERVAL_HEAP_USE_STD_THREAD == true)
#include <thread>
#endif
namespace boost {
namespace heap {
//------------------------------User-Accessible---------------------------------
//! @brief Moves elements in [first,last) to form an interval heap.
template <typename Iterator, typename Compare>
void make_interval_heap (Iterator first, Iterator last, Compare compare);
//! @brief Expands the interval heap to include the element at last-1.
template <typename Iterator, typename Compare>
void push_interval_heap (Iterator first, Iterator last, Compare compare);
//!@{
//! @brief Moves a minimal element to the back of the range for popping.
template <typename Iterator, typename Compare>
void pop_interval_heap_min (Iterator first, Iterator last, Compare compare);
//! @brief Moves a maximal element to the back of the range for popping.
template <typename Iterator, typename Compare>
void pop_interval_heap_max (Iterator first, Iterator last, Compare compare);
//! @brief Moves an element to the back of the range for popping.
template <typename Iterator, typename Compare>
void pop_interval_heap (Iterator first, Iterator last,
typename std::iterator_traits<Iterator>::difference_type index,
Compare compare);
//!@}
//! @brief Restores the interval-heap property if one element violates it.
template <typename Iterator, typename Compare>
void update_interval_heap (Iterator first, Iterator last,
typename std::iterator_traits<Iterator>::difference_type index,
Compare compare);
//! @brief Sorts an interval heap in ascending order.
template <typename Iterator, typename Compare>
void sort_interval_heap (Iterator first, Iterator last, Compare compare);
//! @brief Finds the largest subrange that qualifies as an interval heap.
template <typename Iterator, typename Compare>
Iterator is_interval_heap_until (Iterator first, Iterator last,Compare compare);
//! @brief Checks whether the range is an interval heap.
template <typename Iterator, typename Compare>
bool is_interval_heap (Iterator first, Iterator last, Compare compare);
//-------------------------------Book-Keeping-----------------------------------
/// \cond false
namespace interval_heap_internal {
#if (BOOST_HEAP_INTERVAL_HEAP_USE_STD_THREAD == true)
/*! @brief Minimum number of elements in heap layer before threads branch. Can
// be used to reduce page thrashing, but not usually required. The threaded
// bulk-loading operation has very good locality of reference, and very few
// operations are performed near the top of the heap.
*/
static constexpr int kBranchMin = 0;//;//1 << 2;//12;
//! @brief Minimum number of elements in heap before threading is considered.
static constexpr int kThreadMin = 1 << 5;
/* This parallel version of the heap-maker uses divide-and-conquer methods to
// distribute the task amongst the cores.
*/
//! @brief Internal function for threaded bulk-load.
template <typename Iterator, typename Compare, typename Offset>
void make_block (Iterator, Iterator, Compare, Offset, Offset, unsigned int);
#endif
//! @brief Internal function for single-threaded bulk-load.
template <typename Iterator, typename Compare>
void make_full (Iterator first, Iterator last, Compare compare);
//! @brief Restores the interval-heap property if one leaf element violates it.
template <typename Iterator, typename Compare>
void sift_leaf (Iterator first, Iterator last,
typename std::iterator_traits<Iterator>::difference_type index,
Compare compare);
//! @brief Moves an element up the interval heap.
template <bool left_bound, typename Iterator, typename Offset, typename Compare>
void sift_up (Iterator first, Offset index, Compare compare,Offset limit_child);
//! @brief Moves an element between min/max bounds, and up the interval heap.
template <typename Iterator, typename Offset, typename Compare>
void sift_leaf_max (Iterator first, Iterator last, Offset index,
Compare compare, Offset limit_child);
//! @brief Moves an element between min/max bounds, and up the interval heap.
template <typename Iterator, typename Offset, typename Compare>
void sift_leaf_min (Iterator first, Iterator last, Offset index,
Compare compare, Offset limit_child);
//! @brief Restores the interval-heap property if one element violates it.
template <bool left_bound, typename Iterator, typename Offset, typename Compare>
void sift_down (Iterator first, Iterator last, Offset index, Compare compare,
Offset limit_child);
template<class T>
struct RAIISwapper
{
RAIISwapper (T * p1, T * p2)
: ptr1_(p1), ptr2_(p2)
{
}
RAIISwapper (RAIISwapper const &);
RAIISwapper & operator= (RAIISwapper const &);
inline ~RAIISwapper (void)
{
using std::swap;
if (ptr1_)
swap(*ptr1_, *ptr2_);
}
inline void disable (void)
#if (__cplusplus >= 201103L)
noexcept { ptr1_ = nullptr; }
#else
{ ptr1_ = NULL; }
#endif
T * ptr1_, * ptr2_;
};
} // Namespace interval_heap_internal
/// \endcond
/*! @details This function restores the interval-heap property violated by the
// specified element.
// @param first,last A range of random-access iterators.
// @param index The offset, from @a first, of the offending element.
// @param compare A comparison object.
// @pre @a index is in the range [ 0, @a last - @a first).
// @pre [ @a first, @a last) is a valid interval heap, if the element at
// @a first + @a index is ignored.
// @post [ @a first, @a last) is a valid interval heap.
// @invariant No element is added to or removed from the range.
// @par Complexity:
// O(log n) - Logarithmic on the size of the heap.
// @par Exception safety:
// Strong (rollback) if move/copy/swap (depending on C++ year) do not throw
// exceptions.
*/
template <typename Iterator, typename Compare>
void update_interval_heap (Iterator first, Iterator last,
typename std::iterator_traits<Iterator>::difference_type index,
Compare compare)
{
using namespace std;
using interval_heap_internal::sift_down;
typedef typename iterator_traits<Iterator>::difference_type Offset;
if (index & 1)
sift_down<false, Iterator, Offset, Compare>(first, last, index, compare,2);
else
sift_down<true, Iterator, Offset, Compare>(first, last, index, compare, 2);
}
/*! @details This function expands an interval heap from [ @a first, @a last -
// 1) to [ @a first, @a last), maintaining the interval-heap property. This is
// typically used to add a new element to the interval heap.
// @param first,last A range of random-access iterators.
// @param compare A comparison object.
// @pre [ @a first, @a last - 1) is a valid interval heap.
// @post [ @a first, @a last) is a valid interval heap.
// @invariant No element is added to or removed from the range.
// @par Complexity:
// O(log n) - Logarithmic on the size of the heap.
// @par Exception safety:
// Strong (rollback) if move/copy/swap (depending on C++ year) do not throw
// exceptions.
*/
template <typename Iterator, typename Compare>
void push_interval_heap (Iterator first, Iterator last, Compare compare) {
using interval_heap_internal::sift_leaf;
sift_leaf<Iterator, Compare>(first, last, (last - first) - 1, compare);
}
/*! @details This function moves a specified element to the end of the range of
// iterators. This is typically done so that the element can be efficiently
// removed (by @a pop_back, for example).
// @param first,last A range of random-access iterators.
// @param index The offset, from @a first, of the element to be removed.
// @param compare A comparison object.
// @pre [ @a first, @a last) is a valid interval heap.
// @pre @a index is in the range [ 0, @a last - @a first)
// @post [ @a first, @a last - 1) is a valid interval heap.
// @post The element originally at @a first + @a index has been moved to
// @a last - 1.
// @invariant No element is added to or removed from the range.
// @par Complexity:
// O(log n) - Logarithmic on the size of the heap.
// @par Exception safety:
// Strong (rollback) if move/copy/swap (depending on C++ year) do not throw
// exceptions.
*/
template <typename Iterator, typename Compare>
void pop_interval_heap (Iterator first, Iterator last,
typename std::iterator_traits<Iterator>::difference_type index,
Compare compare)
{
typedef interval_heap_internal::RAIISwapper<typename std::iterator_traits<Iterator>::value_type> guard_t;
using namespace std;
--last;
if (first + index != last) {
#if (__cplusplus >= 201103L)
guard_t scope_guard (std::addressof(*(first + index)), std::addressof(*last));
#else
guard_t scope_guard (&(*(first + index)), &(*last));
#endif
swap(*scope_guard.ptr1_, *scope_guard.ptr2_);
update_interval_heap<Iterator, Compare>(first, last, index, compare);
scope_guard.disable();
}
// If the update throws, the scope guard will undo it.
}
/*! @details This function moves a minimal element to the end of the range of
// iterators. This is typically done so that the element can be efficiently
// removed (by @a pop_back, for example), but can also be used to sort the
// range in descending order.
// @param first,last A range of random-access iterators.
// @param compare A comparison object.
// @pre [ @a first, @a last) is a valid interval heap.
// @post A minimal element from the range has been moved to @a last - 1.
// @post [ @a first, @a last - 1) is a valid interval heap.
// @invariant No element is added to or removed from the range.
// @par Complexity:
// O(log n) - Logarithmic on the size of the heap.
// @par Exception safety:
// Strong (rollback) if move/copy/swap (depending on C++ year) do not throw
// exceptions.
*/
template <typename Iterator, typename Compare>
void pop_interval_heap_min (Iterator first, Iterator last, Compare compare) {
using namespace std;
using interval_heap_internal::sift_down;
typedef typename iterator_traits<Iterator>::difference_type Offset;
--last;
if (first != last) {
swap(*first, *last);
typedef interval_heap_internal::RAIISwapper<typename std::iterator_traits<Iterator>::value_type> guard_t;
#if (__cplusplus >= 201103L)
guard_t scope_guard (std::addressof(*first), std::addressof(*last));
#else
guard_t scope_guard (&(*first), &(*last));
#endif
sift_down<true, Iterator, Offset, Compare>(first, last, 0, compare, 2);
scope_guard.disable();
}
}
/*! @details This function moves a maximal element to the end of the range of
// iterators. This is typically done so that the element can be efficiently
// removed (by @a pop_back, for example), but can also be used to sort the
// range in ascending order.
// @param first,last A range of random-access iterators.
// @param compare A comparison object.
// @pre [ @a first, @a last) is a valid interval heap.
// @post A maximal element from the range has been moved to @a last - 1.
// @post [ @a first, @a last - 1) is a valid interval heap.
// @invariant No element is added to or removed from the range.
// @par Complexity:
// O(log n) - Logarithmic on the size of the heap.
// @par Exception safety:
// Strong (rollback) if move/copy/swap (depending on C++ year) do not throw
// exceptions.
*/
template <typename Iterator, typename Compare>
void pop_interval_heap_max (Iterator first, Iterator last, Compare compare) {
using namespace std;
using interval_heap_internal::sift_down;
typedef typename iterator_traits<Iterator>::difference_type Offset;
// Nothing to be done.
if (last - first <= 2)
return;
--last;
typedef interval_heap_internal::RAIISwapper<typename std::iterator_traits<Iterator>::value_type> guard_t;
#if (__cplusplus >= 201103L)
guard_t scope_guard (std::addressof(*(first + 1)), std::addressof(*last));
#else
guard_t scope_guard (&(*(first + 1)), &(*last));
#endif
swap(*scope_guard.ptr1_, *scope_guard.ptr2_);
sift_down<false, Iterator, Offset, Compare>(first, last, 1, compare, 2);
scope_guard.disable();
}
/*! @details This function takes an interval heap and sorts its elements in
// ascending order.
// @param first,last A range of random-access iterators.
// @param compare A comparison object.
// @pre [ @a first, @a last) is a valid interval heap.
// @post [ @a first, @a last) is sorted in ascending order.
// @invariant No element is added to or removed from the range.
// @par Complexity:
// O(n log n) - Linearithmic on the size of the heap.
// @par Exception safety:
/// Basic - Elements are not added to or removed from the range.
*/
template <typename Iterator, typename Compare>
void sort_interval_heap (Iterator first, Iterator last, Compare compare) {
Iterator cursor = last;
while (cursor != first) {
// If this throws, anything I do to try to fix it is also likely to throw.
pop_interval_heap_max<Iterator, Compare>(first, cursor, compare);
--cursor;
}
}
//! @brief Finds the largest subrange that forms a valid interval heap.
/// @par Complexity:
/// O(n) - Linear on the size of the heap.
/// @par Exception safety:
/// Does not throw.
template <typename Iterator, typename Compare>
Iterator is_interval_heap_until (Iterator first, Iterator last, Compare compare)
{
using namespace std;
typedef typename iterator_traits<Iterator>::difference_type Offset;
Offset index = static_cast<Offset>(0);
try {
Offset index_end = last - first;
while (index < index_end) {
Iterator cursor = first + index;
// Check whether it is a valid interval.
if ((index & 1) && compare(*cursor, *(cursor - 1)))
return cursor;
if (index >= 2) {
// If there exists a parent interval, check for containment.
Iterator parent = first + ((index / 2 - 1) | 1);
if (index & 1) {
if (compare(*parent, *cursor))
return cursor;
} else {
if (compare(*parent, *cursor))
return cursor;
if (compare(*cursor, *(parent - 1)))
return cursor;
}
}
++index;
}
} catch (...) {
return first + index;
}
return last;
}
//! @brief Checks whether the range is a valid interval heap.
/// @par Complexity:
/// O(n) - Linear on the size of the heap.
/// @par Exception safety:
/// Does not throw.
template <typename Iterator, typename Compare>
bool is_interval_heap (Iterator first, Iterator last, Compare compare) {
try {
return (is_interval_heap_until<Iterator,Compare>(first, last, compare)
== last);
} catch (...) {
return false;
}
}
/*! @details This function moves elements in a range to from an interval-heap.
// @param first,last A range of random-access iterators.
// @param compare A comparison object.
// @post [ @a first, @a last) is a valid interval heap.
// @invariant No element is added to or removed from the range.
/// @par Complexity:
/// O(n) - Linear on the size of the heap.
/// @par Exception safety:
/// Basic - Elements are not added to or removed from the range.
// @remark Threaded.
*/
template <typename Iterator, typename Compare>
void make_interval_heap (Iterator first, Iterator last, Compare compare) {
using namespace interval_heap_internal;
// Double-heap property holds vacuously.
if (last - first < 2)
return;
#if (BOOST_HEAP_INTERVAL_HEAP_USE_STD_THREAD == true)
typedef typename std::iterator_traits<Iterator>::difference_type Offset;
unsigned int threads = ((last - first) > kThreadMin)?
std::thread::hardware_concurrency() : 1;
if (threads > 1)
make_block<Iterator, Compare, Offset>(first, last, compare, 0, 2, threads);
else
make_full<Iterator, Compare>(first, last, compare);
#else
make_full<Iterator, Compare>(first, last, compare);
#endif
}
namespace interval_heap_internal {
#if (BOOST_HEAP_INTERVAL_HEAP_USE_STD_THREAD == true)
/* This parallel version of the heap-maker uses divide-and-conquer methods to
// distribute the task amongst the cores.
*/
//! @brief Internal function for threaded bulk-load.
template <typename Iterator, typename Compare, typename Offset>
void make_block (Iterator first, Iterator last, Compare compare,
Offset block_begin, Offset block_end, unsigned int threads) {
using namespace std;
using interval_heap_internal::sift_down;
const Offset index_end = last - first;
const Offset end_parent = index_end / 2 - 1;
if (block_begin < end_parent) {
/* Recurse. If reasonable, pass half the task to another thread. Don't split
// if chunks are tiny. Page updates will cause thrashing.
*/
const Offset child_begin = (block_begin + 1) * 2;
const Offset child_end = (block_end + 1) * 2;
if ((threads > 1) && (block_end - block_begin >= kBranchMin)) {
const Offset child_middle = block_end + block_begin + 2;
// Branch.
unsigned int split_threads = threads >> 1;
thread thread_branch(&make_block<Iterator,Compare,Offset>, first, last,
compare, child_middle, child_end, split_threads);
//make_block(first, last, compare, child_middle, child_end, split_threads);
make_block(first, last, compare, child_begin, child_middle,
threads - split_threads);
thread_branch.join();
//std::cerr << "Split at: " << child_middle << "\n";
} else
make_block(first, last, compare, child_begin, child_end, threads);
/* Make this layer of the interval heap; we assume that all lower layers are
// already OK.
*/
for (Offset index = block_end; (index > block_begin);) {
const Offset coindex = --index; // = index + 1
--index;
// If compare throws, heap property cannot be verified or enforced.
// If swap throws, heap property is violated and cannot be enforced.
if (compare(*(first + coindex), *(first + index)))
swap(*(first + coindex), *(first + index));
//const Offset stop = (index <= end_parent) ? (coindex * 2) : index_end;
const Offset stop = coindex * 2;
sift_down<false, Iterator, Offset, Compare>(first, last, coindex,
compare, stop);
sift_down<true , Iterator, Offset, Compare>(first, last, index,
compare, stop);
}
} else {
if (block_end > index_end)
// If the final interval is a singleton, it's already OK. Skip it.
block_end = index_end ^ (index_end & 1);
/* Make this layer of the interval heap; we assume that all lower layers are
// already OK.
*/
for (Offset index = block_end - 2; (index > block_begin); index -= 2) {
const Offset coindex = index | 1; // = index + 1
// If compare throws, heap property cannot be verified or enforced.
// If swap throws, heap property is violated and cannot be enforced.
if (compare(*(first + coindex), *(first + index)))
swap(*(first + coindex), *(first + index));
}
const Offset coindex = block_begin | 1;
if (coindex < block_end) {
// If compare throws, heap property cannot be verified or enforced.
// If swap throws, heap property is violated and cannot be enforced.
if (compare(*(first + coindex), *(first + block_begin)))
swap(*(first + coindex), *(first + block_begin));
const Offset stop = (block_begin <= end_parent) ? (coindex * 2) : index_end;
sift_down<false, Iterator, Offset, Compare>(first, last, coindex,
compare, stop);
sift_down<true , Iterator, Offset, Compare>(first, last, block_begin,
compare, stop);
}
}
}
#endif
template <typename Iterator, typename Compare>
void make_full (Iterator first, Iterator last, Compare compare) {
using namespace std;
using interval_heap_internal::sift_down;
typedef typename iterator_traits<Iterator>::difference_type Offset;
// Not less than 2.
const Offset index_end = last - first;
// Prevents overflow when number of elements approaches maximum possible index.
const Offset end_parent = index_end / 2 - 1;
// If the final interval is a singleton, it's already OK. Skip it.
Offset index = (index_end ^ (index_end & 1)) - 2;
// Make all leaf nodes.
while (index > end_parent) {
const Offset coindex = index | 1; // = index + 1
if (compare(*(first + coindex), *(first + index)))
swap(*(first + coindex), *(first + index));
index -= 2;
}
index += 2;
do {
const Offset coindex = --index; // = index + 1
--index;
if (compare(*(first + coindex), *(first + index)))
swap(*(first + coindex), *(first + index));
const Offset stop = coindex * 2;
sift_down<false, Iterator, Offset, Compare>(first, last, coindex,
compare, stop);
sift_down<true , Iterator, Offset, Compare>(first, last, index,
compare, stop);
} while (index >= 2);
}
//! @remark Exception safety: Strong if move/swap doesn't throw.
template <bool left_bound, typename Iterator, typename Offset, typename Compare>
void sift_up (Iterator first, Offset origin, Compare compare,Offset limit_child)
{
// Use the most specialized available functions.
using namespace std;
typedef typename iterator_traits<Iterator>::value_type Value;
// Keeping the information about the origin permits strong exception guarantee.
Offset index = origin;
#if (__cplusplus >= 201103L) // C++11
// Float element in limbo while sifting it up the heap.
Value limbo = std::move_if_noexcept(*(first + index));
#endif
// Provides strong exception-safety guarantee (rollback), unless move throws.
try {
while (index >= limit_child) {
const Offset parent = ((index / 2 - 1) | 1) ^ (left_bound ? 1 : 0);
#if (__cplusplus >= 201103L) // C++11
if (compare((left_bound ? limbo : *(first + parent)),
(left_bound ? *(first + parent) : limbo))) {
*(first + index) = std::move_if_noexcept(*(first + parent));
#else
if (compare(*(first + (left_bound ? index : parent)),
*(first + (left_bound ? parent : index)))) {
swap(*(first + index), *(first + parent));
#endif
index = parent;
} else
break;
}
} catch (...) { // Provides strong exception-safety guarantee.
// I need limbo because elements are being moved in the direction of travel.
#if (__cplusplus < 201103L) // Not C++11. Need to allocate limbo.
Value limbo;
swap(*(first + index), limbo);
#endif
swap(*(first + origin), limbo);
while (origin > index) {
origin = ((origin / 2 - 1) | 1) ^ (left_bound ? 1 : 0);
swap(*(first + origin), limbo);
}
throw; // Re-throw the current exception.
}
#if (__cplusplus >= 201103L) // C++11
// Done sifting. Get the element out of limbo.
*(first + index) = std::move_if_noexcept(limbo);
#endif
}
//! @remark Exception safety: As strong as sift_up.
//! @pre @a index refers to a leaf node of the heap.
template <typename Iterator, typename Offset, typename Compare>
void sift_leaf_max (Iterator first, Iterator last, Offset index,
Compare compare, Offset limit_child)
{
// Use the most specialized swap function.
using namespace std;
const Offset index_end = last - first;
// Index of corresponding left-bound (min) element
const Offset co_index = ((index_end - 1) / 2 < index)
? (index ^ 1) : (index * 2);
if (compare(*(first + index), *(first + co_index))) {
typedef interval_heap_internal::RAIISwapper<typename std::iterator_traits<Iterator>::value_type> guard_t;
#if (__cplusplus >= 201103L)
guard_t scope_guard (std::addressof(*(first + index)), std::addressof(*(first + co_index)));
#else
guard_t scope_guard (&(*(first + index)), &(*(first + co_index)));
#endif
swap(*scope_guard.ptr1_, *scope_guard.ptr2_);
sift_up<true, Iterator, Offset, Compare>(first, co_index, compare,
limit_child);
scope_guard.disable();
} else
sift_up<false, Iterator, Offset, Compare>(first, index, compare,
limit_child);
}
//! @remark Exception safety: As strong as sift_up.
//! @pre @a index refers to a leaf node of the heap.
template <typename Iterator, typename Offset, typename Compare>
void sift_leaf_min (Iterator first, Iterator last, Offset index,
Compare compare, Offset limit_child)
{
// Use the most specialized swap function.
using namespace std;
const Offset index_end = last - first;
// Index of corresponding element (initial assumption)
Offset co_index = index | 1;
// Co-index is past the end of the heap. Move to its parent, if possible.
if (co_index >= index_end) {
// Only one element.
if (co_index == 1)
return;
co_index = (co_index / 2 - 1) | 1;
}
if (compare(*(first + co_index), *(first + index))) {
typedef interval_heap_internal::RAIISwapper<typename std::iterator_traits<Iterator>::value_type> guard_t;
#if (__cplusplus >= 201103L)
guard_t scope_guard (std::addressof(*(first + index)), std::addressof(*(first + co_index)));
#else
guard_t scope_guard (&(*(first + index)), &(*(first + co_index)));
#endif
swap(*scope_guard.ptr1_, *scope_guard.ptr2_);
sift_up<false, Iterator, Offset, Compare>(first, co_index, compare,
limit_child);
scope_guard.disable();
} else
sift_up<true, Iterator, Offset, Compare>(first, index, compare,limit_child);
}
//! @remark Exception safety: As strong as sift_up.
template <bool left_bound, typename Iterator, typename Offset, typename Compare>
void sift_down (Iterator first, Iterator last, Offset origin, Compare compare,
Offset limit_child)
{
// Use the most specialized available functions.
using namespace std;
// By keeping track of where I started, I can roll back all changes.
Offset index = origin;
#if (__cplusplus >= 201103L) // C++11
typedef typename iterator_traits<Iterator>::value_type Value;
Value limbo = std::move_if_noexcept(*(first + index));
#endif
const Offset index_end = last - first;
// One past the last element with two children.
const Offset end_parent = index_end / 2 -
((left_bound && ((index_end & 3) == 0)) ? 2 : 1);
try { // This try-catch block rolls back after exceptions.
while (index < end_parent) {
Offset child = index * 2 + (left_bound ? 2 : 1);
// If compare throws, heap property cannot be verified or enforced.
try { // This try-catch block ensures no element is left in limbo.
if (compare(*(first + child + (left_bound ? 2 : 0)),
*(first + child + (left_bound ? 0 : 2))))
child += 2;
} catch (...) {
#if (__cplusplus >= 201103L) // C++11
// Pull the moving element out of limbo, to avoid leaks.
*(first + index) = std::move_if_noexcept(limbo);
#endif
throw; // Re-throw the current exception.
}
#if (__cplusplus >= 201103L) // C++11
*(first + index) = std::move_if_noexcept(*(first + child));
#else
swap(*(first + index), *(first + child));
#endif
index = child;
}
// Special case when index has exactly one child.
if (index <= end_parent + (left_bound ? 0 : 1)) {
Offset child = index * 2 + (left_bound ? 2 : 1);
if (child < index_end) {
const Offset cochild = child + 1;
// Need to treat singletons (child + 1) as both upper and lower bounds.
if (!left_bound && (cochild != index_end)) {
// Calculating this outside the if-statement simplifies exception-handling.
bool swap_required;
try {
swap_required = compare(*(first + child), *(first + cochild));
} catch (...) {
// Pull the moving element out of limbo.
#if (__cplusplus >= 201103L)
*(first + index) = std::move_if_noexcept(limbo);
#endif
throw; // Re-throw the current exception.
}
if (swap_required) {
//++child;
#if (__cplusplus >= 201103L) // C++11
*(first + index) = std::move_if_noexcept(*(first + cochild));
*(first + cochild) = std::move_if_noexcept(limbo);
#else
swap(*(first + index), *(first + cochild));
#endif
index = cochild; // Important for the rollback.
sift_leaf_min<Iterator, Offset, Compare>(first, last, index,
compare, limit_child);
return;
}
}
#if (__cplusplus >= 201103L) // C++11
*(first + index) = std::move_if_noexcept(*(first + child));
#else
swap(*(first + index), *(first + child));
#endif
index = child;
}
}
// Pull the moving element out of limbo.
#if (__cplusplus >= 201103L) // C++11
*(first + index) = std::move_if_noexcept(limbo);
#endif
if (left_bound)
sift_leaf_min<Iterator, Offset, Compare>(first, last, index, compare,
limit_child);
else
sift_leaf_max<Iterator, Offset, Compare>(first, last, index, compare,
limit_child);
} catch (...) {
// Rolls back comparison exceptions. Move exceptions can't be reliably fixed.
while (index > origin) {
const Offset parent = ((index / 2 - 1) | 1) ^ (left_bound ? 1 : 0);
swap(*(first + parent), *(first + index));
index = parent;
}
throw; // Re-throw the current exception.
}
}
//! @remark Exception safety: As strong as sift_up.
template <typename Iterator, typename Compare>
void sift_leaf (Iterator first, Iterator last,
typename std::iterator_traits<Iterator>::difference_type index,
Compare compare)
{
using namespace std;
typedef typename iterator_traits<Iterator>::difference_type Offset;
if (index & 1)
sift_leaf_max<Iterator, Offset, Compare>(first, last, index, compare, 2);
else
sift_leaf_min<Iterator, Offset, Compare>(first, last, index, compare, 2);
}
} // Namespace interval_heap_internal
} // Namespace heap
} // Namespace boost
#endif // BOOST_HEAP_INTERVAL_HEAP_HPP_