Class: Melisa::Search

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/melisa/search.rb

Instance Method Summary collapse

Constructor Details

#initialize(trie, prefix) ⇒ Search

Returns a new instance of Search.



5
6
7
8
# File 'lib/melisa/search.rb', line 5

def initialize(trie, prefix)
  @trie = trie
  @prefix = prefix
end

Instance Method Details

#each(&block) ⇒ Object



20
21
22
23
24
# File 'lib/melisa/search.rb', line 20

def each(&block)
  reset_agent
  # Yield each key
  yield @agent.key_str while @trie.trie.predictive_search(@agent)
end

#has_keys?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/melisa/search.rb', line 37

def has_keys?
  reset_agent
  return @trie.trie.predictive_search(@agent)
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/melisa/search.rb', line 42

def include?(key)
  a = Marisa::Agent.new
  a.set_query(key)
  @trie.trie.lookup(a)
end

#keysObject



30
31
32
33
34
35
# File 'lib/melisa/search.rb', line 30

def keys
  @keys ||= [].tap do |arr|
    reset_agent
    arr << @agent.key_str while @trie.trie.predictive_search(@agent)
  end
end

#reset_agentObject



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

def reset_agent
  # Reset the agent state so predictive_search iterates through all keys
  @agent = Marisa::Agent.new
  @agent.set_query(@prefix)
end

#search(prefix) ⇒ Object



10
11
12
# File 'lib/melisa/search.rb', line 10

def search(prefix)
  Search.new(@trie, @prefix + prefix)
end

#sizeObject



26
27
28
# File 'lib/melisa/search.rb', line 26

def size
  keys.size
end

#with_prefixes(&block) ⇒ Object



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

def with_prefixes(&block)
  reset_agent
  while @trie.trie.common_prefix_search(@agent)
    block.call(@agent.key_str)
  end
end