Class: Decode::Trie

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

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrie

Returns a new instance of Trie.



55
56
57
# File 'lib/decode/trie.rb', line 55

def initialize
  @root = Node.new
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



59
60
61
# File 'lib/decode/trie.rb', line 59

def root
  @root
end

#valueObject

Returns the value of attribute value.



53
54
55
# File 'lib/decode/trie.rb', line 53

def value
  @value
end

Instance Method Details

#each(path) {|path, values| ... } ⇒ Object

Given a base path, enumerate all paths under that.

Yields:

  • (path, values)

    pairs



77
78
79
80
81
# File 'lib/decode/trie.rb', line 77

def each(path, &block)
  if node = @root.lookup(path)
    node.traverse(&block)
  end
end

#insert(path, value) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/decode/trie.rb', line 61

def insert(path, value)
  node = @root
  
  path.each do |key|
    node = (node.children[key] ||= Node.new)
  end
  
  (node.values ||= []) << value
end

#lookup(path) ⇒ Object



71
72
73
# File 'lib/decode/trie.rb', line 71

def lookup(path)
  @root.lookup(path).values
end

#match(path, &block) ⇒ Object



83
84
85
# File 'lib/decode/trie.rb', line 83

def match(path, &block)
  @root.lookup(path)
end