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
|
|
|
|
2009-11-09 04:02:45 +00:00
|
|
|
if (clear_visited) {
|
|
|
|
vector<Vertex>::iterator it = vertices_.begin();
|
|
|
|
vector<Vertex>::iterator en = vertices_.end();
|
|
|
|
for (; it != en; ++it)
|
|
|
|
it->visited = false;
|
|
|
|
}
|
|
|
|
if (!vertices_[s].visited) {
|
2003-02-28 09:49:49 +00:00
|
|
|
Q_.push(s);
|
2009-11-09 04:02:45 +00:00
|
|
|
vertices_[s].visited = true;
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
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
|
2009-11-09 17:56:34 +00:00
|
|
|
// case, reach backwards). It is initialized to the current node
|
2009-11-09 03:43:48 +00:00
|
|
|
// by bfs_init, and then we recurse, adding the nodes we can reach
|
2009-11-09 14:03:10 +00:00
|
|
|
// from the current node as we go. That makes it a breadth-first
|
|
|
|
// search.
|
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 17:56:34 +00:00
|
|
|
vector<Arrow>::iterator it = vertices_[current].in_arrows.begin();
|
|
|
|
vector<Arrow>::iterator const end = vertices_[current].in_arrows.end();
|
2003-02-28 09:49:49 +00:00
|
|
|
for (; it != end; ++it) {
|
2009-11-09 17:56:34 +00:00
|
|
|
const int cv = it->vertex;
|
|
|
|
if (!vertices_[cv].visited) {
|
|
|
|
vertices_[cv].visited = true;
|
|
|
|
Q_.push(cv);
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:56:34 +00:00
|
|
|
vector<Arrow>::const_iterator cit =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.begin();
|
2009-11-09 17:56:34 +00:00
|
|
|
vector<Arrow>::const_iterator end =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.end();
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
int const cv = cit->vertex;
|
2009-11-09 04:02:45 +00:00
|
|
|
if (!vertices_[cv].visited) {
|
|
|
|
vertices_[cv].visited = true;
|
2009-11-09 03:02:11 +00:00
|
|
|
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 17:56:34 +00:00
|
|
|
vector<Arrow>::const_iterator cit =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.begin();
|
2009-11-09 17:56:34 +00:00
|
|
|
vector<Arrow>::const_iterator end =
|
2009-11-09 03:02:11 +00:00
|
|
|
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;
|
2009-11-09 04:02:45 +00:00
|
|
|
if (!vertices_[cv].visited) {
|
|
|
|
vertices_[cv].visited = true;
|
2009-11-09 03:02:11 +00:00
|
|
|
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 17:56:34 +00:00
|
|
|
vector<Arrow>::const_iterator const beg =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.begin();
|
2009-11-09 17:56:34 +00:00
|
|
|
vector<Arrow>::const_iterator cit = beg;
|
|
|
|
vector<Arrow>::const_iterator end =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.end();
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
int const cv = cit->vertex;
|
2009-11-09 04:02:45 +00:00
|
|
|
if (!vertices_[cv].visited) {
|
|
|
|
vertices_[cv].visited = true;
|
2009-11-09 03:02:11 +00:00
|
|
|
Q_.push(cv);
|
2009-11-09 03:43:48 +00:00
|
|
|
// FIXME This will not do for finding multiple paths.
|
2009-11-09 03:53:20 +00:00
|
|
|
// Perhaps we need a vector, or a set. We'll also want
|
|
|
|
// to add this info, even if the node is visited, so
|
|
|
|
// outside this conditional.
|
2009-11-09 03:43:48 +00:00
|
|
|
prev[cv] = pair<int, int>(current, cit->edge);
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
2009-11-09 03:53:20 +00:00
|
|
|
if (cv == to) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
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 17:56:34 +00:00
|
|
|
|
2003-02-28 09:49:49 +00:00
|
|
|
void Graph::init(int size)
|
|
|
|
{
|
|
|
|
vertices_ = vector<Vertex>(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 17:56:34 +00:00
|
|
|
vertices_[to].in_arrows.push_back(Arrow(from, numedges_));
|
|
|
|
vertices_[from].out_arrows.push_back(Arrow(to, numedges_));
|
|
|
|
++numedges_;
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-09 00:55:37 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|