Class: SorobanRustBackend::LCPBT_Forrest
- Inherits:
-
Object
- Object
- SorobanRustBackend::LCPBT_Forrest
- Defined in:
- lib/lcpbt_forrest.rb
Instance Attribute Summary collapse
-
#trees ⇒ Object
Returns the value of attribute trees.
Instance Method Summary collapse
- #add_tree(tree) ⇒ Object
- #all_paths_to(instruction_id) ⇒ Object
- #code_generator_traverse(&block) ⇒ Object
-
#initialize ⇒ LCPBT_Forrest
constructor
A new instance of LCPBT_Forrest.
- #size ⇒ Object
- #traverse ⇒ Object
-
#traverse_to_ids ⇒ Object
This represents a traversal through a unique path, covering all trees in the forest only once.
- #traverse_with_indentation ⇒ Object
Constructor Details
#initialize ⇒ LCPBT_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
#trees ⇒ Object
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 |
#size ⇒ Object
48 49 50 |
# File 'lib/lcpbt_forrest.rb', line 48 def size @trees.size end |
#traverse ⇒ Object
13 14 15 |
# File 'lib/lcpbt_forrest.rb', line 13 def traverse @trees.map(&:traverse) end |
#traverse_to_ids ⇒ Object
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_indentation ⇒ Object
17 18 19 |
# File 'lib/lcpbt_forrest.rb', line 17 def traverse_with_indentation @trees.compact.map(&:traverse_with_indentation) end |