]> git.lyx.org Git - features.git/blob - src/Graph.cpp
8d1e445157481c79c521e7e60d58e868df920caf
[features.git] / src / Graph.cpp
1 /**
2  * \file Graph.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur (original code)
7  * \author Richard Heck (re-implementation)
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Graph.h"
15 #include "Format.h"
16
17 #include "support/debug.h"
18 #include "support/lassert.h"
19
20 #include <algorithm>
21
22 using namespace std;
23
24 namespace lyx {
25
26
27 bool Graph::bfs_init(int s, bool clear_visited, queue<int> & Q)
28 {
29         if (s < 0)
30                 return false;
31         
32         if (!Q.empty())
33                 Q = queue<int>();
34
35         if (clear_visited) {
36                 vector<Vertex>::iterator it = vertices_.begin();
37                 vector<Vertex>::iterator en = vertices_.end();
38                 for (; it != en; ++it)
39                         it->visited = false;
40         }
41         if (!vertices_[s].visited) {
42                 Q.push(s);
43                 vertices_[s].visited = true;
44         }
45         return true;
46 }
47
48
49 Graph::EdgePath const
50         Graph::getReachableTo(int target, bool clear_visited)
51 {
52         EdgePath result;
53         queue<int> Q;
54         if (!bfs_init(target, clear_visited, Q))
55                 return result;
56
57         // Here's the logic, which is shared by the other routines.
58         // Q holds a list of nodes we have been able to reach (in this
59         // case, reach backwards). It is initialized to the current node
60         // by bfs_init, and then we recurse, adding the nodes we can reach
61         // from the current node as we go. That makes it a breadth-first
62         // search.
63         while (!Q.empty()) {
64                 int const current = Q.front();
65                 Q.pop();
66                 if (current != target || formats.get(target).name() != "lyx")
67                         result.push_back(current);
68
69                 vector<Arrow *>::iterator it = vertices_[current].in_arrows.begin();
70                 vector<Arrow *>::iterator const end = vertices_[current].in_arrows.end();
71                 for (; it != end; ++it) {
72                         const int cv = (*it)->from;
73                         if (!vertices_[cv].visited) {
74                                 vertices_[cv].visited = true;
75                                 Q.push(cv);
76                         }
77                 }
78         }
79
80         return result;
81 }
82
83
84 Graph::EdgePath const
85         Graph::getReachable(int from, bool only_viewable,
86                 bool clear_visited, vector<int> excludes)
87 {
88         EdgePath result;
89         queue<int> Q;
90         if (!bfs_init(from, clear_visited, Q))
91                 return result;
92
93         while (!Q.empty()) {
94                 int const current = Q.front();
95                 Q.pop();
96                 Format const & format = formats.get(current);
97                 if (!only_viewable || !format.viewer().empty())
98                         result.push_back(current);
99                 else if (format.isChildFormat()) {
100                         Format const * const parent =
101                                 formats.getFormat(format.parentFormat());
102                         if (parent && !parent->viewer().empty())
103                                 result.push_back(current);
104                 }
105
106                 vector<Arrow *>::const_iterator cit =
107                         vertices_[current].out_arrows.begin();
108                 vector<Arrow *>::const_iterator end =
109                         vertices_[current].out_arrows.end();
110                 for (; cit != end; ++cit) {
111                         int const cv = (*cit)->to;
112                         if (!vertices_[cv].visited) {
113                                 vertices_[cv].visited = true;
114                                 if (find(excludes.begin(), excludes.end(), cv) == excludes.end())
115                                         Q.push(cv);
116                         }
117                 }
118         }
119
120         return result;
121 }
122
123
124 Graph::EdgePath const
125         Graph::getReachable(int from, bool only_viewable,
126                 bool clear_visited, int exclude)
127 {
128         vector<int> excludes;
129         excludes.push_back(exclude);
130         return getReachable(from, only_viewable, clear_visited, excludes);
131 }
132
133
134 bool Graph::isReachable(int from, int to)
135 {
136         if (from == to)
137                 return true;
138
139         queue<int> Q;
140         if (to < 0 || !bfs_init(from, true, Q))
141                 return false;
142
143         while (!Q.empty()) {
144                 int const current = Q.front();
145                 Q.pop();
146                 if (current == to)
147                         return true;
148
149                 vector<Arrow *>::const_iterator cit =
150                         vertices_[current].out_arrows.begin();
151                 vector<Arrow *>::const_iterator end =
152                         vertices_[current].out_arrows.end();
153                 for (; cit != end; ++cit) {
154                         int const cv = (*cit)->to;
155                         if (!vertices_[cv].visited) {
156                                 vertices_[cv].visited = true;
157                                 Q.push(cv);
158                         }
159                 }
160         }
161
162         return false;
163 }
164
165
166 Graph::EdgePath const Graph::getPath(int from, int to)
167 {
168         if (from == to)
169                 return EdgePath();
170
171         queue<int> Q;
172         if (to < 0 || !bfs_init(from, true, Q))
173                 return EdgePath();
174
175         vector<EdgePath> pathes;
176         pathes.resize(vertices_.size());
177         while (!Q.empty()) {
178                 int const current = Q.front();
179                 Q.pop();
180
181                 vector<Arrow *>::const_iterator cit =
182                         vertices_[current].out_arrows.begin();
183                 vector<Arrow *>::const_iterator end =
184                         vertices_[current].out_arrows.end();
185                 for (; cit != end; ++cit) {
186                         int const cv = (*cit)->to;
187                         if (!vertices_[cv].visited) {
188                                 vertices_[cv].visited = true;
189                                 Q.push(cv);
190                                 // NOTE If we wanted to collect all the paths, then
191                                 // we just need to collect them here and not worry
192                                 // about "visited".
193                                 EdgePath lastpath = pathes[(*cit)->from];
194                                 lastpath.push_back((*cit)->id);
195                                 pathes[cv] = lastpath;
196                         }
197                         if (cv == to) {
198                                 return pathes[cv];
199                         }
200                 }
201         }
202         // failure
203         return EdgePath();
204 }
205
206
207 void Graph::init(int size)
208 {
209         vertices_ = vector<Vertex>(size);
210         arrows_.clear();
211         numedges_ = 0;
212 }
213
214
215 void Graph::addEdge(int from, int to)
216 {
217         arrows_.push_back(Arrow(from, to, numedges_));
218         numedges_++;
219         Arrow * ar = &(arrows_.back());
220         vertices_[to].in_arrows.push_back(ar);
221         vertices_[from].out_arrows.push_back(ar);
222 }
223
224
225 // At present, we do not need this debugging code, but
226 // I am going to leave it here in case we need it again.
227 #if 0
228 void Graph::dumpGraph() const
229 {
230         vector<Vertex>::const_iterator it = vertices_.begin();
231         vector<Vertex>::const_iterator en = vertices_.end();
232         for (; it != en; ++it) {
233                 LYXERR0("Next vertex...");
234                 LYXERR0("In arrows...");
235                 std::vector<Arrow *>::const_iterator iit = it->in_arrows.begin();
236                 std::vector<Arrow *>::const_iterator ien = it->in_arrows.end();
237                 for (; iit != ien; ++iit)
238                         LYXERR0("From " << (*iit)->from << " to " << (*iit)->to);
239                 LYXERR0("Out arrows...");
240                 iit = it->out_arrows.begin();
241                 ien = it->out_arrows.end();
242                 for (; iit != ien; ++iit)
243                         LYXERR0("From " << (*iit)->from << " to " << (*iit)->to);
244         }
245 }
246 #endif
247
248
249 } // namespace lyx