class Decode::Trie::Node

A single node in the trie.

Attributes

children[R]

A hash table of all children nodes, indexed by name. @attribute [Hash(String, Node)]

values[RW]

A mutable array of all values that terminate at this node. @attribute [Array]

Public Class Methods

new() click to toggle source
# File lib/decode/trie.rb, line 28
def initialize
        @values = nil
        @children = Hash.new
end

Public Instance Methods

lookup(path, index = 0) click to toggle source

Look up a lexical path starting at this node.

@parameter path [Array(String)] The path to resolve. @returns [Node | Nil]

# File lib/decode/trie.rb, line 45
def lookup(path, index = 0)
        if index < path.size
                if child = @children[path[index]]
                        return child.lookup(path, index+1)
                end
        else
                return self
        end
end
traverse(path = []) { |path, self, ->{ each do |name, node| traverse([*path, name], &block end }| ... } click to toggle source

Traverse the trie from this node. Invoke `descend.call` to traverse the children of the current node.

@parameter path [Array(String)] The current lexical path.

@block {|path, node, descend| descend.call} @yield path [Array(String)] The current lexical path. @yield node [Node] The current node which is being traversed. @yield descend [Proc] The recursive method for traversing children.

# File lib/decode/trie.rb, line 64
def traverse(path = [], &block)
        yield(path, self, ->{
                @children.each do |name, node|
                        node.traverse([*path, name], &block)
                end
        })
end