Class: Solid::Result::EventLogs::Tree

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#currentObject (readonly)

Returns the value of attribute current.



44
45
46
# File 'lib/solid/result/event_logs/tree.rb', line 44

def current
  @current
end

#rootObject (readonly)

Returns the value of attribute root.



44
45
46
# File 'lib/solid/result/event_logs/tree.rb', line 44

def root
  @root
end

#sizeObject (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_valueObject



62
63
64
# File 'lib/solid/result/event_logs/tree.rb', line 62

def current_value
  current.value
end

#idsObject



94
95
96
# File 'lib/solid/result/event_logs/tree.rb', line 94

def ids
  Ids[root]
end

#ids_level_parentObject



136
137
138
# File 'lib/solid/result/event_logs/tree.rb', line 136

def ids_level_parent
  IdsLevelParent[ids]
end

#ids_listObject



98
99
100
# File 'lib/solid/result/event_logs/tree.rb', line 98

def ids_list
  ids.flatten
end

#ids_matrixObject



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_valueObject



58
59
60
# File 'lib/solid/result/event_logs/tree.rb', line 58

def parent_value
  current.parent&.value || root_value
end

#root_valueObject



54
55
56
# File 'lib/solid/result/event_logs/tree.rb', line 54

def root_value
  root.value
end