Class: Tapyrus::MerkleTree::Node
- Inherits:
-
Object
- Object
- Tapyrus::MerkleTree::Node
- Defined in:
- lib/tapyrus/merkle_tree.rb
Overview
node of merkle tree
Instance Attribute Summary collapse
-
#flag ⇒ Object
Returns the value of attribute flag.
-
#left ⇒ Object
Returns the value of attribute left.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#right ⇒ Object
Returns the value of attribute right.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#depth ⇒ Object
calculate the depth of this node in the tree.
-
#find_node(target) ⇒ Object
Node which has same value as target.
- #index ⇒ Object
-
#initialize(value = nil) ⇒ Node
constructor
A new instance of Node.
- #leaf? ⇒ Boolean
- #next_partial ⇒ Object
- #partial? ⇒ Boolean
- #root? ⇒ Boolean
Constructor Details
#initialize(value = nil) ⇒ Node
Returns a new instance of Node.
79 80 81 |
# File 'lib/tapyrus/merkle_tree.rb', line 79 def initialize(value = nil) @value = value end |
Instance Attribute Details
#flag ⇒ Object
Returns the value of attribute flag.
73 74 75 |
# File 'lib/tapyrus/merkle_tree.rb', line 73 def flag @flag end |
#left ⇒ Object
Returns the value of attribute left.
76 77 78 |
# File 'lib/tapyrus/merkle_tree.rb', line 76 def left @left end |
#parent ⇒ Object
Returns the value of attribute parent.
75 76 77 |
# File 'lib/tapyrus/merkle_tree.rb', line 75 def parent @parent end |
#right ⇒ Object
Returns the value of attribute right.
77 78 79 |
# File 'lib/tapyrus/merkle_tree.rb', line 77 def right @right end |
#value ⇒ Object
Returns the value of attribute value.
74 75 76 |
# File 'lib/tapyrus/merkle_tree.rb', line 74 def value @value end |
Instance Method Details
#depth ⇒ Object
calculate the depth of this node in the tree.
120 121 122 123 124 125 126 127 128 |
# File 'lib/tapyrus/merkle_tree.rb', line 120 def depth d = 0 current_node = self until current_node.root? current_node = current_node.parent d += 1 end d end |
#find_node(target) ⇒ Object
Returns node which has same value as target. nil if this node and any children don’t have same value.
132 133 134 135 136 |
# File 'lib/tapyrus/merkle_tree.rb', line 132 def find_node(target) return self if value == target return nil if flag && flag.zero? return left&.find_node(target) || right&.find_node(target) end |
#index ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/tapyrus/merkle_tree.rb', line 138 def index i = 0 d = 1 current_node = self until current_node.root? i += d if current_node.parent.right == current_node current_node = current_node.parent d *= 2 end i end |
#leaf? ⇒ Boolean
103 104 105 |
# File 'lib/tapyrus/merkle_tree.rb', line 103 def leaf? right.nil? && left.nil? end |
#next_partial ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/tapyrus/merkle_tree.rb', line 111 def next_partial return nil if root? && (flag.zero? || (left.nil? && right.nil?) || (left.partial? && right.partial?)) return parent.next_partial if flag.zero? || leaf? return left unless left.partial? self.right = left.dup unless right right.partial? ? parent.next_partial : right end |
#partial? ⇒ Boolean
107 108 109 |
# File 'lib/tapyrus/merkle_tree.rb', line 107 def partial? !flag.nil? end |
#root? ⇒ Boolean
99 100 101 |
# File 'lib/tapyrus/merkle_tree.rb', line 99 def root? parent.nil? end |