Class: SorobanRustBackend::LCPBT_Forrest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLCPBT_Forrest

Returns a new instance of LCPBT_Forrest.



5
6
7
# File 'lib/lcpbt_forrest.rb', line 5

def initialize
  @trees = []
end

Instance Attribute Details

#treesObject

Returns the value of attribute trees.



3
4
5
# File 'lib/lcpbt_forrest.rb', line 3

def trees
  @trees
end

Instance Method Details

#add_tree(tree) ⇒ Object



9
10
11
# File 'lib/lcpbt_forrest.rb', line 9

def add_tree(tree)
  @trees << tree
end

#all_paths_to(instruction_id) ⇒ Object



52
53
54
# File 'lib/lcpbt_forrest.rb', line 52

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

#code_generator_traverse(&block) ⇒ Object



43
44
45
46
# File 'lib/lcpbt_forrest.rb', line 43

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

#sizeObject



48
49
50
# File 'lib/lcpbt_forrest.rb', line 48

def size
  @trees.size
end

#traverseObject



13
14
15
# File 'lib/lcpbt_forrest.rb', line 13

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lcpbt_forrest.rb', line 23

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



17
18
19
# File 'lib/lcpbt_forrest.rb', line 17

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