Class: Courgette::Graph
- Inherits:
-
Object
- Object
- Courgette::Graph
- Defined in:
- lib/courgette/graph.rb
Instance Attribute Summary collapse
-
#adjacency_list ⇒ Object
Returns the value of attribute adjacency_list.
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #dependency_count(node) ⇒ Object
- #depender_count(node) ⇒ Object
- #filter(roots) ⇒ Object
-
#initialize(nodes, edges) ⇒ Graph
constructor
A new instance of Graph.
Constructor Details
#initialize(nodes, edges) ⇒ Graph
Returns a new instance of Graph.
7 8 9 10 11 12 |
# File 'lib/courgette/graph.rb', line 7 def initialize nodes, edges @nodes = nodes @edges = edges setup_graph end |
Instance Attribute Details
#adjacency_list ⇒ Object
Returns the value of attribute adjacency_list.
36 37 38 |
# File 'lib/courgette/graph.rb', line 36 def adjacency_list @adjacency_list end |
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
5 6 7 |
# File 'lib/courgette/graph.rb', line 5 def edges @edges end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
5 6 7 |
# File 'lib/courgette/graph.rb', line 5 def nodes @nodes end |
Instance Method Details
#dependency_count(node) ⇒ Object
14 15 16 |
# File 'lib/courgette/graph.rb', line 14 def dependency_count node edges.select { |r| r.referrer == node }.count end |
#depender_count(node) ⇒ Object
18 19 20 |
# File 'lib/courgette/graph.rb', line 18 def depender_count node edges.select { |r| r.reference == node }.count end |
#filter(roots) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/courgette/graph.rb', line 22 def filter roots return self if roots.nil? visited = Set.new roots.each do |r| dfs(r, visited) end filtered_edges = edges.select { |r| visited.include? r.referrer } Graph.new visited.to_a, filtered_edges end |