]> git.lyx.org Git - lyx.git/blob - src/graph.C
Add converter to plain text and fix child format buglet
[lyx.git] / src / graph.C
1 /**
2  * \file graph.C
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
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "graph.h"
14 #include "format.h"
15
16 #include <algorithm>
17
18 using std::vector;
19 using std::reverse;
20
21
22 int Graph::bfs_init(int s, bool clear_visited)
23 {
24         if (s < 0)
25                 return s;
26
27         Q_ = std::queue<int>();
28
29         if (clear_visited)
30                 fill(visited_.begin(), visited_.end(), false);
31         if (visited_[s] == false) {
32                 Q_.push(s);
33                 visited_[s] = true;
34         }
35         return s;
36 }
37
38
39 vector<int> const
40 Graph::getReachableTo(int target, bool clear_visited)
41 {
42         vector<int> result;
43         int const s = bfs_init(target, clear_visited);
44         if (s < 0)
45                 return result;
46
47         while (!Q_.empty()) {
48                 int const i = Q_.front();
49                 Q_.pop();
50                 if (i != s || formats.get(target).name() != "lyx") {
51                         result.push_back(i);
52                 }
53
54                 vector<int>::iterator it = vertices_[i].in_vertices.begin();
55                 vector<int>::iterator end = vertices_[i].in_vertices.end();
56                 for (; it != end; ++it) {
57                         if (!visited_[*it]) {
58                                 visited_[*it] = true;
59                                 Q_.push(*it);
60                         }
61                 }
62         }
63
64         return result;
65 }
66
67
68 vector<int> const
69 Graph::getReachable(int from, bool only_viewable,
70                     bool clear_visited)
71 {
72         vector<int> result;
73         if (bfs_init(from, clear_visited) < 0)
74                 return result;
75
76         while (!Q_.empty()) {
77                 int const i = Q_.front();
78                 Q_.pop();
79                 Format const & format = formats.get(i);
80                 if (!only_viewable || !format.viewer().empty())
81                         result.push_back(i);
82                 else if (format.isChildFormat()) {
83                         Format const * const parent =
84                                 formats.getFormat(format.parentFormat());
85                         if (parent && !parent->viewer().empty())
86                                 result.push_back(i);
87                 }
88
89                 vector<int>::const_iterator cit =
90                         vertices_[i].out_vertices.begin();
91                 vector<int>::const_iterator end =
92                         vertices_[i].out_vertices.end();
93                 for (; cit != end; ++cit)
94                         if (!visited_[*cit]) {
95                                 visited_[*cit] = true;
96                                 Q_.push(*cit);
97                         }
98         }
99
100         return result;
101 }
102
103
104 bool Graph::isReachable(int from, int to)
105 {
106         if (from == to)
107                 return true;
108
109         int const s = bfs_init(from);
110         if (s < 0 || to < 0)
111                 return false;
112
113         while (!Q_.empty()) {
114                 int const i = Q_.front();
115                 Q_.pop();
116                 if (i == to)
117                         return true;
118
119                 vector<int>::const_iterator cit =
120                         vertices_[i].out_vertices.begin();
121                 vector<int>::const_iterator end =
122                         vertices_[i].out_vertices.end();
123                 for (; cit != end; ++cit) {
124                         if (!visited_[*cit]) {
125                                 visited_[*cit] = true;
126                                 Q_.push(*cit);
127                         }
128                 }
129         }
130
131         return false;
132 }
133
134
135 Graph::EdgePath const
136 Graph::getPath(int from, int t)
137 {
138         EdgePath path;
139         if (from == t)
140                 return path;
141
142         int const s = bfs_init(from);
143         if (s < 0 || t < 0)
144                 return path;
145
146         vector<int> prev_edge(formats.size());
147         vector<int> prev_vertex(formats.size());
148
149         bool found = false;
150         while (!Q_.empty()) {
151                 int const i = Q_.front();
152                 Q_.pop();
153                 if (i == t) {
154                         found = true;
155                         break;
156                 }
157
158                 vector<int>::const_iterator beg =
159                         vertices_[i].out_vertices.begin();
160                 vector<int>::const_iterator cit = beg;
161                 vector<int>::const_iterator end =
162                         vertices_[i].out_vertices.end();
163                 for (; cit != end; ++cit)
164                         if (!visited_[*cit]) {
165                                 int const j = *cit;
166                                 visited_[j] = true;
167                                 Q_.push(j);
168                                 int const k = cit - beg;
169                                 prev_edge[j] = vertices_[i].out_edges[k];
170                                 prev_vertex[j] = i;
171                         }
172         }
173         if (!found)
174                 return path;
175
176         while (t != s) {
177                 path.push_back(prev_edge[t]);
178                 t = prev_vertex[t];
179         }
180         reverse(path.begin(), path.end());
181         return path;
182 }
183
184 void Graph::init(int size)
185 {
186         vertices_ = vector<Vertex>(size);
187         visited_.resize(size);
188         numedges_ = 0;
189 }
190
191 void Graph::addEdge(int s, int t)
192 {
193         vertices_[t].in_vertices.push_back(s);
194         vertices_[s].out_vertices.push_back(t);
195         vertices_[s].out_edges.push_back(numedges_++);
196 }
197
198 vector<Graph::Vertex> Graph::vertices_;