3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Dekel Tsur (original code)
7 * \author Richard Heck (re-implementation)
9 * Full author contact details are available in file CREDITS.
17 #include "support/debug.h"
18 #include "support/lassert.h"
27 bool Graph::bfs_init(int s, bool clear_visited)
35 vector<Vertex>::iterator it = vertices_.begin();
36 vector<Vertex>::iterator en = vertices_.end();
37 for (; it != en; ++it)
40 if (!vertices_[s].visited) {
42 vertices_[s].visited = true;
48 void Graph::clearPaths()
50 vector<Vertex>::iterator it = vertices_.begin();
51 vector<Vertex>::iterator en = vertices_.end();
52 for (; it != en; ++it)
58 Graph::getReachableTo(int target, bool clear_visited)
60 Mutex::Locker lock(&mutex_);
63 if (!bfs_init(target, clear_visited))
66 // Here's the logic, which is shared by the other routines.
67 // Q_ holds a list of nodes we have been able to reach (in this
68 // case, reach backwards). It is initialized to the current node
69 // by bfs_init, and then we recurse, adding the nodes we can reach
70 // from the current node as we go. That makes it a breadth-first
73 int const current = Q_.front();
75 if (current != target || formats.get(target).name() != "lyx")
76 result.push_back(current);
78 vector<Arrow *>::iterator it = vertices_[current].in_arrows.begin();
79 vector<Arrow *>::iterator const end = vertices_[current].in_arrows.end();
80 for (; it != end; ++it) {
81 const int cv = (*it)->from;
82 if (!vertices_[cv].visited) {
83 vertices_[cv].visited = true;
94 Graph::getReachable(int from, bool only_viewable,
97 Mutex::Locker lock(&mutex_);
100 if (!bfs_init(from, clear_visited))
103 while (!Q_.empty()) {
104 int const current = Q_.front();
106 Format const & format = formats.get(current);
107 if (!only_viewable || !format.viewer().empty())
108 result.push_back(current);
109 else if (format.isChildFormat()) {
110 Format const * const parent =
111 formats.getFormat(format.parentFormat());
112 if (parent && !parent->viewer().empty())
113 result.push_back(current);
116 vector<Arrow *>::const_iterator cit =
117 vertices_[current].out_arrows.begin();
118 vector<Arrow *>::const_iterator end =
119 vertices_[current].out_arrows.end();
120 for (; cit != end; ++cit) {
121 int const cv = (*cit)->to;
122 if (!vertices_[cv].visited) {
123 vertices_[cv].visited = true;
133 bool Graph::isReachable(int from, int to)
135 Mutex::Locker lock(&mutex_);
140 if (to < 0 || !bfs_init(from))
143 while (!Q_.empty()) {
144 int const current = Q_.front();
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;
166 Graph::EdgePath const Graph::getPath(int from, int to)
168 Mutex::Locker lock(&mutex_);
173 if (to < 0 || !bfs_init(from))
177 while (!Q_.empty()) {
178 int const current = Q_.front();
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;
190 // NOTE If we wanted to collect all the paths, then
191 // we just need to collect them here and not worry
193 EdgePath lastpath = vertices_[(*cit)->from].path;
194 lastpath.push_back((*cit)->id);
195 vertices_[cv].path = lastpath;
198 return vertices_[cv].path;
207 void Graph::init(int size)
209 Mutex::Locker lock(&mutex_);
211 vertices_ = vector<Vertex>(size);
217 void Graph::addEdge(int from, int to)
219 Mutex::Locker lock(&mutex_);
221 arrows_.push_back(Arrow(from, to, numedges_));
223 Arrow * ar = &(arrows_.back());
224 vertices_[to].in_arrows.push_back(ar);
225 vertices_[from].out_arrows.push_back(ar);
229 // At present, we do not need this debugging code, but
230 // I am going to leave it here in case we need it again.
232 void Graph::dumpGraph() const
234 vector<Vertex>::const_iterator it = vertices_.begin();
235 vector<Vertex>::const_iterator en = vertices_.end();
236 for (; it != en; ++it) {
237 LYXERR0("Next vertex...");
238 LYXERR0("In arrows...");
239 std::vector<Arrow *>::const_iterator iit = it->in_arrows.begin();
240 std::vector<Arrow *>::const_iterator ien = it->in_arrows.end();
241 for (; iit != ien; ++iit)
242 LYXERR0("From " << (*iit)->from << " to " << (*iit)->to);
243 LYXERR0("Out arrows...");
244 iit = it->out_arrows.begin();
245 ien = it->out_arrows.end();
246 for (; iit != ien; ++iit)
247 LYXERR0("From " << (*iit)->from << " to " << (*iit)->to);