-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tree.cpp
154 lines (132 loc) · 3.81 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
//
// Tree.cpp
// Docker_Dependencies
//
// Created by Wang Yi on 7/4/17.
// Copyright © 2017 Wang Yiyiak.co. All rights reserved.
//
#include "Tree.hpp"
std::ostream& operator<<(std::ostream& o, const TreeNode& t)
{
return t.dump(o);
}
vector<TreeNode*>
TreeNode::wfs()
{
vector<TreeNode*> ret;
queue<TreeNode*> q;
q.push(this);
vector<TreeNode*> children;
int depth = 1;
TreeNode* curr = nullptr;
int curr_no = 1, next_no = 0;
while (!q.empty())
{
curr = q.front(); q.pop();
// visit
ret.push_back(curr);
curr_no --;
auto children = curr->children;
next_no += (int)children.size();
if (curr_no == 0){
depth++;
curr_no = next_no;
next_no = 0;
}
for(auto child : children)
{
q.push(child);
}
}
return ret;
}
// typedef _treenode TreeNode;
vector<TreeNode*>& build_children(TreeNode* parent, vector<string> imgs, unordered_map<string, TreeNode*> visited)
{
int i=0;
for(i=0; i<imgs.size(); i++)
{
TreeNode* nd = nullptr;
// if the node is the root of another tree
if (visited.find(imgs[i]) != visited.end()){
nd = visited[imgs[i]];
} else {
nd = new TreeNode(imgs[i]);
}
nd->set_pos(i);
nd->parent = parent;
parent->children.push_back(nd);
}
return parent->children;
}
vector<TreeNode*> build_Tree_Merged(vector<pair<string, string>>& edges)
{
unordered_map<string, vector<string>> g_edges;
stack<TreeNode*> s;
unordered_map<string, TreeNode*> visited;
TreeNode* curr;
TreeNode* p;
vector<TreeNode*> forest;
vector<TreeNode*> ret;
TreeNode sentinel("Sentinel") ;
for(auto edge : edges)
{
// base tree or directed graph egdes build
string name = edge.second;
if(g_edges.find(name) != g_edges.end())
{
g_edges[name].push_back(edge.first);
} else {
vector<string> children(1, edge.first);
g_edges[name] = children;
forest.push_back(new TreeNode(name));
}
}
// merge trees
for(auto tree: forest){
s.push(tree);
while(!s.empty()){
curr = s.top(); s.pop();
if (visited.find(curr->img) != visited.end())
{
// a circle find
p = curr->parent;
if (p != nullptr)
p->children.erase(p->children.begin() + curr->m_pos);
else { curr->parent = &sentinel; curr->set_pos(sentinel.m_pos++); }
continue;
}
visited[curr->img] = curr;
vector<string> imgs = g_edges[curr->img];
auto children = build_children(curr, imgs, visited);
for (auto child : children){ s.push(child); }
} // end while
} // end for
// be considered susptect for copy cost
for(auto tree : forest){
if (tree->parent == nullptr){
TreeNodePrinter tnp(tree);
std::cout << "------" << std::endl;
std::cout << tnp._str();
ret.push_back(tree);
}
}
return ret;
}
shared_ptr<vector<string>> DockerBuildDependency(vector<pair<string, string>>& Relation)
{
shared_ptr<vector<string>> ret(new vector<string>());
vector<TreeNode*> forest = build_Tree_Merged(Relation);
for (auto tree : forest)
{
vector<TreeNode*> nd_seq = tree->wfs();
vector<string> imgs_seq;
for(auto nd : nd_seq)
{
imgs_seq.push_back(nd->img);
}
ret->insert(ret->end(), imgs_seq.begin(), imgs_seq.end());
delete tree;
}
return ret;
}