]> git.lyx.org Git - features.git/blob - src/Graph.cpp
Just some style, and some comments, as I try to figure this out.
[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
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 namespace std;
19
20 namespace lyx {
21
22
23 int Graph::bfs_init(int s, bool clear_visited)
24 {
25         if (s < 0)
26                 return s;
27
28         Q_ = 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 (!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);
88                 }
89
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);
98                         }
99         }
100
101         return result;
102 }
103
104
105 bool Graph::isReachable(int from, int to)
106 {
107         if (from == to)
108                 return true;
109
110         if (to < 0 || bfs_init(from) < 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 Graph::getPath(int from, int to)
136 {
137         EdgePath path;
138         if (from == to)
139                 return path;
140
141         int const s = bfs_init(from);
142         if (s < 0 || to < 0)
143                 return path;
144
145         vector<int> prev_edge(formats.size());
146         vector<int> prev_vertex(formats.size());
147
148         bool found = false;
149         while (!Q_.empty()) {
150                 int const i = Q_.front();
151                 Q_.pop();
152                 if (i == to) {
153                         found = true;
154                         break;
155                 }
156
157                 vector<int>::const_iterator const beg =
158                         vertices_[i].out_vertices.begin();
159                 vector<int>::const_iterator cit = beg;
160                 vector<int>::const_iterator end =
161                         vertices_[i].out_vertices.end();
162                 for (; cit != end; ++cit)
163                         if (!visited_[*cit]) {
164                                 int const j = *cit;
165                                 visited_[j] = true;
166                                 Q_.push(j);
167                                 int const k = cit - beg;
168                                 prev_edge[j] = vertices_[i].out_edges[k];
169                                 prev_vertex[j] = i;
170                         }
171         }
172         if (!found)
173                 return path;
174
175         while (to != s) {
176                 path.push_back(prev_edge[to]);
177                 to = prev_vertex[to];
178         }
179         reverse(path.begin(), path.end());
180         return path;
181 }
182
183
184 void Graph::init(int size)
185 {
186         vertices_ = vector<Vertex>(size);
187         visited_.resize(size);
188         numedges_ = 0;
189 }
190
191
192 void Graph::addEdge(int from, int to)
193 {
194         vertices_[to].in_vertices.push_back(from);
195         vertices_[from].out_vertices.push_back(to);
196         vertices_[from].out_edges.push_back(numedges_++);
197 }
198
199
200 vector<Graph::Vertex> Graph::vertices_;
201
202
203 } // namespace lyx