Class: Decode::Trie::Node
- Inherits:
-
Object
- Object
- Decode::Trie::Node
- Defined in:
- lib/decode/trie.rb
Overview
A single node in the trie.
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
A hash table of all children nodes, indexed by name.
-
#values ⇒ Object
A mutable array of all values that terminate at this node.
Instance Method Summary collapse
-
#initialize ⇒ Node
constructor
A new instance of Node.
-
#lookup(path, index = 0) ⇒ Object
Look up a lexical path starting at this node.
-
#traverse(path = []) {|path, _self, ->{ @children.each do |name, node| node.traverse([*path, name], &block) end }| ... } ⇒ Object
Traverse the trie from this node.
Constructor Details
#initialize ⇒ Node
Returns a new instance of Node.
28 29 30 31 |
# File 'lib/decode/trie.rb', line 28 def initialize @values = nil @children = Hash.new end |
Instance Attribute Details
#children ⇒ Object (readonly)
A hash table of all children nodes, indexed by name.
39 40 41 |
# File 'lib/decode/trie.rb', line 39 def children @children end |
#values ⇒ Object
A mutable array of all values that terminate at this node.
35 36 37 |
# File 'lib/decode/trie.rb', line 35 def values @values end |
Instance Method Details
#lookup(path, index = 0) ⇒ Object
Look up a lexical path starting at this node.
45 46 47 48 49 50 51 52 53 |
# 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, ->{ @children.each do |name, node| node.traverse([*path, name], &block) end }| ... } ⇒ Object
Traverse the trie from this node. Invoke ‘descend.call` to traverse the children of the current node.
64 65 66 67 68 69 70 |
# 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 |