Class: Decode::Trie::Node

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

Overview

A single node in the trie.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

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

#childrenObject (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

#valuesObject

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

#inspectObject 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.

Yields:

Yield Parameters:



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