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