-
Notifications
You must be signed in to change notification settings - Fork 0
/
mind_map.cpp
116 lines (95 loc) · 2.43 KB
/
mind_map.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
#include <iostream>
#include "mind_map.h"
// MapNode //
MapNode::MapNode() : position_(NONE) {}
MapNode::~MapNode() {
for (size_t i = 0; i < children_.size(); ++i) {
delete children_[i];
}
}
std::string MapNode::text() const {
return text_;
}
std::string MapNode::id() const {
return id_;
}
NodePosition MapNode::position() const {
if (position_ == NONE) {
return parent_->position();
}
return position_;
}
NodeTextFont MapNode::font() const {
return font_;
}
std::string MapNode::text_color() const {
return text_color_;
}
std::string MapNode::background_color() const {
return background_color_;
}
std::vector<std::string> MapNode::icons() const {
return icons_;
}
std::vector<ArrowLink> MapNode::arrow_links() const {
return arrow_links_;
}
const MapNode *MapNode::parent() const {
return parent_;
}
std::vector<const MapNode*> MapNode::children() const {
std::vector<const MapNode*> children_collection;
std::vector<MapNode*>::const_iterator children_iterator = children_.begin();
for ( ; children_iterator != children_.end(); ++children_iterator) {
children_collection.push_back(*children_iterator);
}
return children_collection;
}
// Map //
Map::Map() : root_(NULL) {}
Map::~Map() {
delete root_;
}
const MapNode *Map::root() const {
return root_;
}
const MapNode *Map::find_node_by_id(const std::string &node_id) const {
return find_node(root_, node_id);
}
void Map::print() {
if (root_ == NULL) {
return;
}
print_node(root_, 0);
}
void Map::print_node(MapNode const *node, size_t indent) const {
if (node == NULL) {
return;
}
for (size_t i = 0; i < indent; ++i) {
std::cout << " ";
}
std::cout << node->text() << std::endl;
std::vector<const MapNode *> children = node->children();
std::vector<const MapNode *>::const_iterator child = children.begin();
for ( ; child != children.end(); ++child) {
print_node(*child, indent + 1);
}
}
const MapNode *Map::find_node(const MapNode *node, const std::string &node_id) const {
if (node->id() == node_id) {
return node;
}
std::vector<const MapNode *> children = node->children();
if (children.empty()) {
return NULL;
}
std::vector<const MapNode *>::const_iterator child = children.begin();
for ( ; child != children.end(); ++child) {
const MapNode *node = find_node(*child, node_id);
if (node != NULL) {
return node;
}
}
return NULL;
}