Class: CloneKit::Graph
Instance Method Summary collapse
- #add_vertex(vertex, *neighbors) ⇒ Object
- #include?(vertex) ⇒ Boolean
-
#initialize ⇒ Graph
constructor
A new instance of Graph.
- #nodes ⇒ Object
- #tsort_each_child(node, &block) ⇒ Object
- #tsort_each_node(&block) ⇒ Object
Constructor Details
#initialize ⇒ Graph
Returns a new instance of Graph.
9 10 11 |
# File 'lib/clone_kit/graph.rb', line 9 def initialize @vertices = {} end |
Instance Method Details
#add_vertex(vertex, *neighbors) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/clone_kit/graph.rb', line 32 def add_vertex(vertex, *neighbors) existing = @vertices[vertex] @vertices[vertex.to_s] = if existing.nil? Array(neighbors).uniq else (@vertices[vertex.to_s] + Array(neighbors)).uniq end neighbors.each { |n| add_vertex(n) } end |
#include?(vertex) ⇒ Boolean
18 19 20 |
# File 'lib/clone_kit/graph.rb', line 18 def include?(vertex) @vertices.key?(vertex) end |
#nodes ⇒ Object
13 14 15 16 |
# File 'lib/clone_kit/graph.rb', line 13 def nodes tsort @vertices end |
#tsort_each_child(node, &block) ⇒ Object
28 29 30 |
# File 'lib/clone_kit/graph.rb', line 28 def tsort_each_child(node, &block) @vertices[node]&.each(&block) end |
#tsort_each_node(&block) ⇒ Object
24 25 26 |
# File 'lib/clone_kit/graph.rb', line 24 def tsort_each_node(&block) @vertices.each_key(&block) end |