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