Class: Decode::Trie
- Inherits:
-
Object
- Object
- Decode::Trie
- Defined in:
- lib/decode/trie.rb
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#each(path) {|path, values| ... } ⇒ Object
Given a base path, enumerate all paths under that.
-
#initialize ⇒ Trie
constructor
A new instance of Trie.
- #insert(path, value) ⇒ Object
- #lookup(path) ⇒ Object
- #match(path, &block) ⇒ Object
Constructor Details
#initialize ⇒ Trie
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
#root ⇒ Object (readonly)
Returns the value of attribute root.
59 60 61 |
# File 'lib/decode/trie.rb', line 59 def root @root end |
#value ⇒ Object
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.
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 |