Class: Nanoc3::DirectedGraph
- Inherits:
-
Object
- Object
- Nanoc3::DirectedGraph
- Defined in:
- lib/nanoc3/base/directed_graph.rb
Overview
Represents a directed graph. It is used by the dependency tracker for storing and querying dependencies between items.
Instance Attribute Summary (collapse)
-
- (Array) vertices
readonly
The list of vertices in this graph.
Instance Method Summary (collapse)
-
- (void) add_edge(from, to)
Adds an edge from the first vertex to the second vertex.
-
- (void) delete_edges_to(to)
Deletes all edges going to the given vertex.
-
- (Array) direct_predecessors_of(to)
Returns the direct predecessors of the given vertex, i.e.
-
- (Array) direct_successors_of(from)
Returns the direct successors of the given vertex, i.e.
-
- (Array) edges
Returns an array of tuples representing the edges.
-
- (DirectedGraph) initialize(vertices)
constructor
Creates a new directed graph with the given vertices.
-
- (Array) predecessors_of(to)
Returns the predecessors of the given vertex, i.e.
-
- (void) remove_edge(from, to)
Removes the edge from the first vertex to the second vertex.
-
- (Array) successors_of(from)
Returns the successors of the given vertex, i.e.
Constructor Details
- (DirectedGraph) initialize(vertices)
Creates a new directed graph with the given vertices.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/nanoc3/base/directed_graph.rb', line 42 def initialize(vertices) @vertices = vertices @from_graph = {} @to_graph = {} @vertice_indexes = {} vertices.each_with_index do |v, i| @vertice_indexes[v] = i end invalidate_caches end |
Instance Attribute Details
- (Array) vertices (readonly)
The list of vertices in this graph.
39 40 41 |
# File 'lib/nanoc3/base/directed_graph.rb', line 39 def vertices @vertices end |
Instance Method Details
- (void) add_edge(from, to)
This method returns an undefined value.
Adds an edge from the first vertex to the second vertex.
62 63 64 65 66 67 68 69 70 |
# File 'lib/nanoc3/base/directed_graph.rb', line 62 def add_edge(from, to) @from_graph[from] ||= Set.new @from_graph[from] << to @to_graph[to] ||= Set.new @to_graph[to] << from invalidate_caches end |
- (void) delete_edges_to(to)
This method returns an undefined value.
Deletes all edges going to the given vertex.
94 95 96 97 98 99 100 |
# File 'lib/nanoc3/base/directed_graph.rb', line 94 def delete_edges_to(to) @to_graph[to] ||= Set.new @to_graph[to].each do |from| @from_graph[from].delete(to) end @to_graph.delete(to) end |
- (Array) direct_predecessors_of(to)
Returns the direct predecessors of the given vertex, i.e. the vertices x where there is an edge from x to the given vertex y.
108 109 110 |
# File 'lib/nanoc3/base/directed_graph.rb', line 108 def direct_predecessors_of(to) @to_graph[to].to_a end |
- (Array) direct_successors_of(from)
Returns the direct successors of the given vertex, i.e. the vertices y where there is an edge from the given vertex x to y.
118 119 120 |
# File 'lib/nanoc3/base/directed_graph.rb', line 118 def direct_successors_of(from) @from_graph[from].to_a end |
- (Array) edges
Returns an array of tuples representing the edges. The result of this method may take a while to compute and should be cached if possible.
146 147 148 149 150 151 152 153 154 |
# File 'lib/nanoc3/base/directed_graph.rb', line 146 def edges result = [] @vertices.each_with_index do |v, i| direct_successors_of(v).map { |v2| @vertice_indexes[v2] }.each do |i2| result << [ i, i2 ] end end result end |
- (Array) predecessors_of(to)
Returns the predecessors of the given vertex, i.e. the vertices x for which there is a path from x to the given vertex y.
128 129 130 |
# File 'lib/nanoc3/base/directed_graph.rb', line 128 def predecessors_of(to) @predecessors[to] ||= recursively_find_vertices(to, :direct_predecessors_of) end |
- (void) remove_edge(from, to)
This method returns an undefined value.
Removes the edge from the first vertex to the second vertex. If the edge does not exist, nothing is done.
79 80 81 82 83 84 85 86 87 |
# File 'lib/nanoc3/base/directed_graph.rb', line 79 def remove_edge(from, to) @from_graph[from] ||= Set.new @from_graph[from].delete(to) @to_graph[to] ||= Set.new @to_graph[to].delete(from) invalidate_caches end |
- (Array) successors_of(from)
Returns the successors of the given vertex, i.e. the vertices y for which there is a path from the given vertex x to y.
138 139 140 |
# File 'lib/nanoc3/base/directed_graph.rb', line 138 def successors_of(from) @successors[from] ||= recursively_find_vertices(from, :direct_successors_of) end |