Class: DTRCore::Graph::LCPBT_Forrest

Inherits:
Object
  • Object
show all
Defined in:
lib/dtr_core/graph/lcpbt_forrest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLCPBT_Forrest

Returns a new instance of LCPBT_Forrest.



8
9
10
# File 'lib/dtr_core/graph/lcpbt_forrest.rb', line 8

def initialize
  @trees = []
end

Instance Attribute Details

#treesObject

Returns the value of attribute trees.



6
7
8
# File 'lib/dtr_core/graph/lcpbt_forrest.rb', line 6

def trees
  @trees
end

Instance Method Details

#add_tree(tree) ⇒ Object



12
13
14
# File 'lib/dtr_core/graph/lcpbt_forrest.rb', line 12

def add_tree(tree)
  @trees << tree
end

#all_paths_to(instruction_id) ⇒ Object



55
56
57
# File 'lib/dtr_core/graph/lcpbt_forrest.rb', line 55

def all_paths_to(instruction_id)
  @trees.map { |x| x.all_paths_to(instruction_id) }.flatten(1).compact
end

#code_generator_traverse(&block) ⇒ Object



46
47
48
49
# File 'lib/dtr_core/graph/lcpbt_forrest.rb', line 46

def code_generator_traverse(&block)
  traverse_with_indentation
    .map { |tree| tree.reverse.uniq.reverse.map(&block) }
end

#sizeObject



51
52
53
# File 'lib/dtr_core/graph/lcpbt_forrest.rb', line 51

def size
  @trees.size
end

#traverseObject



16
17
18
# File 'lib/dtr_core/graph/lcpbt_forrest.rb', line 16

def traverse
  @trees.map(&:traverse)
end

#traverse_to_idsObject

This represents a traversal through a unique path, covering all trees in the forest only once. This is useful for code generation.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dtr_core/graph/lcpbt_forrest.rb', line 26

def traverse_to_ids
  id_map = {}
  result = []

  traverse
    .map { |x| x.map(&:id) }
    .each do |x|
      sub_result = []
      x.each do |y|
        next if id_map[y]

        sub_result << y
        id_map[y] = true
      end
      result << sub_result
    end

  result
end

#traverse_with_indentationObject



20
21
22
# File 'lib/dtr_core/graph/lcpbt_forrest.rb', line 20

def traverse_with_indentation
  @trees.map(&:traverse_with_indentation)
end