2003-02-28 09:49:49 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file Graph.h
|
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-08-23 00:17:00 +00:00
|
|
|
#ifndef GRAPH_H
|
|
|
|
#define GRAPH_H
|
|
|
|
|
2009-11-10 01:18:43 +00:00
|
|
|
#include <list>
|
2003-02-28 09:49:49 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <vector>
|
|
|
|
|
2003-09-07 21:25:37 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
2009-11-09 02:19:02 +00:00
|
|
|
/// Represents a directed graph, possibly with multiple edges
|
|
|
|
/// connecting the vertices.
|
2003-02-28 09:49:49 +00:00
|
|
|
class Graph {
|
2003-03-04 09:27:27 +00:00
|
|
|
public:
|
2009-11-09 02:19:02 +00:00
|
|
|
Graph() : numedges_(0) {}
|
2003-02-28 09:49:49 +00:00
|
|
|
///
|
|
|
|
typedef std::vector<int> EdgePath;
|
2009-11-09 00:55:37 +00:00
|
|
|
/// \return a vector of the vertices from which "to" can be reached
|
|
|
|
std::vector<int> const getReachableTo(int to, bool clear_visited);
|
|
|
|
/// \return a vector of the vertices that can be reached from "from"
|
2003-02-28 09:49:49 +00:00
|
|
|
std::vector<int> const
|
2009-11-09 00:55:37 +00:00
|
|
|
getReachable(int from, bool only_viewable, bool clear_visited);
|
2009-11-10 01:18:43 +00:00
|
|
|
/// can "from" be reached from "to"?
|
2009-11-09 00:55:37 +00:00
|
|
|
bool isReachable(int from, int to);
|
2009-11-10 01:18:43 +00:00
|
|
|
/// find a path from "from" to "to". always returns one of the
|
|
|
|
/// shortest such paths.
|
2009-11-09 00:55:37 +00:00
|
|
|
EdgePath const getPath(int from, int to);
|
2009-11-10 01:18:43 +00:00
|
|
|
/// called repeatedly to build the graph
|
2009-11-09 00:55:37 +00:00
|
|
|
void addEdge(int from, int to);
|
2009-11-10 01:18:43 +00:00
|
|
|
/// reset the internal data structures
|
2003-02-28 09:49:49 +00:00
|
|
|
void init(int size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
///
|
2009-11-09 02:19:02 +00:00
|
|
|
bool bfs_init(int, bool clear_visited = true);
|
2010-05-31 21:27:17 +00:00
|
|
|
/// clears the paths from a previous search. should be
|
|
|
|
/// called before each new one.
|
|
|
|
void clearPaths();
|
2009-11-10 01:18:43 +00:00
|
|
|
/// used to recover a marked path
|
|
|
|
void getMarkedPath(int from, int to, EdgePath & path);
|
|
|
|
/// these represent the arrows connecting the nodes of the graph.
|
|
|
|
/// this is the basic representation of the graph: as a bunch of
|
|
|
|
/// arrows.
|
2009-11-09 17:56:34 +00:00
|
|
|
struct Arrow {
|
|
|
|
///
|
2009-11-10 01:18:43 +00:00
|
|
|
Arrow(int f, int t, int i):
|
2010-05-31 21:27:17 +00:00
|
|
|
from(f), to(t), id(i) {}
|
2009-11-10 01:18:43 +00:00
|
|
|
/// the vertex at the tail of the arrow
|
|
|
|
int from;
|
|
|
|
/// the vertex at the head
|
|
|
|
int to;
|
|
|
|
/// an id for this arrow, e.g., for use in describing paths
|
|
|
|
/// through the graph
|
|
|
|
int id;
|
2009-11-09 03:02:11 +00:00
|
|
|
};
|
2009-11-10 01:18:43 +00:00
|
|
|
/// a container for the arrows
|
|
|
|
/// we use a list because we want pointers to the arrows,
|
|
|
|
/// and a vector might invalidate them
|
|
|
|
typedef std::list<Arrow> Arrows;
|
|
|
|
Arrows arrows_;
|
|
|
|
/// Represents a vertex of the graph. Note that we could recover
|
|
|
|
/// the in_arrows and out_arrows from the Arrows, so these are in
|
|
|
|
/// effect a kind of cache.
|
2009-11-09 03:02:11 +00:00
|
|
|
struct Vertex {
|
2009-11-10 01:18:43 +00:00
|
|
|
/// arrows that point at this one
|
|
|
|
std::vector<Arrow *> in_arrows;
|
|
|
|
/// arrows out from here
|
|
|
|
std::vector<Arrow *> out_arrows;
|
|
|
|
/// used in the search routines
|
2009-11-09 04:02:45 +00:00
|
|
|
bool visited;
|
2010-05-31 21:27:17 +00:00
|
|
|
///
|
|
|
|
EdgePath path;
|
2003-02-28 09:49:49 +00:00
|
|
|
};
|
2009-11-10 01:18:43 +00:00
|
|
|
/// a container for the vertices
|
|
|
|
/// the index into the vector functions as the identifier by which
|
|
|
|
/// these are referenced in the Arrow struct
|
|
|
|
/// the code making use of the Graph must keep track of the relation
|
|
|
|
/// between these indices and the objects they represent. (in the case
|
|
|
|
/// of Format, this is easy, since the Format objects already have ints
|
|
|
|
/// as identifiers.)
|
2009-11-09 04:02:45 +00:00
|
|
|
std::vector<Vertex> vertices_;
|
2003-02-28 09:49:49 +00:00
|
|
|
///
|
|
|
|
std::queue<int> Q_;
|
2009-11-10 01:18:43 +00:00
|
|
|
/// a counter that we use to assign id's to the arrows
|
|
|
|
/// FIXME This technique assumes a correspondence between the
|
|
|
|
/// ids of the arrows and ids associated with Converters that
|
|
|
|
/// seems kind of fragile. Perhaps a better solution would be
|
|
|
|
/// to pass the ids as we create the arrows.
|
2003-02-28 09:49:49 +00:00
|
|
|
int numedges_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
} // namespace lyx
|
|
|
|
|
2003-02-28 09:49:49 +00:00
|
|
|
#endif //GRAPH_H
|