* de.po: sync with branch.
[lyx.git] / src / Graph.cpp
blobe15a7d48ef80194ac2cd7f2036e9b9d3202d308b
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.
6 * \author Dekel Tsur
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "Graph.h"
14 #include "Format.h"
16 #include <algorithm>
18 using namespace std;
20 namespace lyx {
23 int Graph::bfs_init(int s, bool clear_visited)
25 if (s < 0)
26 return s;
28 Q_ = queue<int>();
30 if (clear_visited)
31 fill(visited_.begin(), visited_.end(), false);
32 if (visited_[s] == false) {
33 Q_.push(s);
34 visited_[s] = true;
36 return s;
40 vector<int> const
41 Graph::getReachableTo(int target, bool clear_visited)
43 vector<int> result;
44 int const s = bfs_init(target, clear_visited);
45 if (s < 0)
46 return result;
48 while (!Q_.empty()) {
49 int const i = Q_.front();
50 Q_.pop();
51 if (i != s || formats.get(target).name() != "lyx") {
52 result.push_back(i);
55 vector<int>::iterator it = vertices_[i].in_vertices.begin();
56 vector<int>::iterator end = vertices_[i].in_vertices.end();
57 for (; it != end; ++it) {
58 if (!visited_[*it]) {
59 visited_[*it] = true;
60 Q_.push(*it);
65 return result;
69 vector<int> const
70 Graph::getReachable(int from, bool only_viewable,
71 bool clear_visited)
73 vector<int> result;
74 if (bfs_init(from, clear_visited) < 0)
75 return result;
77 while (!Q_.empty()) {
78 int const i = Q_.front();
79 Q_.pop();
80 Format const & format = formats.get(i);
81 if (!only_viewable || !format.viewer().empty())
82 result.push_back(i);
83 else if (format.isChildFormat()) {
84 Format const * const parent =
85 formats.getFormat(format.parentFormat());
86 if (parent && !parent->viewer().empty())
87 result.push_back(i);
90 vector<int>::const_iterator cit =
91 vertices_[i].out_vertices.begin();
92 vector<int>::const_iterator end =
93 vertices_[i].out_vertices.end();
94 for (; cit != end; ++cit)
95 if (!visited_[*cit]) {
96 visited_[*cit] = true;
97 Q_.push(*cit);
101 return result;
105 bool Graph::isReachable(int from, int to)
107 if (from == to)
108 return true;
110 int const s = bfs_init(from);
111 if (s < 0 || to < 0)
112 return false;
114 while (!Q_.empty()) {
115 int const i = Q_.front();
116 Q_.pop();
117 if (i == to)
118 return true;
120 vector<int>::const_iterator cit =
121 vertices_[i].out_vertices.begin();
122 vector<int>::const_iterator end =
123 vertices_[i].out_vertices.end();
124 for (; cit != end; ++cit) {
125 if (!visited_[*cit]) {
126 visited_[*cit] = true;
127 Q_.push(*cit);
132 return false;
136 Graph::EdgePath const
137 Graph::getPath(int from, int t)
139 EdgePath path;
140 if (from == t)
141 return path;
143 int const s = bfs_init(from);
144 if (s < 0 || t < 0)
145 return path;
147 vector<int> prev_edge(formats.size());
148 vector<int> prev_vertex(formats.size());
150 bool found = false;
151 while (!Q_.empty()) {
152 int const i = Q_.front();
153 Q_.pop();
154 if (i == t) {
155 found = true;
156 break;
159 vector<int>::const_iterator beg =
160 vertices_[i].out_vertices.begin();
161 vector<int>::const_iterator cit = beg;
162 vector<int>::const_iterator end =
163 vertices_[i].out_vertices.end();
164 for (; cit != end; ++cit)
165 if (!visited_[*cit]) {
166 int const j = *cit;
167 visited_[j] = true;
168 Q_.push(j);
169 int const k = cit - beg;
170 prev_edge[j] = vertices_[i].out_edges[k];
171 prev_vertex[j] = i;
174 if (!found)
175 return path;
177 while (t != s) {
178 path.push_back(prev_edge[t]);
179 t = prev_vertex[t];
181 reverse(path.begin(), path.end());
182 return path;
185 void Graph::init(int size)
187 vertices_ = vector<Vertex>(size);
188 visited_.resize(size);
189 numedges_ = 0;
192 void Graph::addEdge(int s, int t)
194 vertices_[t].in_vertices.push_back(s);
195 vertices_[s].out_vertices.push_back(t);
196 vertices_[s].out_edges.push_back(numedges_++);
199 vector<Graph::Vertex> Graph::vertices_;
202 } // namespace lyx