]> git.lyx.org Git - lyx.git/blob - src/graph.C
STLPort compile fix
[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 (format.name() == "lyx")
81                         continue;
82                 if (!only_viewable || !format.viewer().empty() ||
83                     format.isChildFormat())
84                         result.push_back(i);
85
86                 vector<int>::const_iterator cit =
87                         vertices_[i].out_vertices.begin();
88                 vector<int>::const_iterator end =
89                         vertices_[i].out_vertices.end();
90                 for (; cit != end; ++cit)
91                         if (!visited_[*cit]) {
92                                 visited_[*cit] = true;
93                                 Q_.push(*cit);
94                         }
95         }
96
97         return result;
98 }
99
100
101 bool Graph::isReachable(int from, int to)
102 {
103         if (from == to)
104                 return true;
105
106         int const s = bfs_init(from);
107         if (s < 0 || to < 0)
108                 return false;
109
110         while (!Q_.empty()) {
111                 int const i = Q_.front();
112                 Q_.pop();
113                 if (i == to)
114                         return true;
115
116                 vector<int>::const_iterator cit =
117                         vertices_[i].out_vertices.begin();
118                 vector<int>::const_iterator end =
119                         vertices_[i].out_vertices.end();
120                 for (; cit != end; ++cit) {
121                         if (!visited_[*cit]) {
122                                 visited_[*cit] = true;
123                                 Q_.push(*cit);
124                         }
125                 }
126         }
127
128         return false;
129 }
130
131
132 Graph::EdgePath const
133 Graph::getPath(int from, int t)
134 {
135         EdgePath path;
136         if (from == t)
137                 return path;
138
139         int const s = bfs_init(from);
140         if (s < 0 || t < 0)
141                 return path;
142
143         vector<int> prev_edge(formats.size());
144         vector<int> prev_vertex(formats.size());
145
146         bool found = false;
147         while (!Q_.empty()) {
148                 int const i = Q_.front();
149                 Q_.pop();
150                 if (i == t) {
151                         found = true;
152                         break;
153                 }
154
155                 vector<int>::const_iterator beg =
156                         vertices_[i].out_vertices.begin();
157                 vector<int>::const_iterator cit = beg;
158                 vector<int>::const_iterator end =
159                         vertices_[i].out_vertices.end();
160                 for (; cit != end; ++cit)
161                         if (!visited_[*cit]) {
162                                 int const j = *cit;
163                                 visited_[j] = true;
164                                 Q_.push(j);
165                                 int const k = cit - beg;
166                                 prev_edge[j] = vertices_[i].out_edges[k];
167                                 prev_vertex[j] = i;
168                         }
169         }
170         if (!found)
171                 return path;
172
173         while (t != s) {
174                 path.push_back(prev_edge[t]);
175                 t = prev_vertex[t];
176         }
177         reverse(path.begin(), path.end());
178         return path;
179 }
180
181 void Graph::init(int size)
182 {
183         vertices_ = vector<Vertex>(size);
184         visited_.resize(size);
185         numedges_ = 0;
186 }
187
188 void Graph::addEdge(int s, int t)
189 {
190         vertices_[t].in_vertices.push_back(s);
191         vertices_[s].out_vertices.push_back(t);
192         vertices_[s].out_edges.push_back(numedges_++);
193 }
194
195 vector<Graph::Vertex> Graph::vertices_;