Class: Canon::TreeDiff::Core::NodeSignature

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

Overview

NodeSignature computes unique signatures for tree nodes

Based on XDiff (2002, U. Wisconsin) approach:

  • Signature is the path from root to node

  • Format: /ancestor1/ancestor2/…/node/type

  • Used for fast exact matching via hash lookup

Features:

  • Deterministic: Same path always produces same signature

  • Hierarchical: Parent-child relationships encoded

  • Type-aware: Distinguishes element vs text nodes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, include_attributes: true) ⇒ NodeSignature

Initialize signature for a node

Parameters:

  • node (TreeNode)

    Node to compute signature for

  • include_attributes (Boolean) (defaults to: true)

    Whether to include attributes



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

def initialize(node, include_attributes: true)
  @node = node
  @include_attributes = include_attributes
  @path = compute_path
  @signature_string = compute_signature_string
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/canon/tree_diff/core/node_signature.rb', line 18

def path
  @path
end

#signature_stringObject (readonly)

Returns the value of attribute signature_string.



18
19
20
# File 'lib/canon/tree_diff/core/node_signature.rb', line 18

def signature_string
  @signature_string
end

Class Method Details

.for(node, include_attributes: true) ⇒ NodeSignature

Compute and cache signature for a node

Parameters:

  • node (TreeNode)

    Node to compute signature for

  • include_attributes (Boolean) (defaults to: true)

    Whether to include attributes in signature

Returns:



36
37
38
39
40
41
42
43
# File 'lib/canon/tree_diff/core/node_signature.rb', line 36

def self.for(node, include_attributes: true)
  if include_attributes
    node.signature ||= new(node, include_attributes: true)
  else
    # Don't cache loose signatures
    new(node, include_attributes: false)
  end
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Check if two signatures are equal

Parameters:

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/canon/tree_diff/core/node_signature.rb', line 49

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

  signature_string == other.signature_string
end

#hashInteger

Hash value for use in Hash/Set

Returns:

  • (Integer)


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

def hash
  signature_string.hash
end

#inspectString

Detailed inspection

Returns:

  • (String)


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

def inspect
  "#<NodeSignature #{signature_string.inspect}>"
end

#to_sString

String representation

Returns:

  • (String)


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

def to_s
  signature_string
end