Class: MSSMT::BranchNode

Inherits:
Object
  • Object
show all
Defined in:
lib/mssmt/branch_node.rb

Overview

Intermediate or root node within a MS-SMT.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, right) ⇒ BranchNode

Constructor



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mssmt/branch_node.rb', line 12

def initialize(left, right)
  if !left.is_a?(BranchNode) && !left.is_a?(LeafNode)
    raise ArgumentError, "left must be a branch or leaf node"
  end
  if !right.is_a?(BranchNode) && !right.is_a?(LeafNode)
    raise ArgumentError, "right must be a branch or leaf node"
  end

  @left = left
  @right = right
  @sum = left.sum + right.sum
  if @sum > Tree::MAX_SUM_VALUE
    raise OverflowError, "sum: #{sum} is overflow"
  end

  @node_hash =
    Digest::SHA256.digest(
      "#{left.node_hash}#{right.node_hash}#{[@sum].pack("Q>")}"
    )
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



6
7
8
# File 'lib/mssmt/branch_node.rb', line 6

def left
  @left
end

#node_hashObject (readonly)

Returns the value of attribute node_hash.



6
7
8
# File 'lib/mssmt/branch_node.rb', line 6

def node_hash
  @node_hash
end

#rightObject (readonly)

Returns the value of attribute right.



6
7
8
# File 'lib/mssmt/branch_node.rb', line 6

def right
  @right
end

#sumObject (readonly)

Returns the value of attribute sum.



6
7
8
# File 'lib/mssmt/branch_node.rb', line 6

def sum
  @sum
end

Instance Method Details

#==(other) ⇒ Boolean

Check whether same branch|computed node or not.

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/mssmt/branch_node.rb', line 35

def ==(other)
  return false unless [BranchNode, ComputedNode].include?(other.class)
  node_hash == other.node_hash && sum == other.sum
end

#inspectObject



40
41
42
# File 'lib/mssmt/branch_node.rb', line 40

def inspect
  "left: #{left.node_hash.unpack1("H*")}, right: #{right.node_hash.unpack1("H*")}, sum: #{sum}"
end