Class: CloneKit::Graph

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/clone_kit/graph.rb

Instance Method Summary collapse

Constructor Details

#initializeGraph

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

Returns:

  • (Boolean)


18
19
20
# File 'lib/clone_kit/graph.rb', line 18

def include?(vertex)
  @vertices.key?(vertex)
end

#nodesObject



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