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, #agent, #build, #build_if_necessary, #built?, #get_id, #get_key, #get_weight, #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, weight = nil) ⇒ Object



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

def add_many(hash, weight=nil)
  for key, value in hash
    push(key_and_value_as_string(key, value), weight)
  end
end

#each(&block) ⇒ Object



49
50
51
52
53
# File 'lib/melisa/bytes_trie.rb', line 49

def each(&block)
  search('').each do |str|
    yield string_as_key_and_value(str)
  end
end

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



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

def get(key)
  build_if_necessary
  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



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

def get_all(key)
  build_if_necessary
  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: []=



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

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