-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.h
259 lines (191 loc) · 5.56 KB
/
node.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#pragma once
#include <map>
#include <stack>
#include <vector>
#include <iostream>
#include "dataStructures.h"
#include "benchmark.h"
template <typename keyT>
class node : public ObjectWithKey<keyT> {
private:
node<keyT> *parent;
protected:
ObjectWithKey<keyT> *val;
obj_counter_t counter;
ihppNodeChildrenList< keyT, node<keyT> > children;
void clearLevelKCounters(unsigned int k, unsigned int deepth);
public:
typedef typename ihppNodeChildrenList< keyT, node<keyT> >::iterator iterator;
node();
node(const node& n);
node(keyT key, ObjectWithKey<keyT>* val, obj_counter_t counter=0);
node<keyT>& operator=(const node& n);
~node() = default;
node<keyT> *getChildRef(keyT k);
node<keyT> *addChild(const node<keyT>& n);
node<keyT> *replaceChild(node<keyT>& n);
node<keyT> *addChildByVal(node<keyT> n) { return addChild(n); }
node<keyT> *getParentRef() { return (parent && *parent) ? parent : 0; }
void getAllTreeNodesRef(std::vector< node<keyT>* >& vec);
bool isValid() const { return val != nullptr; }
size_t childrenCount() const { return children.size(); }
size_t recursiveAllNodesCount() const;
void autoSetParents();
keyT getKey() const { assert(isValid()); return val->getKey(); }
ObjectWithKey<keyT> *getValue() { assert(isValid()); return val; }
auto begin() { return children.begin(); }
auto end() { return children.end(); }
auto begin() const { return children.cbegin(); }
auto end() const { return children.cend(); }
auto cbegin() { return children.cbegin(); }
auto cend() { return children.cend(); }
void incCounter() { counter++; }
obj_counter_t getCounter() const { return counter; }
void setCounter(obj_counter_t c) { counter=c; }
node<keyT> kpath(unsigned int k);
node<keyT> kpathR(unsigned int k);
void resetVisitedRecursive();
void clearLevelKCounters(unsigned int k);
operator std::string() {
return (val)
? static_cast<std::string>(*getValue())
: std::string();
}
operator bool() { return isValid(); }
};
template <typename keyT>
inline node<keyT> &node<keyT>::operator=(const node& n)
{
val = n.val;
children = n.children;
counter = n.counter;
parent = nullptr;
BM_inc_nodes_copied();
return *this;
}
template <typename keyT>
inline node<keyT>::node(const node<keyT> &n)
: parent(nullptr)
, val(n.val)
, counter(n.counter)
, children(n.children)
{
BM_inc_nodes_copied();
}
template <typename keyT>
inline node<keyT>::node()
: parent(nullptr)
, val(nullptr)
, counter(0)
{
BM_inc_empty_nodes_created();
}
template <typename keyT>
inline node<keyT>::node(keyT key, ObjectWithKey<keyT> *val, obj_counter_t counter)
: parent(nullptr)
, val(val)
, counter(counter)
{
assert(val);
assert(key == val->getKey());
BM_inc_nodes_created();
}
template <typename keyT>
inline std::ostream& operator<<(std::ostream& s, const node<keyT>& n) {
return n ? s << n.getKey() : s << "(null)";
}
template <typename keyT>
void node<keyT>::clearLevelKCounters(unsigned int k, unsigned int deepth) {
if (deepth >= k)
return;
for (auto& child : *this)
child.clearLevelKCounters(k, deepth+1);
}
template <typename keyT>
size_t node<keyT>::recursiveAllNodesCount() const {
size_t count = 1;
for (const auto& child : *this)
count += child.recursiveAllNodesCount();
return count;
}
template <typename keyT>
inline void node<keyT>::clearLevelKCounters(unsigned int k) {
clearLevelKCounters(k, 0);
}
template <typename keyT>
void node<keyT>::getAllTreeNodesRef(std::vector< node<keyT>* >& vec) {
vec.push_back(this);
for (auto& child : *this)
child.getAllTreeNodesRef(vec);
}
template <typename keyT>
node<keyT> node<keyT>::kpathR(unsigned int k) {
node res;
node *curr,*ptr;
size_t i=0;
if (!k)
return node(getKey(),val,counter);
res = node(getKey(),val,counter);
ptr = &res;
curr = this->parent;
while (curr && curr->isValid() && i < k) {
ptr = ptr->addChildByVal(node(curr->getKey(),curr->val,counter));
curr = curr->parent;
i++;
}
return res;
}
template <typename keyT>
node<keyT> node<keyT>::kpath(unsigned int k) {
node res;
node *curr,*ptr;
unsigned int i=0;
std::stack<node> list;
if (!k)
return node(getKey(),val,counter);
list.push(node(getKey(),val,counter));
curr=this->parent;
while (curr && curr->isValid() && i < k) {
list.push(node(curr->getKey(),curr->val,counter));
curr = curr->parent;
i++;
}
res = list.top();
list.pop();
ptr = &res;
while (!list.empty()) {
ptr=ptr->addChildByVal(list.top());
list.pop();
}
return res;
}
template <typename keyT>
inline node<keyT>* node<keyT>::addChild(const node<keyT>& n) {
node *t;
assert(!getChildRef(n.getKey()));
t = &children.insert(n.getKey(), n);
t->parent = this;
return t;
}
template <typename keyT>
inline node<keyT>* node<keyT>::replaceChild(node<keyT>& n) {
node *t;
assert(getChildRef(n.getKey()));
t = &children.replace(n.getKey(), n);
t->parent = this;
return t;
}
template <typename keyT>
inline node<keyT>* node<keyT>::getChildRef(keyT k) {
iterator it = children.find(k);
if (it != end())
return &(*it);
return 0;
}
template <typename keyT>
void node<keyT>::autoSetParents() {
for (auto& child : *this) {
child.parent = this;
child.autoSetParents();
}
}