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 {
|
|
|
|
|
|
|
|
|
2011-01-19 14:09:44 +00:00
|
|
|
bool Graph::bfs_init(int s, bool clear_visited, queue<int> & Q)
|
2003-02-28 09:49:49 +00:00
|
|
|
{
|
2011-01-19 14:09:44 +00:00
|
|
|
if (s < 0)
|
2009-11-09 02:19:02 +00:00
|
|
|
return false;
|
2017-07-03 17:53:14 +00:00
|
|
|
|
2011-01-19 14:09:44 +00:00
|
|
|
if (!Q.empty())
|
|
|
|
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) {
|
2011-01-19 14:09:44 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-19 14:29:46 +00:00
|
|
|
Graph::EdgePath const
|
2009-11-09 03:02:11 +00:00
|
|
|
Graph::getReachableTo(int target, bool clear_visited)
|
2003-02-28 09:49:49 +00:00
|
|
|
{
|
2011-01-19 14:29:46 +00:00
|
|
|
EdgePath result;
|
2011-01-19 09:03:41 +00:00
|
|
|
queue<int> Q;
|
2011-01-19 14:09:44 +00:00
|
|
|
if (!bfs_init(target, clear_visited, Q))
|
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.
|
2011-01-19 09:03:41 +00:00
|
|
|
// 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.
|
2011-01-19 09:03:41 +00:00
|
|
|
while (!Q.empty()) {
|
|
|
|
int const current = Q.front();
|
|
|
|
Q.pop();
|
2017-03-14 03:33:40 +00:00
|
|
|
if (current != target || theFormats().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;
|
2011-01-19 09:03:41 +00:00
|
|
|
Q.push(cv);
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-19 14:29:46 +00:00
|
|
|
Graph::EdgePath const
|
2009-11-09 03:02:11 +00:00
|
|
|
Graph::getReachable(int from, bool only_viewable,
|
2011-03-31 13:03:29 +00:00
|
|
|
bool clear_visited, set<int> excludes)
|
2003-02-28 09:49:49 +00:00
|
|
|
{
|
2011-01-19 14:29:46 +00:00
|
|
|
EdgePath result;
|
2011-01-19 09:03:41 +00:00
|
|
|
queue<int> Q;
|
2011-01-19 14:09:44 +00:00
|
|
|
if (!bfs_init(from, clear_visited, Q))
|
2003-02-28 09:49:49 +00:00
|
|
|
return result;
|
|
|
|
|
2011-01-19 09:03:41 +00:00
|
|
|
while (!Q.empty()) {
|
|
|
|
int const current = Q.front();
|
|
|
|
Q.pop();
|
2017-03-14 03:33:40 +00:00
|
|
|
Format const & format = theFormats().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 =
|
2017-03-14 03:33:40 +00:00
|
|
|
theFormats().getFormat(format.parentFormat());
|
2006-07-31 17:43:59 +00:00
|
|
|
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;
|
2011-03-31 13:03:29 +00:00
|
|
|
if (excludes.find(cv) == excludes.end())
|
2011-03-30 22:24:30 +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;
|
|
|
|
|
2011-01-19 09:03:41 +00:00
|
|
|
queue<int> Q;
|
2011-01-19 14:09:44 +00:00
|
|
|
if (to < 0 || !bfs_init(from, true, Q))
|
2003-02-28 09:49:49 +00:00
|
|
|
return false;
|
|
|
|
|
2011-01-19 09:03:41 +00:00
|
|
|
while (!Q.empty()) {
|
|
|
|
int const current = Q.front();
|
|
|
|
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;
|
2011-01-19 09:03:41 +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
|
|
|
{
|
2009-11-09 00:55:37 +00:00
|
|
|
if (from == to)
|
2011-01-16 09:53:56 +00:00
|
|
|
return EdgePath();
|
2003-02-28 09:49:49 +00:00
|
|
|
|
2011-01-19 09:03:41 +00:00
|
|
|
queue<int> Q;
|
2011-01-19 14:09:44 +00:00
|
|
|
if (to < 0 || !bfs_init(from, true, Q))
|
2011-01-16 09:53:56 +00:00
|
|
|
return EdgePath();
|
2003-02-28 09:49:49 +00:00
|
|
|
|
2011-01-19 09:03:41 +00:00
|
|
|
vector<EdgePath> pathes;
|
|
|
|
pathes.resize(vertices_.size());
|
|
|
|
while (!Q.empty()) {
|
|
|
|
int const current = Q.front();
|
|
|
|
Q.pop();
|
2003-02-28 09:49:49 +00:00
|
|
|
|
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;
|
2011-01-19 09:03:41 +00:00
|
|
|
Q.push(cv);
|
2010-05-31 21:27:17 +00:00
|
|
|
// NOTE If we wanted to collect all the paths, then
|
|
|
|
// we just need to collect them here and not worry
|
|
|
|
// about "visited".
|
2011-01-19 09:03:41 +00:00
|
|
|
EdgePath lastpath = pathes[(*cit)->from];
|
2010-05-31 21:27:17 +00:00
|
|
|
lastpath.push_back((*cit)->id);
|
2011-01-19 09:03:41 +00:00
|
|
|
pathes[cv] = lastpath;
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
2009-11-09 03:53:20 +00:00
|
|
|
if (cv == to) {
|
2011-01-19 09:03:41 +00:00
|
|
|
return pathes[cv];
|
2009-11-09 03:53:20 +00:00
|
|
|
}
|
2009-11-09 03:02:11 +00:00
|
|
|
}
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
2010-05-31 21:27:17 +00:00
|
|
|
// failure
|
2011-01-16 09:53:56 +00:00
|
|
|
return EdgePath();
|
2003-02-28 09:49:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-10 01:18:43 +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-05-31 21:27:17 +00:00
|
|
|
// At present, we do not need this debugging code, but
|
|
|
|
// I am going to leave it here in case we need it again.
|
|
|
|
#if 0
|
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)
|
2010-05-31 21:27:17 +00:00
|
|
|
LYXERR0("From " << (*iit)->from << " to " << (*iit)->to);
|
2010-04-18 23:05:12 +00:00
|
|
|
LYXERR0("Out arrows...");
|
|
|
|
iit = it->out_arrows.begin();
|
|
|
|
ien = it->out_arrows.end();
|
|
|
|
for (; iit != ien; ++iit)
|
2010-05-31 21:27:17 +00:00
|
|
|
LYXERR0("From " << (*iit)->from << " to " << (*iit)->to);
|
2010-04-18 23:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-31 21:27:17 +00:00
|
|
|
#endif
|
2010-04-18 23:05:12 +00:00
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|