1 | // compile as:
|
---|
2 | // g++ boost_binomial_heap_bug.cpp -o boost_binomial_heap_bug
|
---|
3 | //
|
---|
4 | #include <iostream>
|
---|
5 | #include <cassert>
|
---|
6 |
|
---|
7 | //#define BOOST_HEAP_SANITYCHECKS
|
---|
8 | #include <boost/heap/fibonacci_heap.hpp>
|
---|
9 | #include <boost/heap/binomial_heap.hpp>
|
---|
10 |
|
---|
11 | typedef struct node_t {
|
---|
12 | int value;
|
---|
13 | int weight;
|
---|
14 |
|
---|
15 | explicit node_t(int v, int w) {
|
---|
16 | value = v;
|
---|
17 | weight = w;
|
---|
18 | }
|
---|
19 | } node_t;
|
---|
20 |
|
---|
21 | typedef struct node_comparator {
|
---|
22 | inline
|
---|
23 | bool operator()(const node_t &n1, const node_t &n2) const {
|
---|
24 | return n1.weight > n2.weight;
|
---|
25 | }
|
---|
26 | } node_comparator;
|
---|
27 |
|
---|
28 | typedef boost::heap::fibonacci_heap<node_t, boost::heap::compare<node_comparator> > fibonacci_heap_t;
|
---|
29 | typedef boost::heap::binomial_heap<node_t, boost::heap::compare<node_comparator>, boost::heap::stable<true> > binomial_stable_heap_t;
|
---|
30 | typedef boost::heap::binomial_heap<node_t, boost::heap::compare<node_comparator> > binomial_unstable_heap_t;
|
---|
31 |
|
---|
32 | template <typename T>
|
---|
33 | void check_heap(T &heap) {
|
---|
34 | int heap_size = 0;
|
---|
35 |
|
---|
36 | for (int i = 0; i < 3; i++) {
|
---|
37 | heap.push(node_t(i, 9));
|
---|
38 | heap_size++;
|
---|
39 | }
|
---|
40 |
|
---|
41 | assert (heap_size == heap.size());
|
---|
42 |
|
---|
43 | while (!heap.empty()) {
|
---|
44 | const node_t &e = heap.top();
|
---|
45 | heap.pop();
|
---|
46 |
|
---|
47 | heap_size--;
|
---|
48 |
|
---|
49 | assert (heap_size == heap.size());
|
---|
50 | }
|
---|
51 | }
|
---|
52 | int main(void) {
|
---|
53 | std::cout << "Checking fibonacci heap...\n";
|
---|
54 | fibonacci_heap_t fheap;
|
---|
55 | check_heap(fheap);
|
---|
56 |
|
---|
57 | std::cout << "Checking binomial stable heap...\n";
|
---|
58 | binomial_stable_heap_t bsheap;
|
---|
59 | check_heap(bsheap);
|
---|
60 |
|
---|
61 | std::cout << "Checking binomial unstable heap...\n";
|
---|
62 | binomial_unstable_heap_t buheap;
|
---|
63 | check_heap(buheap);
|
---|
64 | return EXIT_SUCCESS;
|
---|
65 | }
|
---|