Class: Funk::Graph
Instance Method Summary collapse
-
#initialize(map) ⇒ Graph
constructor
A new instance of Graph.
- #tsort_each_child(n, &b) ⇒ Object
- #tsort_each_node(&b) ⇒ Object
Constructor Details
#initialize(map) ⇒ Graph
Returns a new instance of Graph.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/funk/graph.rb', line 8 def initialize(map) fn_map = map.each_with_object({}) do |(name, impl), fn_map| fn = fn_map[name] = Fn.new(name, impl) fn.dependencies.each do |dep_name| fn_map[dep_name] ||= InputFn.new(dep_name) end end accum = Hash.new { |h,k| h[k] = [] } @nodes = fn_map.each_with_object(accum) do |(name, fn), graph| graph[fn] = [] fn.dependencies.each do |dep_name| graph[fn] << fn_map[dep_name] end end end |
Instance Method Details
#tsort_each_child(n, &b) ⇒ Object
28 29 30 |
# File 'lib/funk/graph.rb', line 28 def tsort_each_child(n, &b) @nodes[n].each(&b) end |
#tsort_each_node(&b) ⇒ Object
32 33 34 |
# File 'lib/funk/graph.rb', line 32 def tsort_each_node(&b) @nodes.each_key(&b) end |