-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Ensure the tracking on nested and_then callings
- Loading branch information
Showing
11 changed files
with
318 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
class BCDD::Result | ||
module Transitions | ||
class Tree | ||
class Node | ||
attr_reader :id, :value, :parent, :normalizer, :children | ||
|
||
def initialize(value, parent:, id:, normalizer:) | ||
@normalizer = normalizer | ||
|
||
@id = id | ||
@value = normalizer.call(id, value) | ||
@parent = parent | ||
|
||
@children = [] | ||
end | ||
|
||
def insert(value, id:) | ||
node = self.class.new(value, parent: self, id: id, normalizer: normalizer) | ||
|
||
@children << node | ||
|
||
node | ||
end | ||
|
||
def root? | ||
parent.nil? | ||
end | ||
|
||
def leaf? | ||
children.empty? | ||
end | ||
|
||
def node? | ||
!leaf? | ||
end | ||
|
||
def inspect | ||
"#<#{self.class.name} id=#{id} children.size=#{children.size}>" | ||
end | ||
end | ||
|
||
attr_reader :size, :root, :current | ||
|
||
def initialize(value, normalizer: ->(_id, val) { val }) | ||
@size = 0 | ||
|
||
@root = Node.new(value, parent: nil, id: @size, normalizer: normalizer) | ||
|
||
@current = @root | ||
end | ||
|
||
def root_value | ||
root.value | ||
end | ||
|
||
def parent_value | ||
current.parent&.value || root_value | ||
end | ||
|
||
def current_value | ||
current.value | ||
end | ||
|
||
def insert(value) | ||
@size += 1 | ||
|
||
current.insert(value, id: size) | ||
end | ||
|
||
def insert!(value) | ||
@current = insert(value) | ||
end | ||
|
||
def move_up!(level = 1) | ||
tap { level.times { @current = current.parent || root } } | ||
end | ||
|
||
def move_down!(level = 1, index: -1) | ||
tap { level.times { current.children[index].then { |child| @current = child if child } } } | ||
end | ||
|
||
def move_to_root! | ||
tap { @current = root } | ||
end | ||
|
||
NestedIds = ->(node) { [node.id, node.children.map(&NestedIds)] } | ||
|
||
def nested_ids | ||
NestedIds[root] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.