Class: Canon::TreeDiff::Core::NodeWeight

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/tree_diff/core/node_weight.rb

Overview

NodeWeight computes weights for tree nodes

Based on XyDiff/Cobena (2002, INRIA) approach:

  • Weight reflects subtree size/importance

  • Formula: 1 + Σ(child_weights)

  • Text nodes: 1 + log(text_length) for significant text

  • Used to prioritize matching (heaviest first)

Features:

  • Hierarchical: Parent weight includes all descendants

  • Text-aware: Longer text has higher weight

  • Cached: Computed once and reused

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ NodeWeight

Initialize weight for a node

Parameters:

  • node (TreeNode)

    Node to compute weight for



24
25
26
27
# File 'lib/canon/tree_diff/core/node_weight.rb', line 24

def initialize(node)
  @node = node
  @value = compute_weight
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



19
20
21
# File 'lib/canon/tree_diff/core/node_weight.rb', line 19

def value
  @value
end

Class Method Details

.for(node) ⇒ NodeWeight

Compute and cache weight for a node

Parameters:

  • node (TreeNode)

    Node to compute weight for

Returns:



33
34
35
# File 'lib/canon/tree_diff/core/node_weight.rb', line 33

def self.for(node)
  node.weight ||= new(node)
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare weights (for sorting)

Parameters:

Returns:

  • (Integer)

    -1, 0, or 1



41
42
43
44
45
# File 'lib/canon/tree_diff/core/node_weight.rb', line 41

def <=>(other)
  return nil unless other.is_a?(NodeWeight)

  value <=> other.value
end

#==(other) ⇒ Boolean

Check if equal

Parameters:

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/canon/tree_diff/core/node_weight.rb', line 51

def ==(other)
  return false unless other.is_a?(NodeWeight)

  value == other.value
end

#inspectString

Detailed inspection

Returns:

  • (String)


81
82
83
# File 'lib/canon/tree_diff/core/node_weight.rb', line 81

def inspect
  "#<NodeWeight #{value}>"
end

#to_fFloat

Numeric value for calculations

Returns:

  • (Float)


60
61
62
# File 'lib/canon/tree_diff/core/node_weight.rb', line 60

def to_f
  value
end

#to_iInteger

Integer value for calculations

Returns:

  • (Integer)


67
68
69
# File 'lib/canon/tree_diff/core/node_weight.rb', line 67

def to_i
  value.to_i
end

#to_sString

String representation

Returns:

  • (String)


74
75
76
# File 'lib/canon/tree_diff/core/node_weight.rb', line 74

def to_s
  value.to_s
end