-
Notifications
You must be signed in to change notification settings - Fork 0
/
DP_Stractural_composite.cpp
183 lines (172 loc) · 4.92 KB
/
DP_Stractural_composite.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
//Made by Israel at 15/03/22, preventing circles in hierarchy.. Have a good day you there! (user... ;)
#include <iostream>
#include <list>
class Component {
public:
Component* parent_ = nullptr;
//next 4 are the base:
virtual void operation() = 0;
virtual void add(Component*) = 0;
virtual void Remove(Component*) = 0;
virtual void GetChild(Component*) = 0;
//next 6 for circles check, using saved pointer to parent as well:
virtual int hierarchy_check() = 0;
virtual const std::list<Component*> get_tree_hierarchy() = 0;
virtual int check_every_Child(Component*) { return 0; }
virtual int get_tree_size() { return 0; }
void SetParent(Component* parent) {
this->parent_ = parent;
}
Component* GetParent() const {
if (parent_ != nullptr)
return this->parent_;
std::cout << "nullptr reached\n";
}
virtual ~Component() {} //good practice
};
class Leaf :public Component {
private:
std::string name;
int node_type = 0; //Leaf type
public:
Leaf(std::string my_name) {
name = my_name;
}
void operation() {
std::cout << "Doing the right things in leaf "<<this->name<<"!\n";
}
void add(Component*) {}
void Remove(Component*) {}
void GetChild(Component*) {}
int hierarchy_check() {
return node_type;
}
const std::list<Component*> get_tree_hierarchy() { return this->get_tree_hierarchy(); }
};
class Composite :public Component {
std::list<Component*> tree_hierarchy; //Can have many leafs and branches, circles are not good (branch1 son of branch1 in hierarchy somewhere).
int node_type = 1; //not a leaf
public:
//Doing wanted operation/s
void operation() {
for (Component* child : tree_hierarchy)
{
child->operation();
}
}
const std::list<Component*>get_tree_hierarchy()override {
if (tree_hierarchy.size() != 0)
return tree_hierarchy;
}
//List isn't empty:
int get_tree_size() override {
return tree_hierarchy.size();
}
//checking circles toward sons, like branch2.add(branch1) while branch2 is branch1 son...
int check_every_Child(Component* node) override {
int is_true=0; //sum reccursion result
if (node) {
if (node->get_tree_size() !=0) {
for (auto child : node->get_tree_hierarchy())
if (child != node->get_tree_hierarchy().back()) {
if (this == child) {
is_true += 1;
return is_true;
}
is_true+=check_every_Child(child);
}
else
if (this == child) {
is_true += 1;
return is_true;
}
}
}
if (is_true==0)
return 0;
return 1;
}
//Be uniqe in the tree... checking for circles:
bool Check_uniqe_circle_preventation(Component* node) {
if (node->hierarchy_check() == 0) //It's a leaf
return false;
//need to check:
//1: does dad point to son in hierarchy
//2: check it's not the same (tree.add(tree))
//3: does son point to dad in hierarchy
//1:
if (tree_hierarchy.size()!=0)
for (auto child : tree_hierarchy) {
if (child) //not nullptr
if(child->hierarchy_check() != 0) //not a leaf (so we can have common leaf, otherwise it would say it's a circle.
if (node == child->GetParent())
return true;
}
//2:
if (node == this)
return true;
//3:
return check_every_Child(node);
}
//Adding leaf or composite to branch/main tree, while checking for unnwanted circles:
void add(Component* node) override {
bool is_exist = Check_uniqe_circle_preventation(node);
if (!is_exist) {
tree_hierarchy.push_back(node);
node->SetParent(this);
}
else
std::cout << "Circle preventing adding this composite again!\n";
}
//Removing specific node
void Remove(Component* node) override {
tree_hierarchy.remove(node);
node->SetParent(nullptr);
}
//Doing every childs operations:
void GetChild(Component* h) override {
for(auto child:tree_hierarchy)
if (child!= tree_hierarchy.back())
{
GetChild(child);
}
else {
child->operation();
}
}
//Leaf (0) or composite (1) check:
int hierarchy_check() {
return node_type;
}
};
int main() {
std::cout << "all right, lets begin!\n";
Component *tree = new Composite();
Leaf* leefy1 = new Leaf("first_L");
Leaf* leefy2 = new Leaf("Sec_L");
Leaf* leefy3 = new Leaf("Third_L");
Component* Branch1 = new Composite();
Component* Branch2 = new Composite();
tree->add(tree); //Circle 1
tree->add(leefy1);
Branch1->add(leefy1); //many same leafs..
Branch1->add(leefy1);
Branch1->add(leefy2);
Branch1->add(leefy2);
Branch2->add(leefy2);
Branch2->add(leefy3);
Branch1->add(Branch2); //Haveing common leaf, it's ok.
Branch1->add(Branch1); //Circle 2
Branch2->add(Branch1); //Circle 3
tree->add(Branch1);
tree->add(Branch2); //Not circle as I understand it
Branch1->add(tree); //Circle 4
Branch2->add(tree); //Circle 5
tree->operation();
delete tree;
delete leefy1;
delete leefy2;
delete leefy3;
delete Branch1;
delete Branch2;
}