Class: Ykytils::TreeManager

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/ykutils/treemanager.rb

Instance Method Summary collapse

Constructor Details

#initializeTreeManager

Returns a new instance of TreeManager.



7
8
9
# File 'lib/ykutils/treemanager.rb', line 7

def initialize
  @table = {}
end

Instance Method Details

#add(parent_name, name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ykutils/treemanager.rb', line 11

def add(parent_name, name)
  if parent_name
    parent = @table[parent_name]
    if parent
      @table[parent_name] << name
    else
      @table[parent_name] = [name]
    end
  else
    @table[name] = []
  end
end

#tsort_each_child(node, &block) ⇒ Object



24
25
26
27
# File 'lib/ykutils/treemanager.rb', line 24

def tsort_each_child(node, &block)
  ary = @table[node]
  ary&.each(&block)
end

#tsort_each_node(&block) ⇒ Object



29
30
31
# File 'lib/ykutils/treemanager.rb', line 29

def tsort_each_node(&block)
  @table.keys.each(&block)
end