]> git.lyx.org Git - lyx.git/blob - src/Graph.h
305cf70dcf0370ee36c7961b00966950df43c8fa
[lyx.git] / src / Graph.h
1 // -*- C++ -*-
2 /**
3  * \file Graph.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Dekel Tsur (original code)
8  * \author Richard Heck (re-implementation)
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef GRAPH_H
14 #define GRAPH_H
15
16
17 #include <list>
18 #include <queue>
19 #include <vector>
20
21
22 namespace lyx {
23
24
25 /// Represents a directed graph, possibly with multiple edges
26 /// connecting the vertices.
27 class Graph {
28 public:
29         Graph() : numedges_(0) {}
30         ///
31         typedef std::vector<int> EdgePath;
32         /// \return a vector of the vertices from which "to" can be reached
33         std::vector<int> const getReachableTo(int to, bool clear_visited);
34         /// \return a vector of the vertices that can be reached from "from"
35         std::vector<int> const
36                 getReachable(int from, bool only_viewable, bool clear_visited);
37         /// can "from" be reached from "to"?
38         bool isReachable(int from, int to);
39         /// find a path from "from" to "to". always returns one of the
40         /// shortest such paths.
41         EdgePath const getPath(int from, int to);
42         /// called repeatedly to build the graph
43         void addEdge(int from, int to);
44         /// reset the internal data structures
45         void init(int size);
46
47 private:
48         ///
49         bool bfs_init(int, bool clear_visited, std::queue<int> & Q);
50         /// used to recover a marked path 
51         void getMarkedPath(int from, int to, EdgePath & path);
52         /// these represent the arrows connecting the nodes of the graph.
53         /// this is the basic representation of the graph: as a bunch of 
54         /// arrows.
55         struct Arrow {
56                 ///
57                 Arrow(int f, int t, int i): 
58                         from(f), to(t), id(i) {}
59                 /// the vertex at the tail of the arrow
60                 int from;
61                 /// the vertex at the head
62                 int to;
63                 /// an id for this arrow, e.g., for use in describing paths 
64                 /// through the graph
65                 int id;
66         };
67         /// a container for the arrows
68         /// we use a list because we want pointers to the arrows,
69         /// and a vector might invalidate them
70         typedef std::list<Arrow> Arrows;
71         Arrows arrows_;
72         /// Represents a vertex of the graph. Note that we could recover
73         /// the in_arrows and out_arrows from the Arrows, so these are in
74         /// effect a kind of cache.
75         struct Vertex {
76                 /// arrows that point at this one
77                 std::vector<Arrow *> in_arrows;
78                 /// arrows out from here
79                 std::vector<Arrow *> out_arrows;
80                 /// used in the search routines
81                 bool visited;
82         };
83         /// a container for the vertices
84         /// the index into the vector functions as the identifier by which
85         /// these are referenced in the Arrow struct
86         /// the code making use of the Graph must keep track of the relation
87         /// between these indices and the objects they represent. (in the case
88         /// of Format, this is easy, since the Format objects already have ints
89         /// as identifiers.)
90         std::vector<Vertex> vertices_;
91         
92         /// a counter that we use to assign id's to the arrows
93         /// FIXME This technique assumes a correspondence between the
94         /// ids of the arrows and ids associated with Converters that
95         /// seems kind of fragile. Perhaps a better solution would be
96         /// to pass the ids as we create the arrows.
97         int numedges_;
98 };
99
100
101
102 } // namespace lyx
103
104 #endif //GRAPH_H