Class: Solid::Result::EventLogs::Tree
- Inherits:
-
Object
- Object
- Solid::Result::EventLogs::Tree
- Defined in:
- lib/solid/result/event_logs/tree.rb
Defined Under Namespace
Classes: Node
Constant Summary collapse
- Ids =
->(node) { [node.id, node.children.map(&Ids)] }
- IdsMatrix =
->(tree, row, col, memo, previous) do last_row = previous[0] tree.each_with_index do |node, index| row = [(index + 1), last_row].max id, leaf = node memo[id] = previous == [row, col] ? [row, col + 1] : [row, col] previous = memo[id] IdsMatrix[leaf, row, col + 1, memo, previous] end end
- IdsLevelParent =
->((id, node), parent = 0, level = 0, memo = {}) do memo[id] = [level, parent] node.each { |leaf| IdsLevelParent[leaf, id, level + 1, memo] } memo end
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #current_value ⇒ Object
- #ids ⇒ Object
- #ids_level_parent ⇒ Object
- #ids_list ⇒ Object
- #ids_matrix ⇒ Object
-
#initialize(value, normalizer: ->(_id, val) { val }) ⇒ Tree
constructor
A new instance of Tree.
- #insert(value) ⇒ Object
- #insert!(value) ⇒ Object
- #move_down!(level = 1, index: -1)) ⇒ Object
- #move_to!(node) ⇒ Object
- #move_to_root! ⇒ Object
- #move_up!(level = 1) ⇒ Object
- #parent_value ⇒ Object
- #root_value ⇒ Object
Constructor Details
#initialize(value, normalizer: ->(_id, val) { val }) ⇒ Tree
Returns a new instance of Tree.
46 47 48 49 50 51 52 |
# File 'lib/solid/result/event_logs/tree.rb', line 46 def initialize(value, normalizer: ->(_id, val) { val }) @size = 0 @root = Node.new(value, parent: nil, id: size, normalizer: normalizer) @current = root end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
44 45 46 |
# File 'lib/solid/result/event_logs/tree.rb', line 44 def current @current end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
44 45 46 |
# File 'lib/solid/result/event_logs/tree.rb', line 44 def root @root end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
44 45 46 |
# File 'lib/solid/result/event_logs/tree.rb', line 44 def size @size end |
Instance Method Details
#current_value ⇒ Object
62 63 64 |
# File 'lib/solid/result/event_logs/tree.rb', line 62 def current_value current.value end |
#ids_level_parent ⇒ Object
136 137 138 |
# File 'lib/solid/result/event_logs/tree.rb', line 136 def ids_level_parent IdsLevelParent[ids] end |
#ids_list ⇒ Object
98 99 100 |
# File 'lib/solid/result/event_logs/tree.rb', line 98 def ids_list ids.flatten end |
#ids_matrix ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/solid/result/event_logs/tree.rb', line 118 def ids_matrix current = [0, 0] memo = { 0 => current } IdsMatrix[ids[1], 1, 1, memo, current] memo end |
#insert(value) ⇒ Object
66 67 68 69 70 |
# File 'lib/solid/result/event_logs/tree.rb', line 66 def insert(value) @size += 1 current.insert(value, id: size) end |
#insert!(value) ⇒ Object
72 73 74 |
# File 'lib/solid/result/event_logs/tree.rb', line 72 def insert!(value) move_to! insert(value) end |
#move_down!(level = 1, index: -1)) ⇒ Object
84 85 86 |
# File 'lib/solid/result/event_logs/tree.rb', line 84 def move_down!(level = 1, index: -1) tap { level.times { current.children[index].then { |child| move_to!(child) if child } } } end |
#move_to!(node) ⇒ Object
76 77 78 |
# File 'lib/solid/result/event_logs/tree.rb', line 76 def move_to!(node) tap { @current = node } end |
#move_to_root! ⇒ Object
88 89 90 |
# File 'lib/solid/result/event_logs/tree.rb', line 88 def move_to_root! move_to!(root) end |
#move_up!(level = 1) ⇒ Object
80 81 82 |
# File 'lib/solid/result/event_logs/tree.rb', line 80 def move_up!(level = 1) tap { level.times { move_to!(current.parent || root) } } end |
#parent_value ⇒ Object
58 59 60 |
# File 'lib/solid/result/event_logs/tree.rb', line 58 def parent_value current.parent&.value || root_value end |
#root_value ⇒ Object
54 55 56 |
# File 'lib/solid/result/event_logs/tree.rb', line 54 def root_value root.value end |