Class: ASTDistance::Forest
- Inherits:
-
Object
- Object
- ASTDistance::Forest
- Defined in:
- lib/ast_distance/forest.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#node_index_tbl ⇒ Object
readonly
Returns the value of attribute node_index_tbl.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Class Method Summary collapse
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(left:, right:, node_index_tbl: {}) ⇒ Forest
constructor
A new instance of Forest.
- #node_count ⇒ Object
- #rightmost_forest ⇒ Object
- #root_index ⇒ Object
- #root_node ⇒ Object
Constructor Details
#initialize(left:, right:, node_index_tbl: {}) ⇒ Forest
Returns a new instance of Forest.
38 39 40 41 42 43 |
# File 'lib/ast_distance/forest.rb', line 38 def initialize(left:, right:, node_index_tbl: {}) @left = left @right = right @node_index_tbl = node_index_tbl @id = "#{left}:#{right}" end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
36 37 38 |
# File 'lib/ast_distance/forest.rb', line 36 def id @id end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
36 37 38 |
# File 'lib/ast_distance/forest.rb', line 36 def left @left end |
#node_index_tbl ⇒ Object (readonly)
Returns the value of attribute node_index_tbl.
36 37 38 |
# File 'lib/ast_distance/forest.rb', line 36 def node_index_tbl @node_index_tbl end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
36 37 38 |
# File 'lib/ast_distance/forest.rb', line 36 def right @right end |
Class Method Details
.ast_to_forest(ast:) ⇒ Object
3 4 5 6 7 |
# File 'lib/ast_distance/forest.rb', line 3 def self.ast_to_forest(ast:) node_count, node_index_tbl = build_node_index_tbl(parent: ast) new(left: 0, right: node_count, node_index_tbl: node_index_tbl) end |
Instance Method Details
#empty? ⇒ Boolean
67 68 69 |
# File 'lib/ast_distance/forest.rb', line 67 def empty? left == right end |
#node_count ⇒ Object
59 60 61 |
# File 'lib/ast_distance/forest.rb', line 59 def node_count right - left end |
#rightmost_forest ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/ast_distance/forest.rb', line 45 def rightmost_forest v = root_index - 1 while v > 0 && !node_index_tbl[v].root_node? v -= 1 end Forest.new(left: v + 1, right: root_index, node_index_tbl: node_index_tbl) end |
#root_index ⇒ Object
63 64 65 |
# File 'lib/ast_distance/forest.rb', line 63 def root_index right - 1 end |
#root_node ⇒ Object
55 56 57 |
# File 'lib/ast_distance/forest.rb', line 55 def root_node node_index_tbl[root_index] end |