forked from GothenburgBitFactory/libshared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.cpp
275 lines (233 loc) · 7.6 KB
/
Tree.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2010 - 2021, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <Tree.h>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <shared.h>
#include <format.h>
////////////////////////////////////////////////////////////////////////////////
// - Tree, Branch and Node are synonymous.
// - A Tree may contain any number of branches.
// - A Branch may contain any number of name/value pairs, unique by name.
// - The destructor will delete all branches recursively.
// - Tree::enumerate is a snapshot, and is invalidated by modification.
// - Branch sequence is preserved.
void Tree::addBranch (std::shared_ptr <Tree> branch)
{
if (! branch)
throw "Failed to allocate memory for parse tree.";
_branches.push_back (branch);
}
////////////////////////////////////////////////////////////////////////////////
void Tree::removeBranch (std::shared_ptr <Tree> branch)
{
for (auto i = _branches.begin (); i != _branches.end (); ++i)
{
if (branch == *i)
{
_branches.erase (i);
return;
}
}
}
////////////////////////////////////////////////////////////////////////////////
void Tree::removeAllBranches ()
{
_branches.erase (_branches.begin (), _branches.end ());
}
////////////////////////////////////////////////////////////////////////////////
void Tree::replaceBranch (std::shared_ptr <Tree> from, std::shared_ptr <Tree> to)
{
for (unsigned int i = 0; i < _branches.size (); ++i)
{
if (_branches[i] == from)
{
_branches[i] = to;
return;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Accessor for attributes.
void Tree::attribute (const std::string& name, const std::string& value)
{
_attributes[name] = value;
}
////////////////////////////////////////////////////////////////////////////////
// Accessor for attributes.
void Tree::attribute (const std::string& name, const int value)
{
_attributes[name] = format (value);
}
////////////////////////////////////////////////////////////////////////////////
// Accessor for attributes.
void Tree::attribute (const std::string& name, const double value)
{
_attributes[name] = format (value, 1, 8);
}
////////////////////////////////////////////////////////////////////////////////
// Accessor for attributes.
std::string Tree::attribute (const std::string& name)
{
// Prevent autovivification.
auto i = _attributes.find (name);
if (i != _attributes.end ())
return i->second;
return "";
}
////////////////////////////////////////////////////////////////////////////////
void Tree::removeAttribute (const std::string& name)
{
_attributes.erase (name);
}
////////////////////////////////////////////////////////////////////////////////
// Recursively builds a list of std::shared_ptr <Tree> objects, left to right,
// depth first. The reason for the depth-first enumeration is that a client may
// wish to traverse the tree and delete nodes. With a depth-first iteration,
// this is a safe mechanism, and a node pointer will never be dereferenced after
// it has been deleted.
void Tree::enumerate (std::vector <std::shared_ptr <Tree>>& all) const
{
for (auto& i : _branches)
{
i->enumerate (all);
all.push_back (i);
}
}
////////////////////////////////////////////////////////////////////////////////
bool Tree::hasTag (const std::string& tag) const
{
return std::find (_tags.begin (), _tags.end (), tag) != _tags.end ();
}
////////////////////////////////////////////////////////////////////////////////
void Tree::tag (const std::string& tag)
{
if (! hasTag (tag))
_tags.push_back (tag);
}
////////////////////////////////////////////////////////////////////////////////
void Tree::unTag (const std::string& tag)
{
auto i = std::find (_tags.begin (), _tags.end (), tag);
if (i != _tags.end ())
_tags.erase (i);
}
////////////////////////////////////////////////////////////////////////////////
int Tree::countTags () const
{
return _tags.size ();
}
////////////////////////////////////////////////////////////////////////////////
int Tree::count () const
{
// This branch.
int total = 1;
// Recurse and count the branches.
for (auto& i : _branches)
total += i->count ();
return total;
}
////////////////////////////////////////////////////////////////////////////////
std::shared_ptr <Tree> Tree::find (const std::string& path)
{
std::vector <std::string> elements = split (path, '/');
// Must start at the trunk.
auto cursor = std::make_shared <Tree> (*this);
auto it = elements.begin ();
if (cursor->_name != *it)
return nullptr;
// Perhaps the trunk is what is needed?
if (elements.size () == 1)
return cursor;
// Now look for the next branch.
for (++it; it != elements.end (); ++it)
{
bool found = false;
// If the cursor has a branch that matches *it, proceed.
for (auto i = cursor->_branches.begin (); i != cursor->_branches.end (); ++i)
{
if ((*i)->_name == *it)
{
cursor = *i;
found = true;
break;
}
}
if (! found)
return nullptr;
}
return cursor;
}
////////////////////////////////////////////////////////////////////////////////
std::string Tree::dumpNode (
const std::shared_ptr <Tree> t,
int depth) const
{
std::stringstream out;
// Dump node
for (int i = 0; i < depth; ++i)
out << " ";
out
// Useful for debugging tree node new/delete errors.
// << std::hex << t << " "
<< "\033[1m" << t->_name << "\033[0m";
// Dump attributes.
std::string atts;
for (auto& a : t->_attributes)
{
if (atts != "")
atts += ' ';
atts += a.first + "='\033[33m" + a.second + "\033[0m'";
}
if (atts.length ())
out << ' ' << atts;
// Dump tags.
std::string tags;
for (auto& tag : t->_tags)
{
if (tags.length ())
tags += ' ';
tags += "\033[32m" + tag + "\033[0m";
}
if (tags.length ())
out << ' ' << tags;
out << '\n';
// Recurse for branches.
for (auto& b : t->_branches)
out << dumpNode (b, depth + 1);
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string Tree::dump () const
{
std::stringstream out;
out << "Tree (" << count () << " nodes)\n"
<< dumpNode (std::make_shared <Tree> (*this), 1);
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////