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.
- #inspect ⇒ Object (also: #to_s)
-
#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.
13 14 15 16 |
# File 'lib/decode/trie.rb', line 13 def initialize @values = nil @children = Hash.new end |
Instance Attribute Details
#children ⇒ Object (readonly)
A hash table of all children nodes, indexed by name.
30 31 32 |
# File 'lib/decode/trie.rb', line 30 def children @children end |
#values ⇒ Object
A mutable array of all values that terminate at this node.
26 27 28 |
# File 'lib/decode/trie.rb', line 26 def values @values end |
Instance Method Details
#inspect ⇒ Object Also known as: to_s
18 19 20 |
# File 'lib/decode/trie.rb', line 18 def inspect "#<#{self.class} #{@children.size} children>" end |
#lookup(path, index = 0) ⇒ Object
Look up a lexical path starting at this node.
36 37 38 39 40 41 42 43 44 |
# File 'lib/decode/trie.rb', line 36 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.
55 56 57 58 59 60 61 |
# File 'lib/decode/trie.rb', line 55 def traverse(path = [], &block) yield(path, self, ->{ @children.each do |name, node| node.traverse([*path, name], &block) end }) end |