Class: Canon::TreeDiff::Core::NodeWeight
- Inherits:
-
Object
- Object
- Canon::TreeDiff::Core::NodeWeight
- 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
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.for(node) ⇒ NodeWeight
Compute and cache weight for a node.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare weights (for sorting).
-
#==(other) ⇒ Boolean
Check if equal.
-
#initialize(node) ⇒ NodeWeight
constructor
Initialize weight for a node.
-
#inspect ⇒ String
Detailed inspection.
-
#to_f ⇒ Float
Numeric value for calculations.
-
#to_i ⇒ Integer
Integer value for calculations.
-
#to_s ⇒ String
String representation.
Constructor Details
#initialize(node) ⇒ NodeWeight
Initialize weight for a node
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
#value ⇒ Object (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
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)
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
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 |
#inspect ⇒ String
Detailed inspection
81 82 83 |
# File 'lib/canon/tree_diff/core/node_weight.rb', line 81 def inspect "#<NodeWeight #{value}>" end |
#to_f ⇒ Float
Numeric value for calculations
60 61 62 |
# File 'lib/canon/tree_diff/core/node_weight.rb', line 60 def to_f value end |
#to_i ⇒ Integer
Integer value for calculations
67 68 69 |
# File 'lib/canon/tree_diff/core/node_weight.rb', line 67 def to_i value.to_i end |
#to_s ⇒ String
String representation
74 75 76 |
# File 'lib/canon/tree_diff/core/node_weight.rb', line 74 def to_s value.to_s end |