]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/graph.rs
auto merge of #19628 : jbranchaud/rust/add-string-as-string-doctest, r=steveklabnik
[rust.git] / src / librustc / middle / graph.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! A graph module for use in dataflow, region resolution, and elsewhere.
12 //!
13 //! # Interface details
14 //!
15 //! You customize the graph by specifying a "node data" type `N` and an
16 //! "edge data" type `E`. You can then later gain access (mutable or
17 //! immutable) to these "user-data" bits. Currently, you can only add
18 //! nodes or edges to the graph. You cannot remove or modify them once
19 //! added. This could be changed if we have a need.
20 //!
21 //! # Implementation details
22 //!
23 //! The main tricky thing about this code is the way that edges are
24 //! stored. The edges are stored in a central array, but they are also
25 //! threaded onto two linked lists for each node, one for incoming edges
26 //! and one for outgoing edges. Note that every edge is a member of some
27 //! incoming list and some outgoing list.  Basically you can load the
28 //! first index of the linked list from the node data structures (the
29 //! field `first_edge`) and then, for each edge, load the next index from
30 //! the field `next_edge`). Each of those fields is an array that should
31 //! be indexed by the direction (see the type `Direction`).
32
33 #![allow(dead_code)] // still WIP
34
35 use std::fmt::{Formatter, Error, Show};
36 use std::uint;
37
38 pub struct Graph<N,E> {
39     nodes: Vec<Node<N>> ,
40     edges: Vec<Edge<E>> ,
41 }
42
43 pub struct Node<N> {
44     first_edge: [EdgeIndex, ..2], // see module comment
45     pub data: N,
46 }
47
48 pub struct Edge<E> {
49     next_edge: [EdgeIndex, ..2], // see module comment
50     source: NodeIndex,
51     target: NodeIndex,
52     pub data: E,
53 }
54
55 impl<E: Show> Show for Edge<E> {
56     fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
57         write!(f, "Edge {{ next_edge: [{}, {}], source: {}, target: {}, data: {} }}",
58                self.next_edge[0], self.next_edge[1], self.source,
59                self.target, self.data)
60     }
61 }
62
63 #[deriving(Clone, PartialEq, Show)]
64 pub struct NodeIndex(pub uint);
65 #[allow(non_upper_case_globals)]
66 pub const InvalidNodeIndex: NodeIndex = NodeIndex(uint::MAX);
67
68 impl Copy for NodeIndex {}
69
70 #[deriving(PartialEq, Show)]
71 pub struct EdgeIndex(pub uint);
72 #[allow(non_upper_case_globals)]
73 pub const InvalidEdgeIndex: EdgeIndex = EdgeIndex(uint::MAX);
74
75 impl Copy for EdgeIndex {}
76
77 // Use a private field here to guarantee no more instances are created:
78 #[deriving(Show)]
79 pub struct Direction { repr: uint }
80 #[allow(non_upper_case_globals)]
81 pub const Outgoing: Direction = Direction { repr: 0 };
82 #[allow(non_upper_case_globals)]
83 pub const Incoming: Direction = Direction { repr: 1 };
84
85 impl Copy for Direction {}
86
87 impl NodeIndex {
88     fn get(&self) -> uint { let NodeIndex(v) = *self; v }
89     /// Returns unique id (unique with respect to the graph holding associated node).
90     pub fn node_id(&self) -> uint { self.get() }
91 }
92
93 impl EdgeIndex {
94     fn get(&self) -> uint { let EdgeIndex(v) = *self; v }
95     /// Returns unique id (unique with respect to the graph holding associated edge).
96     pub fn edge_id(&self) -> uint { self.get() }
97 }
98
99 impl<N,E> Graph<N,E> {
100     pub fn new() -> Graph<N,E> {
101         Graph {
102             nodes: Vec::new(),
103             edges: Vec::new(),
104         }
105     }
106
107     pub fn with_capacity(num_nodes: uint,
108                          num_edges: uint) -> Graph<N,E> {
109         Graph {
110             nodes: Vec::with_capacity(num_nodes),
111             edges: Vec::with_capacity(num_edges),
112         }
113     }
114
115     ///////////////////////////////////////////////////////////////////////////
116     // Simple accessors
117
118     #[inline]
119     pub fn all_nodes<'a>(&'a self) -> &'a [Node<N>] {
120         let nodes: &'a [Node<N>] = self.nodes.as_slice();
121         nodes
122     }
123
124     #[inline]
125     pub fn all_edges<'a>(&'a self) -> &'a [Edge<E>] {
126         let edges: &'a [Edge<E>] = self.edges.as_slice();
127         edges
128     }
129
130     ///////////////////////////////////////////////////////////////////////////
131     // Node construction
132
133     pub fn next_node_index(&self) -> NodeIndex {
134         NodeIndex(self.nodes.len())
135     }
136
137     pub fn add_node(&mut self, data: N) -> NodeIndex {
138         let idx = self.next_node_index();
139         self.nodes.push(Node {
140             first_edge: [InvalidEdgeIndex, InvalidEdgeIndex],
141             data: data
142         });
143         idx
144     }
145
146     pub fn mut_node_data<'a>(&'a mut self, idx: NodeIndex) -> &'a mut N {
147         &mut self.nodes[idx.get()].data
148     }
149
150     pub fn node_data<'a>(&'a self, idx: NodeIndex) -> &'a N {
151         &self.nodes[idx.get()].data
152     }
153
154     pub fn node<'a>(&'a self, idx: NodeIndex) -> &'a Node<N> {
155         &self.nodes[idx.get()]
156     }
157
158     ///////////////////////////////////////////////////////////////////////////
159     // Edge construction and queries
160
161     pub fn next_edge_index(&self) -> EdgeIndex {
162         EdgeIndex(self.edges.len())
163     }
164
165     pub fn add_edge(&mut self,
166                     source: NodeIndex,
167                     target: NodeIndex,
168                     data: E) -> EdgeIndex {
169         let idx = self.next_edge_index();
170
171         // read current first of the list of edges from each node
172         let source_first = self.nodes[source.get()]
173                                      .first_edge[Outgoing.repr];
174         let target_first = self.nodes[target.get()]
175                                      .first_edge[Incoming.repr];
176
177         // create the new edge, with the previous firsts from each node
178         // as the next pointers
179         self.edges.push(Edge {
180             next_edge: [source_first, target_first],
181             source: source,
182             target: target,
183             data: data
184         });
185
186         // adjust the firsts for each node target be the next object.
187         self.nodes[source.get()].first_edge[Outgoing.repr] = idx;
188         self.nodes[target.get()].first_edge[Incoming.repr] = idx;
189
190         return idx;
191     }
192
193     pub fn mut_edge_data<'a>(&'a mut self, idx: EdgeIndex) -> &'a mut E {
194         &mut self.edges[idx.get()].data
195     }
196
197     pub fn edge_data<'a>(&'a self, idx: EdgeIndex) -> &'a E {
198         &self.edges[idx.get()].data
199     }
200
201     pub fn edge<'a>(&'a self, idx: EdgeIndex) -> &'a Edge<E> {
202         &self.edges[idx.get()]
203     }
204
205     pub fn first_adjacent(&self, node: NodeIndex, dir: Direction) -> EdgeIndex {
206         //! Accesses the index of the first edge adjacent to `node`.
207         //! This is useful if you wish to modify the graph while walking
208         //! the linked list of edges.
209
210         self.nodes[node.get()].first_edge[dir.repr]
211     }
212
213     pub fn next_adjacent(&self, edge: EdgeIndex, dir: Direction) -> EdgeIndex {
214         //! Accesses the next edge in a given direction.
215         //! This is useful if you wish to modify the graph while walking
216         //! the linked list of edges.
217
218         self.edges[edge.get()].next_edge[dir.repr]
219     }
220
221     ///////////////////////////////////////////////////////////////////////////
222     // Iterating over nodes, edges
223
224     pub fn each_node<'a>(&'a self, f: |NodeIndex, &'a Node<N>| -> bool) -> bool {
225         //! Iterates over all edges defined in the graph.
226         self.nodes.iter().enumerate().all(|(i, node)| f(NodeIndex(i), node))
227     }
228
229     pub fn each_edge<'a>(&'a self, f: |EdgeIndex, &'a Edge<E>| -> bool) -> bool {
230         //! Iterates over all edges defined in the graph
231         self.edges.iter().enumerate().all(|(i, edge)| f(EdgeIndex(i), edge))
232     }
233
234     pub fn each_outgoing_edge<'a>(&'a self,
235                                   source: NodeIndex,
236                                   f: |EdgeIndex, &'a Edge<E>| -> bool)
237                                   -> bool {
238         //! Iterates over all outgoing edges from the node `from`
239
240         self.each_adjacent_edge(source, Outgoing, f)
241     }
242
243     pub fn each_incoming_edge<'a>(&'a self,
244                                   target: NodeIndex,
245                                   f: |EdgeIndex, &'a Edge<E>| -> bool)
246                                   -> bool {
247         //! Iterates over all incoming edges to the node `target`
248
249         self.each_adjacent_edge(target, Incoming, f)
250     }
251
252     pub fn each_adjacent_edge<'a>(&'a self,
253                                   node: NodeIndex,
254                                   dir: Direction,
255                                   f: |EdgeIndex, &'a Edge<E>| -> bool)
256                                   -> bool {
257         //! Iterates over all edges adjacent to the node `node`
258         //! in the direction `dir` (either `Outgoing` or `Incoming)
259
260         let mut edge_idx = self.first_adjacent(node, dir);
261         while edge_idx != InvalidEdgeIndex {
262             let edge = &self.edges[edge_idx.get()];
263             if !f(edge_idx, edge) {
264                 return false;
265             }
266             edge_idx = edge.next_edge[dir.repr];
267         }
268         return true;
269     }
270
271     ///////////////////////////////////////////////////////////////////////////
272     // Fixed-point iteration
273     //
274     // A common use for graphs in our compiler is to perform
275     // fixed-point iteration. In this case, each edge represents a
276     // constraint, and the nodes themselves are associated with
277     // variables or other bitsets. This method facilitates such a
278     // computation.
279
280     pub fn iterate_until_fixed_point<'a>(&'a self,
281                                          op: |iter_index: uint,
282                                               edge_index: EdgeIndex,
283                                               edge: &'a Edge<E>|
284                                               -> bool) {
285         let mut iteration = 0;
286         let mut changed = true;
287         while changed {
288             changed = false;
289             iteration += 1;
290             for (i, edge) in self.edges.iter().enumerate() {
291                 changed |= op(iteration, EdgeIndex(i), edge);
292             }
293         }
294     }
295 }
296
297 pub fn each_edge_index(max_edge_index: EdgeIndex, f: |EdgeIndex| -> bool) {
298     let mut i = 0;
299     let n = max_edge_index.get();
300     while i < n {
301         if !f(EdgeIndex(i)) {
302             return;
303         }
304         i += 1;
305     }
306 }
307
308 impl<E> Edge<E> {
309     pub fn source(&self) -> NodeIndex {
310         self.source
311     }
312
313     pub fn target(&self) -> NodeIndex {
314         self.target
315     }
316 }
317
318 #[cfg(test)]
319 mod test {
320     use middle::graph::*;
321     use std::fmt::Show;
322
323     type TestNode = Node<&'static str>;
324     type TestEdge = Edge<&'static str>;
325     type TestGraph = Graph<&'static str, &'static str>;
326
327     fn create_graph() -> TestGraph {
328         let mut graph = Graph::new();
329
330         // Create a simple graph
331         //
332         //    A -+> B --> C
333         //       |  |     ^
334         //       |  v     |
335         //       F  D --> E
336
337         let a = graph.add_node("A");
338         let b = graph.add_node("B");
339         let c = graph.add_node("C");
340         let d = graph.add_node("D");
341         let e = graph.add_node("E");
342         let f = graph.add_node("F");
343
344         graph.add_edge(a, b, "AB");
345         graph.add_edge(b, c, "BC");
346         graph.add_edge(b, d, "BD");
347         graph.add_edge(d, e, "DE");
348         graph.add_edge(e, c, "EC");
349         graph.add_edge(f, b, "FB");
350
351         return graph;
352     }
353
354     #[test]
355     fn each_node() {
356         let graph = create_graph();
357         let expected = ["A", "B", "C", "D", "E", "F"];
358         graph.each_node(|idx, node| {
359             assert_eq!(&expected[idx.get()], graph.node_data(idx));
360             assert_eq!(expected[idx.get()], node.data);
361             true
362         });
363     }
364
365     #[test]
366     fn each_edge() {
367         let graph = create_graph();
368         let expected = ["AB", "BC", "BD", "DE", "EC", "FB"];
369         graph.each_edge(|idx, edge| {
370             assert_eq!(&expected[idx.get()], graph.edge_data(idx));
371             assert_eq!(expected[idx.get()], edge.data);
372             true
373         });
374     }
375
376     fn test_adjacent_edges<N:PartialEq+Show,E:PartialEq+Show>(graph: &Graph<N,E>,
377                                       start_index: NodeIndex,
378                                       start_data: N,
379                                       expected_incoming: &[(E,N)],
380                                       expected_outgoing: &[(E,N)]) {
381         assert!(graph.node_data(start_index) == &start_data);
382
383         let mut counter = 0;
384         graph.each_incoming_edge(start_index, |edge_index, edge| {
385             assert!(graph.edge_data(edge_index) == &edge.data);
386             assert!(counter < expected_incoming.len());
387             debug!("counter={} expected={} edge_index={} edge={}",
388                    counter, expected_incoming[counter], edge_index, edge);
389             match expected_incoming[counter] {
390                 (ref e, ref n) => {
391                     assert!(e == &edge.data);
392                     assert!(n == graph.node_data(edge.source));
393                     assert!(start_index == edge.target);
394                 }
395             }
396             counter += 1;
397             true
398         });
399         assert_eq!(counter, expected_incoming.len());
400
401         let mut counter = 0;
402         graph.each_outgoing_edge(start_index, |edge_index, edge| {
403             assert!(graph.edge_data(edge_index) == &edge.data);
404             assert!(counter < expected_outgoing.len());
405             debug!("counter={} expected={} edge_index={} edge={}",
406                    counter, expected_outgoing[counter], edge_index, edge);
407             match expected_outgoing[counter] {
408                 (ref e, ref n) => {
409                     assert!(e == &edge.data);
410                     assert!(start_index == edge.source);
411                     assert!(n == graph.node_data(edge.target));
412                 }
413             }
414             counter += 1;
415             true
416         });
417         assert_eq!(counter, expected_outgoing.len());
418     }
419
420     #[test]
421     fn each_adjacent_from_a() {
422         let graph = create_graph();
423         test_adjacent_edges(&graph, NodeIndex(0), "A",
424                             &[],
425                             &[("AB", "B")]);
426     }
427
428     #[test]
429     fn each_adjacent_from_b() {
430         let graph = create_graph();
431         test_adjacent_edges(&graph, NodeIndex(1), "B",
432                             &[("FB", "F"), ("AB", "A"),],
433                             &[("BD", "D"), ("BC", "C"),]);
434     }
435
436     #[test]
437     fn each_adjacent_from_c() {
438         let graph = create_graph();
439         test_adjacent_edges(&graph, NodeIndex(2), "C",
440                             &[("EC", "E"), ("BC", "B")],
441                             &[]);
442     }
443
444     #[test]
445     fn each_adjacent_from_d() {
446         let graph = create_graph();
447         test_adjacent_edges(&graph, NodeIndex(3), "D",
448                             &[("BD", "B")],
449                             &[("DE", "E")]);
450     }
451 }