Broom
1.0.0
A thread-local C++ Garbage Collector
Loading...
Searching...
No Matches
examples
simple
main.cc
Go to the documentation of this file.
1
#include <cstdint>
2
#include <limits>
3
#include <print>
4
#include <queue>
5
#include <stack>
6
#include <string>
7
#include <vector>
8
9
#include "
include/broom-unordered-map.h
"
10
#include "
include/broom.h
"
11
12
#if defined(__clang__) || defined(__GNUC__)
13
#define NOINLINE __attribute__((noinline))
14
#elif defined(_MSC_VER)
15
#define NOINLINE __declspec(noinline)
16
#endif
17
18
class
Node
{
19
public
:
20
Node
(std::string s,
size_t
id
) : name_(s), id_(id) {}
21
~Node
() { std::println(
"Destroying node: {}"
, id_); }
22
Node
*
Connect
(
Node
* which,
size_t
distance) {
23
adjacent_nodes_.push_back({which, distance});
24
which->adjacent_nodes_.push_back({
this
, distance});
25
return
this
;
26
}
27
28
const
std::string&
Name
()
const
{
return
name_; }
29
const
std::vector<std::pair<Node*, size_t>>&
Adjacent
()
const
{
30
return
adjacent_nodes_;
31
}
32
size_t
Identifier
()
const
{
return
id_; }
33
34
private
:
35
std::string name_;
36
std::vector<std::pair<Node*, size_t>> adjacent_nodes_;
37
size_t
id_;
38
};
39
40
class
Graph
{
41
public
:
42
Graph
() =
default
;
43
Node
*
New
(std::string s) {
44
adj_[s] =
broom::allocate<Node>
(1, s, id_++);
45
return
adj_[s];
46
}
47
48
Node
*
operator[]
(std::string s) {
return
adj_[s]; }
49
50
size_t
ShortestPath
(std::string from, std::string to) {
51
std::queue<Node*> to_visit;
52
Node
* from_node = adj_[from];
53
Node
* to_node = adj_[to];
54
std::vector<size_t> distance(adj_.size(),
55
std::numeric_limits<size_t>::max());
56
to_visit.push(from_node);
57
distance[from_node->
Identifier
()] = 0;
58
59
while
(!to_visit.empty()) {
60
Node
* current = to_visit.front();
61
to_visit.pop();
62
63
for
(
auto
[n, dist] : current->
Adjacent
()) {
64
if
(distance[n->Identifier()] >
65
distance[current->
Identifier
()] + dist) {
66
distance[n->Identifier()] = distance[current->
Identifier
()] + dist;
67
to_visit.push(n);
68
}
69
}
70
}
71
return
distance[to_node->
Identifier
()];
72
}
73
74
private
:
75
broom::unordered_map<std::string, Node*>
adj_;
76
size_t
id_ = 0;
77
};
78
79
NOINLINE
void
CalculateShortestPaths
(
Graph
& g) {
80
std::println(
"Shortest path from home 8 to library: {}"
,
81
g.
ShortestPath
(
"home 8"
,
"library"
));
82
std::println(
"Shortest path from home bus station to library: {}"
,
83
g.
ShortestPath
(
"bus station"
,
"library"
));
84
std::println(
"Shortest path from home 4 to library: {}"
,
85
g.
ShortestPath
(
"home 4"
,
"library"
));
86
std::println(
"Shortest path from home 5 to home 7: {}"
,
87
g.
ShortestPath
(
"home 5"
,
"home 7"
));
88
}
89
90
NOINLINE
void
GraphInFunction
() {
91
Graph
collected_g;
92
collected_g.
New
(
"bakery"
);
93
collected_g.
New
(
"restaurant"
)->
Connect
(collected_g[
"bakery"
], 21);
94
collected_g.
New
(
"fountain"
)
95
->
Connect
(collected_g[
"bakery"
], 2)
96
->
Connect
(collected_g[
"restaurant"
], 34);
97
}
98
99
int
main
() {
100
broom::scope
gc_scope;
101
std::println(
"Creating graph in function"
);
102
GraphInFunction
();
103
// The nodes from collected_g will be alive here.
104
std::println(
"Creating new graph"
);
105
Graph
g;
106
g.
New
(
"library"
);
107
g.
New
(
"train station"
)->
Connect
(g[
"library"
], 12);
108
g.
New
(
"bus station"
)
109
->
Connect
(g[
"train station"
], 3)
110
->
Connect
(g[
"library"
], 70);
111
g.
New
(
"home 1"
)->
Connect
(g[
"train station"
], 17);
112
g.
New
(
"home 2"
)->
Connect
(g[
"home 1"
], 2);
113
g.
New
(
"home 3"
)
114
->
Connect
(g[
"home 1"
], 17)
115
->
Connect
(g[
"train station"
], 95)
116
->
Connect
(g[
"library"
], 9);
117
g.
New
(
"home 4"
)
118
->
Connect
(g[
"home 3"
], 16)
119
->
Connect
(g[
"home 1"
], 103)
120
->
Connect
(g[
"train station"
], 5);
121
g.
New
(
"home 5"
)->
Connect
(g[
"train station"
], 27);
122
g.
New
(
"home 6"
)
123
->
Connect
(g[
"home 5"
], 173)
124
->
Connect
(g[
"home 1"
], 2304)
125
->
Connect
(g[
"library"
], 123)
126
->
Connect
(g[
"home 2"
], 3);
127
g.
New
(
"home 7"
)->
Connect
(g[
"library"
], 78);
128
g.
New
(
"home 8"
)->
Connect
(g[
"home 7"
], 421);
129
std::println(
"Created new graph"
);
130
131
// Force collect the collected_g. This may or may not always happen, depending
132
// on what bytes are scanned. Broom is a conservative GC, so it might make
133
// mistakes and keep dead allocations alive.
134
broom::force_collection
();
135
CalculateShortestPaths
(g);
136
return
0;
137
}
broom-unordered-map.h
broom.h
Graph
Definition
main.cc:40
Graph::ShortestPath
size_t ShortestPath(std::string from, std::string to)
Definition
main.cc:50
Graph::operator[]
Node * operator[](std::string s)
Definition
main.cc:48
Graph::New
Node * New(std::string s)
Definition
main.cc:43
Graph::Graph
Graph()=default
Node
Definition
main.cc:18
Node::Connect
Node * Connect(Node *which, size_t distance)
Definition
main.cc:22
Node::Adjacent
const std::vector< std::pair< Node *, size_t > > & Adjacent() const
Definition
main.cc:29
Node::Name
const std::string & Name() const
Definition
main.cc:28
Node::~Node
~Node()
Definition
main.cc:21
Node::Identifier
size_t Identifier() const
Definition
main.cc:32
Node::Node
Node(std::string s, size_t id)
Definition
main.cc:20
broom::force_collection
void force_collection()
Definition
api.cc:39
broom::queue
std::queue< T, broom::deque< T > > queue
Definition
broom-queue.h:12
main
int main()
Definition
main.cc:233
CalculateShortestPaths
NOINLINE void CalculateShortestPaths(Graph &g)
Definition
main.cc:79
GraphInFunction
NOINLINE void GraphInFunction()
Definition
main.cc:90
broom::BroomScope
Definition
broom.h:111
Generated by
1.9.8