]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | /* | |
5 | * Copyright (C) 2016 Red Hat Inc. | |
11fdf7f2 TL |
6 | * |
7 | * Author: J. Eric Ivancich <ivancich@redhat.com> | |
8 | * | |
9 | * This is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU Lesser General Public License version | |
11 | * 2.1, as published by the Free Software Foundation. See file | |
12 | * COPYING. | |
7c673cae FG |
13 | */ |
14 | ||
15 | ||
16 | #include <memory> | |
17 | #include <string> | |
18 | #include <iostream> | |
19 | ||
20 | #include "indirect_intrusive_heap.h" | |
21 | ||
22 | ||
23 | class TestCompare; | |
24 | ||
25 | ||
26 | class Test1 { | |
27 | friend TestCompare; | |
28 | ||
29 | int data; | |
30 | ||
31 | public: | |
32 | ||
33 | crimson::IndIntruHeapData heap_data; | |
34 | ||
11fdf7f2 | 35 | explicit Test1(int _data) : data(_data) {} |
7c673cae FG |
36 | |
37 | friend std::ostream& operator<<(std::ostream& out, const Test1& d) { | |
38 | out << d.data << " (" << d.heap_data << ")"; | |
39 | return out; | |
40 | } | |
41 | ||
42 | int& the_data() { return data; } | |
43 | }; | |
44 | ||
45 | ||
46 | struct TestCompare { | |
47 | bool operator()(const Test1& d1, const Test1& d2) { | |
48 | return d1.data < d2.data; | |
49 | } | |
50 | }; | |
51 | ||
52 | ||
53 | int main(int argc, char** argv) { | |
54 | Test1 d1(2); | |
55 | Test1 d2(3); | |
56 | Test1 d3(1); | |
57 | Test1 d4(-5); | |
58 | ||
59 | crimson::IndIntruHeap<std::shared_ptr<Test1>, Test1, &Test1::heap_data, TestCompare> my_heap; | |
60 | ||
61 | const std::shared_ptr<Test1> d99 = std::make_shared<Test1>(99); | |
62 | ||
63 | my_heap.push(std::make_shared<Test1>(2)); | |
64 | my_heap.push(d99); | |
65 | my_heap.push(std::make_shared<Test1>(1)); | |
66 | my_heap.push(std::make_shared<Test1>(-5)); | |
67 | my_heap.push(std::make_shared<Test1>(12)); | |
68 | my_heap.push(std::make_shared<Test1>(-12)); | |
69 | my_heap.push(std::make_shared<Test1>(-7)); | |
70 | ||
71 | std::cout << my_heap << std::endl; | |
72 | ||
73 | auto& t = my_heap.top(); | |
74 | t.the_data() = 17; | |
75 | my_heap.adjust_down(t); | |
76 | ||
77 | std::cout << my_heap << std::endl; | |
78 | ||
79 | my_heap.display_sorted(std::cout); | |
80 | ||
81 | while (!my_heap.empty()) { | |
82 | auto& top = my_heap.top(); | |
83 | std::cout << top << std::endl; | |
84 | my_heap.pop(); | |
85 | std::cout << my_heap << std::endl; | |
86 | } | |
87 | ||
88 | return 0; | |
89 | } |