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.
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.
11 //! A graph module for use in dataflow, region resolution, and elsewhere.
13 //! # Interface details
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.
21 //! # Implementation details
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`).
33 #![allow(dead_code)] // still WIP
35 use std::fmt::{Formatter, Error, Show};
38 pub struct Graph<N,E> {
44 first_edge: [EdgeIndex, ..2], // see module comment
49 next_edge: [EdgeIndex, ..2], // see module comment
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)
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);
68 impl Copy for NodeIndex {}
70 #[deriving(PartialEq, Show)]
71 pub struct EdgeIndex(pub uint);
72 #[allow(non_upper_case_globals)]
73 pub const InvalidEdgeIndex: EdgeIndex = EdgeIndex(uint::MAX);
75 impl Copy for EdgeIndex {}
77 // Use a private field here to guarantee no more instances are created:
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 };
85 impl Copy for Direction {}
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() }
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() }
99 impl<N,E> Graph<N,E> {
100 pub fn new() -> Graph<N,E> {
107 pub fn with_capacity(num_nodes: uint,
108 num_edges: uint) -> Graph<N,E> {
110 nodes: Vec::with_capacity(num_nodes),
111 edges: Vec::with_capacity(num_edges),
115 ///////////////////////////////////////////////////////////////////////////
119 pub fn all_nodes<'a>(&'a self) -> &'a [Node<N>] {
120 let nodes: &'a [Node<N>] = self.nodes.as_slice();
125 pub fn all_edges<'a>(&'a self) -> &'a [Edge<E>] {
126 let edges: &'a [Edge<E>] = self.edges.as_slice();
130 ///////////////////////////////////////////////////////////////////////////
133 pub fn next_node_index(&self) -> NodeIndex {
134 NodeIndex(self.nodes.len())
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],
146 pub fn mut_node_data<'a>(&'a mut self, idx: NodeIndex) -> &'a mut N {
147 &mut self.nodes[idx.get()].data
150 pub fn node_data<'a>(&'a self, idx: NodeIndex) -> &'a N {
151 &self.nodes[idx.get()].data
154 pub fn node<'a>(&'a self, idx: NodeIndex) -> &'a Node<N> {
155 &self.nodes[idx.get()]
158 ///////////////////////////////////////////////////////////////////////////
159 // Edge construction and queries
161 pub fn next_edge_index(&self) -> EdgeIndex {
162 EdgeIndex(self.edges.len())
165 pub fn add_edge(&mut self,
168 data: E) -> EdgeIndex {
169 let idx = self.next_edge_index();
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];
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],
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;
193 pub fn mut_edge_data<'a>(&'a mut self, idx: EdgeIndex) -> &'a mut E {
194 &mut self.edges[idx.get()].data
197 pub fn edge_data<'a>(&'a self, idx: EdgeIndex) -> &'a E {
198 &self.edges[idx.get()].data
201 pub fn edge<'a>(&'a self, idx: EdgeIndex) -> &'a Edge<E> {
202 &self.edges[idx.get()]
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.
210 self.nodes[node.get()].first_edge[dir.repr]
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.
218 self.edges[edge.get()].next_edge[dir.repr]
221 ///////////////////////////////////////////////////////////////////////////
222 // Iterating over nodes, edges
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))
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))
234 pub fn each_outgoing_edge<'a>(&'a self,
236 f: |EdgeIndex, &'a Edge<E>| -> bool)
238 //! Iterates over all outgoing edges from the node `from`
240 self.each_adjacent_edge(source, Outgoing, f)
243 pub fn each_incoming_edge<'a>(&'a self,
245 f: |EdgeIndex, &'a Edge<E>| -> bool)
247 //! Iterates over all incoming edges to the node `target`
249 self.each_adjacent_edge(target, Incoming, f)
252 pub fn each_adjacent_edge<'a>(&'a self,
255 f: |EdgeIndex, &'a Edge<E>| -> bool)
257 //! Iterates over all edges adjacent to the node `node`
258 //! in the direction `dir` (either `Outgoing` or `Incoming)
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) {
266 edge_idx = edge.next_edge[dir.repr];
271 ///////////////////////////////////////////////////////////////////////////
272 // Fixed-point iteration
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
280 pub fn iterate_until_fixed_point<'a>(&'a self,
281 op: |iter_index: uint,
282 edge_index: EdgeIndex,
285 let mut iteration = 0;
286 let mut changed = true;
290 for (i, edge) in self.edges.iter().enumerate() {
291 changed |= op(iteration, EdgeIndex(i), edge);
297 pub fn each_edge_index(max_edge_index: EdgeIndex, f: |EdgeIndex| -> bool) {
299 let n = max_edge_index.get();
301 if !f(EdgeIndex(i)) {
309 pub fn source(&self) -> NodeIndex {
313 pub fn target(&self) -> NodeIndex {
320 use middle::graph::*;
323 type TestNode = Node<&'static str>;
324 type TestEdge = Edge<&'static str>;
325 type TestGraph = Graph<&'static str, &'static str>;
327 fn create_graph() -> TestGraph {
328 let mut graph = Graph::new();
330 // Create a simple graph
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");
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");
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);
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);
376 fn test_adjacent_edges<N:PartialEq+Show,E:PartialEq+Show>(graph: &Graph<N,E>,
377 start_index: NodeIndex,
379 expected_incoming: &[(E,N)],
380 expected_outgoing: &[(E,N)]) {
381 assert!(graph.node_data(start_index) == &start_data);
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] {
391 assert!(e == &edge.data);
392 assert!(n == graph.node_data(edge.source));
393 assert!(start_index == edge.target);
399 assert_eq!(counter, expected_incoming.len());
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] {
409 assert!(e == &edge.data);
410 assert!(start_index == edge.source);
411 assert!(n == graph.node_data(edge.target));
417 assert_eq!(counter, expected_outgoing.len());
421 fn each_adjacent_from_a() {
422 let graph = create_graph();
423 test_adjacent_edges(&graph, NodeIndex(0), "A",
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"),]);
437 fn each_adjacent_from_c() {
438 let graph = create_graph();
439 test_adjacent_edges(&graph, NodeIndex(2), "C",
440 &[("EC", "E"), ("BC", "B")],
445 fn each_adjacent_from_d() {
446 let graph = create_graph();
447 test_adjacent_edges(&graph, NodeIndex(3), "D",