Class: MSSMT::LeafNode

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

Overview

Leaf node within a MS-SMT that commit to a value and some integer value (the sum) associated with the value.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, sum) ⇒ LeafNode

Constructor



12
13
14
15
16
17
18
# File 'lib/mssmt/leaf_node.rb', line 12

def initialize(value, sum)
  @value = value
  if sum > Tree::MAX_SUM_VALUE
    raise OverflowError, "sum: #{sum} is overflow"
  end
  @sum = sum
end

Instance Attribute Details

#sumObject (readonly)

Returns the value of attribute sum.



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

def sum
  @sum
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.empty_leafMSSMT::LeafNode

Generate empty leaf node.



22
23
24
# File 'lib/mssmt/leaf_node.rb', line 22

def self.empty_leaf
  LeafNode.new(nil, 0)
end

Instance Method Details

#==(other) ⇒ Object



38
39
40
41
# File 'lib/mssmt/leaf_node.rb', line 38

def ==(other)
  return false unless other.is_a?(LeafNode)
  node_hash == other.node_hash
end

#empty?Boolean

Check whether value and sum is empty.



34
35
36
# File 'lib/mssmt/leaf_node.rb', line 34

def empty?
  (value.nil? or value.empty?) && sum.zero?
end

#node_hashString

Calculate node hash.



28
29
30
# File 'lib/mssmt/leaf_node.rb', line 28

def node_hash
  Digest::SHA256.digest("#{value}#{[sum].pack("Q>")}")
end