Class: ASTDistance::Forest

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#idObject (readonly)

Returns the value of attribute id.



36
37
38
# File 'lib/ast_distance/forest.rb', line 36

def id
  @id
end

#leftObject (readonly)

Returns the value of attribute left.



36
37
38
# File 'lib/ast_distance/forest.rb', line 36

def left
  @left
end

#node_index_tblObject (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

#rightObject (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

Returns:

  • (Boolean)


67
68
69
# File 'lib/ast_distance/forest.rb', line 67

def empty?
  left == right
end

#node_countObject



59
60
61
# File 'lib/ast_distance/forest.rb', line 59

def node_count
  right - left
end

#rightmost_forestObject



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_indexObject



63
64
65
# File 'lib/ast_distance/forest.rb', line 63

def root_index
  right - 1
end

#root_nodeObject



55
56
57
# File 'lib/ast_distance/forest.rb', line 55

def root_node
  node_index_tbl[root_index]
end