2003-02-28 09:49:49 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file Graph.cpp
|
2003-02-28 09:49:49 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Dekel Tsur
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-02-28 09:49:49 +00:00
|
|
|
*/
|
|
|
|
|
2003-09-08 09:51:40 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Graph.h"
|
|
|
|
#include "Format.h"
|
2003-02-28 09:49:49 +00:00
|
|
|
|
2003-09-05 22:17:02 +00:00
|
|
|
#include <algorithm>
|
2003-02-28 09:49:49 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2003-02-28 09:49:49 +00:00
|
|
|
|
2007-07-17 17:46:54 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
2003-02-28 09:49:49 +00:00
|
|
|
int Graph::bfs_init(int s, bool clear_visited)
|
|
|
|
{
|
|
|
|
if (s < 0)
|
|
|
|
return s;
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
Q_ = queue<int>();
|
2003-02-28 09:49:49 +00:00
|
|
|
|
|
|
|
if (clear_visited)
|
|
|
|
fill(visited_.begin(), visited_.end(), false);
|
|
|
|
if (visited_[s] == false) {
|
|
|
|
Q_.push(s);
|
|
|
|
visited_[s] = true;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<int> const
|
|
|
|
Graph::getReachableTo(int target, bool clear_visited)
|
|
|
|
{
|
|
|
|
vector<int> result;
|
|
|
|
int const s = bfs_init(target, clear_visited);
|
|
|
|
if (s < 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
while (!Q_.empty()) {
|
|
|
|
int const i = Q_.front();
|
|
|
|
Q_.pop();
|
|
|
|
if (i != s || formats.get(target).name() != "lyx") {
|
|
|
|
result.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<int>::iterator it = vertices_[i].in_vertices.begin();
|
|
|
|
vector<int>::iterator end = vertices_[i].in_vertices.end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (!visited_[*it]) {
|
|
|
|
visited_[*it] = true;
|
|
|
|
Q_.push(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<int> const
|
|
|
|
Graph::getReachable(int from, bool only_viewable,
|
|
|
|
bool clear_visited)
|
|
|
|
{
|
|
|
|
vector<int> result;
|
|
|
|
if (bfs_init(from, clear_visited) < 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
while (!Q_.empty()) {
|
|
|
|
int const i = Q_.front();
|
|
|
|
Q_.pop();
|
|
|
|
Format const & format = formats.get(i);
|
2006-07-31 17:43:59 +00:00
|
|
|
if (!only_viewable || !format.viewer().empty())
|
2003-02-28 09:49:49 +00:00
|
|
|
result.push_back(i);
|
2006-07-31 17:43:59 +00:00
|
|
|
else if (format.isChildFormat()) {
|
|
|
|
Format const * const parent =
|
|
|
|
formats.getFormat(format.parentFormat());
|
|
|
|
if (parent && !parent->viewer().empty())
|
|
|
|
result.push_back(i);
|
|
|
|
}
|
2003-02-28 09:49:49 +00:00
|
|
|
|
|
|
|
vector<int>::const_iterator cit =
|
|
|
|
vertices_[i].out_vertices.begin();
|
|
|
|
vector<int>::const_iterator end =
|
|
|
|
vertices_[i].out_vertices.end();
|
|
|
|
for (; cit != end; ++cit)
|
|
|
|
if (!visited_[*cit]) {
|
|
|
|
visited_[*cit] = true;
|
|
|
|
Q_.push(*cit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Graph::isReachable(int from, int to)
|
|
|
|
{
|
|
|
|
if (from == to)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
int const s = bfs_init(from);
|
|
|
|
if (s < 0 || to < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (!Q_.empty()) {
|
|
|
|
int const i = Q_.front();
|
|
|
|
Q_.pop();
|
|
|
|
if (i == to)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
vector<int>::const_iterator cit =
|
|
|
|
vertices_[i].out_vertices.begin();
|
|
|
|
vector<int>::const_iterator end =
|
|
|
|
vertices_[i].out_vertices.end();
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
if (!visited_[*cit]) {
|
|
|
|
visited_[*cit] = true;
|
|
|
|
Q_.push(*cit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Graph::EdgePath const
|
|
|
|
Graph::getPath(int from, int t)
|
|
|
|
{
|
|
|
|
EdgePath path;
|
|
|
|
if (from == t)
|
|
|
|
return path;
|
|
|
|
|
|
|
|
int const s = bfs_init(from);
|
|
|
|
if (s < 0 || t < 0)
|
|
|
|
return path;
|
|
|
|
|
|
|
|
vector<int> prev_edge(formats.size());
|
|
|
|
vector<int> prev_vertex(formats.size());
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
while (!Q_.empty()) {
|
|
|
|
int const i = Q_.front();
|
|
|
|
Q_.pop();
|
|
|
|
if (i == t) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<int>::const_iterator beg =
|
|
|
|
vertices_[i].out_vertices.begin();
|
|
|
|
vector<int>::const_iterator cit = beg;
|
|
|
|
vector<int>::const_iterator end =
|
|
|
|
vertices_[i].out_vertices.end();
|
|
|
|
for (; cit != end; ++cit)
|
|
|
|
if (!visited_[*cit]) {
|
|
|
|
int const j = *cit;
|
|
|
|
visited_[j] = true;
|
|
|
|
Q_.push(j);
|
|
|
|
int const k = cit - beg;
|
|
|
|
prev_edge[j] = vertices_[i].out_edges[k];
|
|
|
|
prev_vertex[j] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
return path;
|
|
|
|
|
|
|
|
while (t != s) {
|
|
|
|
path.push_back(prev_edge[t]);
|
|
|
|
t = prev_vertex[t];
|
|
|
|
}
|
|
|
|
reverse(path.begin(), path.end());
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graph::init(int size)
|
|
|
|
{
|
|
|
|
vertices_ = vector<Vertex>(size);
|
|
|
|
visited_.resize(size);
|
|
|
|
numedges_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graph::addEdge(int s, int t)
|
|
|
|
{
|
|
|
|
vertices_[t].in_vertices.push_back(s);
|
|
|
|
vertices_[s].out_vertices.push_back(t);
|
|
|
|
vertices_[s].out_edges.push_back(numedges_++);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Graph::Vertex> Graph::vertices_;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|