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:26:01 +00:00
|
|
|
if (s < 0)
|
2009-11-09 02:19:02 +00:00
|
|
|
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
|
2009-11-09 03:02:11 +00:00
|
|
|
Graph::getReachableTo(int target, bool clear_visited)
|
2003-02-28 09:49:49 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2009-11-09 03:43:48 +00:00
|
|
|
// Here's the logic, which is shared by the other routines.
|
|
|
|
// Q_ holds a list of nodes we have been able to reach (in this
|
|
|
|
// case, reach backwards). It is initailized to the current node
|
|
|
|
// by bfs_init, and then we recurse, adding the nodes we can reach
|
|
|
|
// from the current node as we go.
|
2003-02-28 09:49:49 +00:00
|
|
|
while (!Q_.empty()) {
|
2009-11-09 02:26:01 +00:00
|
|
|
int const current = Q_.front();
|
2003-02-28 09:49:49 +00:00
|
|
|
Q_.pop();
|
2009-11-09 03:02:11 +00:00
|
|
|
if (current != target || formats.get(target).name() != "lyx")
|
2009-11-09 02:26:01 +00:00
|
|
|
result.push_back(current);
|
2003-02-28 09:49:49 +00:00
|
|
|
|
2009-11-09 02:26:01 +00:00
|
|
|
vector<int>::iterator it = vertices_[current].in_vertices.begin();
|
|
|
|
vector<int>::iterator end = vertices_[current].in_vertices.end();
|
2003-02-28 09:49:49 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (!visited_[*it]) {
|
|
|
|
visited_[*it] = true;
|
|
|
|
Q_.push(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<int> const
|
2009-11-09 03:02:11 +00:00
|
|
|
Graph::getReachable(int from, bool only_viewable,
|
|
|
|
bool clear_visited)
|
2003-02-28 09:49:49 +00:00
|
|
|
{
|
|
|
|
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()) {
|
2009-11-09 03:02:11 +00:00
|
|
|
int const current = Q_.front();
|
2003-02-28 09:49:49 +00:00
|
|
|
Q_.pop();
|
2009-11-09 03:02:11 +00:00
|
|
|
Format const & format = formats.get(current);
|
2006-07-31 17:43:59 +00:00
|
|
|
if (!only_viewable || !format.viewer().empty())
|
2009-11-09 03:02:11 +00:00
|
|
|
result.push_back(current);
|
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())
|
2009-11-09 03:02:11 +00:00
|
|
|
result.push_back(current);
|
2006-07-31 17:43:59 +00:00
|
|
|
}
|
2003-02-28 09:49:49 +00:00
|
|
|
|
2009-11-09 03:02:11 +00:00
|
|
|
vector<OutEdge>::const_iterator cit =
|
|
|
|
vertices_[current].out_arrows.begin();
|
|
|
|
vector<OutEdge>::const_iterator end =
|
|
|
|
vertices_[current].out_arrows.end();
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
int const cv = cit->vertex;
|
|
|
|
if (!visited_[cv]) {
|
|
|
|
visited_[cv] = true;
|
|
|
|
Q_.push(cv);
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
2009-11-09 03:02:11 +00:00
|
|
|
}
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Graph::isReachable(int from, int to)
|
|
|
|
{
|
|
|
|
if (from == to)
|
|
|
|
return true;
|
|
|
|
|
2009-11-09 02:26:01 +00:00
|
|
|
if (to < 0 || !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;
|
|
|
|
|
2009-11-09 03:02:11 +00:00
|
|
|
vector<OutEdge>::const_iterator cit =
|
|
|
|
vertices_[current].out_arrows.begin();
|
|
|
|
vector<OutEdge>::const_iterator end =
|
|
|
|
vertices_[current].out_arrows.end();
|
2003-02-28 09:49:49 +00:00
|
|
|
for (; cit != end; ++cit) {
|
2009-11-09 03:02:11 +00:00
|
|
|
int const cv = cit->vertex;
|
|
|
|
if (!visited_[cv]) {
|
|
|
|
visited_[cv] = true;
|
|
|
|
Q_.push(cv);
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:26:01 +00:00
|
|
|
if (to < 0 || !bfs_init(from))
|
2003-02-28 09:49:49 +00:00
|
|
|
return path;
|
|
|
|
|
2009-11-09 03:43:48 +00:00
|
|
|
// pair<vertex, edge>
|
|
|
|
vector<pair<int, int> > prev(vertices_.size());
|
2003-02-28 09:49:49 +00:00
|
|
|
|
|
|
|
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-09 03:02:11 +00:00
|
|
|
vector<OutEdge>::const_iterator const beg =
|
|
|
|
vertices_[current].out_arrows.begin();
|
|
|
|
vector<OutEdge>::const_iterator cit = beg;
|
|
|
|
vector<OutEdge>::const_iterator end =
|
|
|
|
vertices_[current].out_arrows.end();
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
int const cv = cit->vertex;
|
|
|
|
if (!visited_[cv]) {
|
|
|
|
visited_[cv] = true;
|
|
|
|
Q_.push(cv);
|
2009-11-09 03:43:48 +00:00
|
|
|
// FIXME This will not do for finding multiple paths.
|
|
|
|
// Perhaps we need a vector, or a set.
|
|
|
|
prev[cv] = pair<int, int>(current, cit->edge);
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
2009-11-09 03:02:11 +00:00
|
|
|
}
|
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 03:43:48 +00:00
|
|
|
path.push_back(prev[to].second);
|
|
|
|
to = prev[to].first;
|
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);
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[from].out_arrows.push_back(OutEdge(to, 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
|