-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary_trees.cpp
83 lines (70 loc) · 1.84 KB
/
binary_trees.cpp
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
#include "gc.hpp"
#include <iostream>
#include <stdlib.h>
#include "pointers.hpp"
struct Node
{
GCObject gc;
Node *left;
Node *right;
int check() const
{
if (left)
{
return left->check() + 1 + right->check();
}
else
{
return 1;
}
}
};
const uint64_t Node_offsets[3] = {offsetof(Node, left), offsetof(Node, right), 0};
Node *node_new(const int d)
{
Node *result = get_gc<Node>(Node_offsets);
if (d > 0)
{
result->left = node_new(d - 1);
result->right = node_new(d - 1);
}
else
{
result->left = result->right = nullptr;
}
return result;
}
using LS = PointerListStyle;
using Iter = PointersLinkedListIterator<LS>;
int main(int argc, char *argv[])
{
int min_depth = 4;
int max_depth = std::max(min_depth + 2, (argc == 2 ? atoi(argv[1]) : 10));
int stretch_depth = max_depth + 1;
Pointers<2, LS> ptrs;
{
ptrs.values[0] = reinterpret_cast<GCObject *>(node_new(stretch_depth));
std::cout << "stretch tree of depth " << stretch_depth << "\t "
<< "check: " << reinterpret_cast<Node *>(ptrs.values[0])->check() << std::endl;
walk<Iter>(&ptrs);
}
ptrs.values[0] = reinterpret_cast<GCObject *>(node_new(max_depth));
for (int d = min_depth; d <= max_depth; d += 2)
{
int iterations = 1 << (max_depth - d + min_depth), c = 0;
for (int i = 1; i <= iterations; ++i)
{
ptrs.values[1] = reinterpret_cast<GCObject *>(node_new(d));
c += reinterpret_cast<Node *>(ptrs.values[0])->check();
}
std::cout << iterations << "\t trees of depth " << d << "\t "
<< "check: " << c << std::endl;
walk<Iter>(&ptrs);
sweep();
}
std::cout << "long lived tree of depth " << max_depth << "\t "
<< "check: " << ((reinterpret_cast<Node *>(ptrs.values[0]))->check()) << "\n";
walk<Iter>(&ptrs);
sweep();
return 0;
}