Class: Fib::Trie

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/fib/trie.rb', line 3

def data
  @data
end

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/fib/trie.rb', line 3

def key
  @key
end

#subnodeObject

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