Class: Fib::Trie
- Inherits:
-
Object
- Object
- Fib::Trie
- Defined in:
- lib/fib/trie.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#key ⇒ Object
Returns the value of attribute key.
-
#subnode ⇒ Object
Returns the value of attribute subnode.
Instance Method Summary collapse
- #add_sub(key, node) ⇒ Object
- #dig(*node_key) ⇒ Object
-
#initialize(key, data, subnode = {}) ⇒ Trie
constructor
A new instance of Trie.
Constructor Details
#initialize(key, data, subnode = {}) ⇒ Trie
Returns a new instance of Trie.
5 6 7 8 9 |
# File 'lib/fib/trie.rb', line 5 def initialize key, data, subnode={} @key = key @data = data @subnode = subnode end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/fib/trie.rb', line 3 def data @data end |
#key ⇒ Object
Returns the value of attribute key.
3 4 5 |
# File 'lib/fib/trie.rb', line 3 def key @key end |
#subnode ⇒ Object
Returns the value of attribute subnode.
3 4 5 |
# File 'lib/fib/trie.rb', line 3 def subnode @subnode end |
Instance Method Details
#add_sub(key, node) ⇒ Object
24 25 26 27 28 |
# File 'lib/fib/trie.rb', line 24 def add_sub key, node t = Trie.new key, node subnode[key] = t t end |
#dig(*node_key) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/fib/trie.rb', line 11 def dig *node_key return nil unless node_key.is_a? Array if node_key.size < 1 return data end current_key = node_key.first node_key.shift subnode.has_key?(current_key) ? subnode[current_key]&.dig(*node_key) : nil end |