]> git.proxmox.com Git - ceph.git/blob - ceph/src/dmclock/support/test/test_ind_intru_heap.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / dmclock / support / test / test_ind_intru_heap.cc
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.
6 */
7
8
9 #include <memory>
10 #include <string>
11 #include <iostream>
12
13 #include "indirect_intrusive_heap.h"
14
15
16 class TestCompare;
17
18
19 class Test1 {
20 friend TestCompare;
21
22 int data;
23
24 public:
25
26 crimson::IndIntruHeapData heap_data;
27
28 Test1(int _data) : data(_data) {}
29
30 friend std::ostream& operator<<(std::ostream& out, const Test1& d) {
31 out << d.data << " (" << d.heap_data << ")";
32 return out;
33 }
34
35 int& the_data() { return data; }
36 };
37
38
39 struct TestCompare {
40 bool operator()(const Test1& d1, const Test1& d2) {
41 return d1.data < d2.data;
42 }
43 };
44
45
46 int main(int argc, char** argv) {
47 Test1 d1(2);
48 Test1 d2(3);
49 Test1 d3(1);
50 Test1 d4(-5);
51
52 crimson::IndIntruHeap<std::shared_ptr<Test1>, Test1, &Test1::heap_data, TestCompare> my_heap;
53
54 const std::shared_ptr<Test1> d99 = std::make_shared<Test1>(99);
55
56 my_heap.push(std::make_shared<Test1>(2));
57 my_heap.push(d99);
58 my_heap.push(std::make_shared<Test1>(1));
59 my_heap.push(std::make_shared<Test1>(-5));
60 my_heap.push(std::make_shared<Test1>(12));
61 my_heap.push(std::make_shared<Test1>(-12));
62 my_heap.push(std::make_shared<Test1>(-7));
63
64 std::cout << my_heap << std::endl;
65
66 auto& t = my_heap.top();
67 t.the_data() = 17;
68 my_heap.adjust_down(t);
69
70 std::cout << my_heap << std::endl;
71
72 my_heap.display_sorted(std::cout);
73
74 while (!my_heap.empty()) {
75 auto& top = my_heap.top();
76 std::cout << top << std::endl;
77 my_heap.pop();
78 std::cout << my_heap << std::endl;
79 }
80
81 return 0;
82 }