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.
|
|
|
|
*
|
2009-11-10 01:18:43 +00:00
|
|
|
* \author Dekel Tsur (original code)
|
|
|
|
* \author Richard Heck (re-implementation)
|
2003-02-28 09:49:49 +00:00
|
|
|
*
|
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
|
|
|
|
2009-11-10 01:18:43 +00:00
|
|
|
#include "support/debug.h"
|
|
|
|
#include "support/lassert.h"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-10 01:18:43 +00:00
|
|
|
void Graph::clearMarks()
|
|
|
|
{
|
|
|
|
Arrows::iterator it = arrows_.begin();
|
|
|
|
Arrows::iterator const en = arrows_.end();
|
|
|
|
for (; it != en; ++it)
|
|
|
|
it->marked = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-10 01:18:43 +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-10 01:18:43 +00:00
|
|
|
const int cv = (*it)->from;
|
2009-11-09 17:56:34 +00:00
|
|
|
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-10 01:18:43 +00:00
|
|
|
vector<Arrow *>::const_iterator cit =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.begin();
|
2009-11-10 01:18:43 +00:00
|
|
|
vector<Arrow *>::const_iterator end =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.end();
|
|
|
|
for (; cit != end; ++cit) {
|
2009-11-10 01:18:43 +00:00
|
|
|
int const cv = (*cit)->to;
|
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-10 01:18:43 +00:00
|
|
|
vector<Arrow *>::const_iterator cit =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.begin();
|
2009-11-10 01:18:43 +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-10 01:18:43 +00:00
|
|
|
int const cv = (*cit)->to;
|
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-10 01:18:43 +00:00
|
|
|
// In effect, the way this works is that we construct a sub-graph
|
|
|
|
// by starting at "from" and following the arrows outward. Instead
|
|
|
|
// of actually constructing a sub-graph, though, we "mark" the
|
|
|
|
// arrows we traverse as we go. Once we hit "to", we abort the
|
|
|
|
// marking process and then call getMarkedPath() to reconstruct
|
|
|
|
// the marked path.
|
2003-02-28 09:49:49 +00:00
|
|
|
bool found = false;
|
2009-11-10 01:18:43 +00:00
|
|
|
clearMarks();
|
2003-02-28 09:49:49 +00:00
|
|
|
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();
|
|
|
|
|
2010-04-18 21:19:08 +00:00
|
|
|
vector<Arrow *>::const_iterator cit =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.begin();
|
2009-11-10 01:18:43 +00:00
|
|
|
vector<Arrow *>::const_iterator end =
|
2009-11-09 03:02:11 +00:00
|
|
|
vertices_[current].out_arrows.end();
|
|
|
|
for (; cit != end; ++cit) {
|
2009-11-10 01:18:43 +00:00
|
|
|
int const cv = (*cit)->to;
|
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-10 01:18:43 +00:00
|
|
|
(*cit)->marked = true;
|
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-10 01:18:43 +00:00
|
|
|
getMarkedPath(from, to, path);
|
2003-02-28 09:49:49 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2009-11-10 01:18:43 +00:00
|
|
|
|
|
|
|
// We assume we have marked the graph, as in getPath(). We also
|
|
|
|
// assume that we have done so in such a way as to guarantee a
|
|
|
|
// marked path from "from" to "to".
|
|
|
|
// We then start at "to" and find the arrow leading to it that
|
|
|
|
// has been marked. We add that to the path we are constructing,
|
|
|
|
// step back on that arrow, and continue the process (i.e., recurse).
|
|
|
|
void Graph::getMarkedPath(int from, int to, EdgePath & path) {
|
|
|
|
if (from == to) {
|
|
|
|
reverse(path.begin(), path.end());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// find marked in_arrow
|
|
|
|
vector<Arrow *>::const_iterator it = vertices_[to].in_arrows.begin();
|
2010-04-18 23:05:12 +00:00
|
|
|
vector<Arrow *>::const_iterator const en = vertices_[to].in_arrows.end();
|
2009-11-10 01:18:43 +00:00
|
|
|
for (; it != en; ++it)
|
|
|
|
if ((*it)->marked)
|
|
|
|
break;
|
|
|
|
if (it == en) {
|
2010-04-18 23:05:12 +00:00
|
|
|
// debug code to try to figure out what's up.
|
|
|
|
LYXERR0("Failed to find marked arrow.\n"
|
|
|
|
"From: " << from << ", To: " << to);
|
|
|
|
dumpGraph();
|
2009-11-10 01:18:43 +00:00
|
|
|
LASSERT(false, /* */);
|
2010-05-31 20:20:57 +00:00
|
|
|
path.clear();
|
2009-11-10 01:18:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-11-11 23:24:39 +00:00
|
|
|
path.push_back((*it)->id);
|
|
|
|
getMarkedPath(from, (*it)->from, path);
|
2009-11-10 01:18:43 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2009-11-10 01:18:43 +00:00
|
|
|
arrows_.clear();
|
2003-02-28 09:49:49 +00:00
|
|
|
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-10 01:18:43 +00:00
|
|
|
arrows_.push_back(Arrow(from, to, numedges_));
|
|
|
|
numedges_++;
|
|
|
|
Arrow * ar = &(arrows_.back());
|
|
|
|
vertices_[to].in_arrows.push_back(ar);
|
|
|
|
vertices_[from].out_arrows.push_back(ar);
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-09 00:55:37 +00:00
|
|
|
|
2010-04-18 23:05:12 +00:00
|
|
|
void Graph::dumpGraph() const
|
|
|
|
{
|
|
|
|
vector<Vertex>::const_iterator it = vertices_.begin();
|
|
|
|
vector<Vertex>::const_iterator en = vertices_.end();
|
|
|
|
for (; it != en; ++it) {
|
|
|
|
LYXERR0("Next vertex...");
|
|
|
|
LYXERR0("In arrows...");
|
|
|
|
std::vector<Arrow *>::const_iterator iit = it->in_arrows.begin();
|
|
|
|
std::vector<Arrow *>::const_iterator ien = it->in_arrows.end();
|
|
|
|
for (; iit != ien; ++iit)
|
|
|
|
LYXERR0("From " << (*iit)->from << " to " << (*iit)->to
|
|
|
|
<< ". Marked: " << (*iit)->marked);
|
|
|
|
LYXERR0("Out arrows...");
|
|
|
|
iit = it->out_arrows.begin();
|
|
|
|
ien = it->out_arrows.end();
|
|
|
|
for (; iit != ien; ++iit)
|
|
|
|
LYXERR0("From " << (*iit)->from << " to " << (*iit)->to
|
|
|
|
<< ". Marked: " << (*iit)->marked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|