Class: Decode::Trie::Node
- Inherits:
-
Object
- Object
- Decode::Trie::Node
- Defined in:
- lib/decode/trie.rb
Overview
Represents a single node in the trie.
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
A hash table of all children nodes, indexed by name.
-
#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.
-
#values ⇒ Object
A mutable array of all values that terminate at this node.
Instance Method Summary collapse
-
#Child nodes indexed by their path component.=(nodesindexedbytheirpathcomponent. = (value)) ⇒ Object
A hash table of all children nodes, indexed by name.
-
#initialize ⇒ Node
constructor
Initialize an empty node.
-
#inspect ⇒ Object
(also: #to_s)
Generate a string representation of this node.
-
#lookup(path, index = 0) ⇒ Object
Look up a lexical path starting at this node.
-
#traverse(path = []) {|path, _self, descend| ... } ⇒ Object
Traverse the trie from this node.
Constructor Details
#initialize ⇒ Node
Initialize an empty node.
15 16 17 18 |
# File 'lib/decode/trie.rb', line 15 def initialize @children = {} @values = nil end |
Instance Attribute Details
#children ⇒ Object (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 |
#values ⇒ Object
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 |
#inspect ⇒ Object 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.
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 |