Class: Decode::Trie::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/trie.rb

Overview

Represents a single node in the trie.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Initialize an empty node.



15
16
17
18
# File 'lib/decode/trie.rb', line 15

def initialize
  @children = {}
  @values = nil
end

Instance Attribute Details

#childrenObject (readonly)

A hash table of all children nodes, indexed by name.



35
36
37
# File 'lib/decode/trie.rb', line 35

def children
  @children
end

#The values stored at this node, or nil if no values.(valuesstoredatthisnode) ⇒ Object (readonly)

A mutable array of all values that terminate at this node.



31
# File 'lib/decode/trie.rb', line 31

attr_accessor :values

#valuesObject

A mutable array of all values that terminate at this node.



31
32
33
# File 'lib/decode/trie.rb', line 31

def values
  @values
end

Instance Method Details

#Child nodes indexed by their path component.=(nodesindexedbytheirpathcomponent. = (value)) ⇒ Object

A hash table of all children nodes, indexed by name.



35
# File 'lib/decode/trie.rb', line 35

attr :children

#inspectObject Also known as: to_s

Generate a string representation of this node.



22
23
24
# File 'lib/decode/trie.rb', line 22

def inspect
  "#<#{self.class} #{@children.size} children>"
end

#lookup(path, index = 0) ⇒ Object

Look up a lexical path starting at this node.



41
42
43
44
45
46
47
48
49
# File 'lib/decode/trie.rb', line 41

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, descend| ... } ⇒ Object

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

Yields:

  • (path, _self, descend)

Yield Parameters:



59
60
61
62
63
64
65
66
67
# File 'lib/decode/trie.rb', line 59

def traverse(path = [], &block)
  descend = lambda do
    @children.each do |name, node|
      node.traverse([*path, name], &block)
    end
  end
  
  yield(path, self, descend)
end