Class: Melisa::BytesTrie

Inherits:
Trie
  • Object
show all
Defined in:
lib/melisa/bytes_trie.rb

Direct Known Subclasses

IntTrie

Instance Attribute Summary

Attributes inherited from Trie

#trie

Instance Method Summary collapse

Methods inherited from Trie

#add, #build, #each, #has_keys?, #keys, #load, #save, #search, #size

Methods included from BaseConfigFlags

#binary_flag, #config_flags, #lookup_cache_size, #valid_node_order, #valid_num_tries

Constructor Details

#initialize(hash = {}, separator = VALUE_SEPARATOR, opts = {}) ⇒ BytesTrie

Returns a new instance of BytesTrie.



5
6
7
8
9
10
11
12
# File 'lib/melisa/bytes_trie.rb', line 5

def initialize(hash={}, separator=VALUE_SEPARATOR, opts={})
  super([], [], opts)

  @sep = separator
  @sep_c = separator.force_encoding('binary').ord

  add_many(hash, [])
end

Instance Method Details

#add_many(hash, weights) ⇒ Object



14
15
16
17
18
# File 'lib/melisa/bytes_trie.rb', line 14

def add_many(hash, weights)
  for key, value in hash
    push(raw_key(key, value))
  end
end

#get(key) ⇒ Object Also known as: []



24
25
26
27
28
29
30
31
# File 'lib/melisa/bytes_trie.rb', line 24

def get(key)
  build unless @built
  agent = Marisa::Agent.new
  agent.set_query(key + @sep)
  if @trie.predictive_search(agent)
    agent_key_value(agent)
  end
end

#get_all(key) ⇒ Object

Search for many results with a given prefix



40
41
42
43
44
45
46
47
48
49
# File 'lib/melisa/bytes_trie.rb', line 40

def get_all(key)
  build unless @built
  agent = Marisa::Agent.new
  agent.set_query(key)
  [].tap do |results|
    while @trie.predictive_search(agent)
      results << agent_key_value(agent)
    end
  end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/melisa/bytes_trie.rb', line 20

def include?(key)
  super(key + @sep)
end

#set(key, value) ⇒ Object Also known as: []=



34
35
36
# File 'lib/melisa/bytes_trie.rb', line 34

def set(key, value)
  add(raw_key(key, value))
end