-
Notifications
You must be signed in to change notification settings - Fork 1
/
forest.h
162 lines (121 loc) · 3.29 KB
/
forest.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once
#include "node.h"
template <typename keyT>
class forest {
static void joinSubtrees(node<keyT> &t1, const node<keyT> &t2);
ihppNodeChildrenMap< keyT, node<keyT> > trees;
public:
typedef typename ihppNodeChildrenMap< keyT, node<keyT> >::iterator iterator;
forest() = default;
forest(const forest &f);
forest(const node<keyT>& n);
forest<keyT> &operator=(const forest<keyT>& f);
node<keyT> *getTreeRef(keyT k);
node<keyT> *toTree();
node<keyT> *addTree(const node<keyT>& n);
auto begin() { return trees.begin(); }
auto end() { return trees.end(); }
auto begin() const { return trees.begin(); }
auto end() const { return trees.end(); }
auto cbegin() { return trees.cbegin(); }
auto cend() { return trees.cend(); }
size_t treesCount() const { return trees.size(); }
size_t recursiveAllNodesCount() const;
void autoSetParents();
forest<keyT> inverseK(unsigned int k);
void join(const node<keyT>& t2);
void join(const forest<keyT>& f2);
};
template <typename keyT>
inline forest<keyT>::forest(const forest<keyT>& f)
: trees(f.trees)
{
BM_inc_forests_copied();
}
template <typename keyT>
inline forest<keyT>::forest(const node<keyT>& n)
: trees()
{
addTree(n);
BM_inc_forests_copied();
}
template <typename keyT>
inline forest<keyT> &forest<keyT>::operator=(const forest<keyT>& f)
{
trees = f.trees;
BM_inc_forests_copied();
return *this;
}
template <typename keyT>
inline node<keyT>* forest<keyT>::addTree(const node<keyT>& n) {
assert(!getTreeRef(n.getKey()));
return &trees.insert(n.getKey(), n);
}
template <typename keyT>
size_t forest<keyT>::recursiveAllNodesCount() const {
size_t count = 0;
for (const auto& tree : *this)
count += tree.recursiveAllNodesCount();
return count;
}
template <typename keyT>
inline node<keyT> *forest<keyT>::getTreeRef(keyT k) {
auto it = trees.find(k);
if (it != end())
return &(*it);
return 0;
}
template <typename keyT>
node<keyT>* forest<keyT>::toTree()
{
assert(treesCount() == 1);
return &(*begin());
}
template <typename keyT>
void forest<keyT>::autoSetParents()
{
for (auto& child : *this) {
if (!child.getParentRef())
child.autoSetParents();
}
}
template <typename keyT>
void forest<keyT>::joinSubtrees(node<keyT> &t1, const node<keyT> &t2)
{
t1.setCounter(t1.getCounter() + t2.getCounter());
for (const auto& t2_child : t2) {
node<keyT> *t = t1.getChildRef(t2_child.getKey());
if (!t)
t1.addChild(t2_child);
else
joinSubtrees(*t, t2_child);
}
}
template <typename keyT>
void forest<keyT>::join(const node<keyT>& t2)
{
node<keyT> *t = getTreeRef(t2.getKey());
if (t)
joinSubtrees(*t, t2);
else
addTree(t2);
}
template <typename keyT>
void forest<keyT>::join(const forest<keyT>& f2)
{
for (auto& tree : f2)
join(tree);
}
template <typename keyT>
forest<keyT> forest<keyT>::inverseK(unsigned int k)
{
forest res;
for (auto& tree : *this) {
std::vector< node<keyT> * > nodes;
tree.autoSetParents();
tree.getAllTreeNodesRef(nodes);
for (node<keyT> *other_tree : nodes)
res.join(other_tree->kpathR(k));
}
return res;
}