Sha256: d0c51589b25d98156528d5b9bbad446cbf16aaf51a720b1c80d42539bdea03ce
Contents?: true
Size: 1.91 KB
Versions: 1
Compression:
Stored size: 1.91 KB
Contents
# 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) move_to! insert(value) end def move_to!(node) tap { @current = node } end def move_up!(level = 1) tap { level.times { move_to!(current.parent || root) } } end def move_down!(level = 1, index: -1) tap { level.times { current.children[index].then { |child| move_to!(child) if child } } } end def move_to_root! move_to!(root) end NestedIds = ->(node) { [node.id, node.children.map(&NestedIds)] } def nested_ids NestedIds[root] end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
bcdd-result-0.12.0 | lib/bcdd/result/transitions/tree.rb |