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 {
|
|
|
|
|
|
|
|
|
2009-11-09 02:19:02 +00:00
|
|
|
bool Graph::bfs_init(int s, bool clear_visited)
|
2003-02-28 09:49:49 +00:00
|
|
|
{
|
2009-11-09 02:19:02 +00:00
|
|
|
if (s < 0 || s >= vertices_.size())
|
|
|
|
return false;
|
2003-02-28 09:49:49 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2009-11-09 02:19:02 +00:00
|
|
|
return true;
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<int> const
|
|
|
|
Graph::getReachableTo(int target, bool clear_visited)
|
|
|
|
{
|
|
|
|
vector<int> result;
|
2009-11-09 02:19:02 +00:00
|
|
|
if (!bfs_init(target, clear_visited))
|
2003-02-28 09:49:49 +00:00
|
|
|
return result;
|
|
|
|
|
|
|
|
while (!Q_.empty()) {
|
|
|
|
int const i = Q_.front();
|
|
|
|
Q_.pop();
|
2009-11-09 02:19:02 +00:00
|
|
|
if (i != target || formats.get(target).name() != "lyx") {
|
2003-02-28 09:49:49 +00:00
|
|
|
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;
|
2009-11-09 02:19:02 +00:00
|
|
|
if (!bfs_init(from, clear_visited))
|
2003-02-28 09:49:49 +00:00
|
|
|
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;
|
|
|
|
|
2009-11-09 02:19:02 +00:00
|
|
|
if (to < 0 || to >= vertices_.size() || !bfs_init(from))
|
2003-02-28 09:49:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
while (!Q_.empty()) {
|
2009-11-09 02:19:02 +00:00
|
|
|
int const current = Q_.front();
|
2003-02-28 09:49:49 +00:00
|
|
|
Q_.pop();
|
2009-11-09 02:19:02 +00:00
|
|
|
if (current == to)
|
2003-02-28 09:49:49 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
vector<int>::const_iterator cit =
|
2009-11-09 02:19:02 +00:00
|
|
|
vertices_[current].out_vertices.begin();
|
2003-02-28 09:49:49 +00:00
|
|
|
vector<int>::const_iterator end =
|
2009-11-09 02:19:02 +00:00
|
|
|
vertices_[current].out_vertices.end();
|
2003-02-28 09:49:49 +00:00
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
if (!visited_[*cit]) {
|
|
|
|
visited_[*cit] = true;
|
|
|
|
Q_.push(*cit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-09 00:55:37 +00:00
|
|
|
Graph::EdgePath const Graph::getPath(int from, int to)
|
2003-02-28 09:49:49 +00:00
|
|
|
{
|
|
|
|
EdgePath path;
|
2009-11-09 00:55:37 +00:00
|
|
|
if (from == to)
|
2003-02-28 09:49:49 +00:00
|
|
|
return path;
|
|
|
|
|
2009-11-09 02:19:02 +00:00
|
|
|
if (to < 0 || to >= vertices_.size() || !bfs_init(from))
|
2003-02-28 09:49:49 +00:00
|
|
|
return path;
|
|
|
|
|
|
|
|
vector<int> prev_edge(formats.size());
|
|
|
|
vector<int> prev_vertex(formats.size());
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
while (!Q_.empty()) {
|
2009-11-09 02:19:02 +00:00
|
|
|
int const current = Q_.front();
|
2003-02-28 09:49:49 +00:00
|
|
|
Q_.pop();
|
2009-11-09 02:19:02 +00:00
|
|
|
if (current == to) {
|
2003-02-28 09:49:49 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-08 22:37:28 +00:00
|
|
|
vector<int>::const_iterator const beg =
|
2009-11-09 02:19:02 +00:00
|
|
|
vertices_[current].out_vertices.begin();
|
2003-02-28 09:49:49 +00:00
|
|
|
vector<int>::const_iterator cit = beg;
|
|
|
|
vector<int>::const_iterator end =
|
2009-11-09 02:19:02 +00:00
|
|
|
vertices_[current].out_vertices.end();
|
2003-02-28 09:49:49 +00:00
|
|
|
for (; cit != end; ++cit)
|
|
|
|
if (!visited_[*cit]) {
|
|
|
|
int const j = *cit;
|
|
|
|
visited_[j] = true;
|
|
|
|
Q_.push(j);
|
|
|
|
int const k = cit - beg;
|
2009-11-09 02:19:02 +00:00
|
|
|
prev_edge[j] = vertices_[current].out_edges[k];
|
|
|
|
prev_vertex[j] = current;
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
return path;
|
|
|
|
|
2009-11-09 02:19:02 +00:00
|
|
|
while (to != from) {
|
2009-11-09 00:55:37 +00:00
|
|
|
path.push_back(prev_edge[to]);
|
|
|
|
to = prev_vertex[to];
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
reverse(path.begin(), path.end());
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2009-11-09 00:55:37 +00:00
|
|
|
|
2003-02-28 09:49:49 +00:00
|
|
|
void Graph::init(int size)
|
|
|
|
{
|
|
|
|
vertices_ = vector<Vertex>(size);
|
|
|
|
visited_.resize(size);
|
|
|
|
numedges_ = 0;
|
|
|
|
}
|
|
|
|
|
2009-11-09 00:55:37 +00:00
|
|
|
|
|
|
|
void Graph::addEdge(int from, int to)
|
2003-02-28 09:49:49 +00:00
|
|
|
{
|
2009-11-09 00:55:37 +00:00
|
|
|
vertices_[to].in_vertices.push_back(from);
|
|
|
|
vertices_[from].out_vertices.push_back(to);
|
|
|
|
vertices_[from].out_edges.push_back(numedges_++);
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-09 00:55:37 +00:00
|
|
|
|
2003-02-28 09:49:49 +00:00
|
|
|
vector<Graph::Vertex> Graph::vertices_;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|