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.



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

def initialize
	@values = nil
	@children = Hash.new
end

Instance Attribute Details

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

#valuesObject

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.

Yields:

Yield Parameters:



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