Class: VMM::Trie

Inherits:
Object
  • Object
show all
Defined in:
lib/lite/vmm.rb

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ Trie

Returns a new instance of Trie.



63
64
65
# File 'lib/lite/vmm.rb', line 63

def initialize(root=nil)
  @root = root.nil? ? ( [{}, {} ]) : root
end

Instance Method Details

#grow(context, symbol) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lite/vmm.rb', line 71

def grow(context, symbol) 
  node = @root
  @root[1][symbol]||=0
  @root[1][symbol]+=1
  context.each do |ch|
    node[0][ch] ||= new_node symbol
    node[1][symbol]||=0 
    node[1][symbol] += 1
    node = node[0][ch]
  end
  true
end

#new_node(v) ⇒ Object



92
93
94
# File 'lib/lite/vmm.rb', line 92

def new_node( v )
  [{}, { v => 1}]
end

#path(sym_arr) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/lite/vmm.rb', line 84

def path sym_arr
  sym_arr.inject([@root]) do |agg,ch|
    next(agg) unless agg.last[0][ch]
    agg << agg.last[0][ch]
    agg
  end
end

#rootObject



67
68
69
# File 'lib/lite/vmm.rb', line 67

def root
  @root
end