Class: Melisa::Search
- Inherits:
-
Object
- Object
- Melisa::Search
- Includes:
- Enumerable
- Defined in:
- lib/melisa/search.rb
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #has_keys? ⇒ Boolean
- #include?(key) ⇒ Boolean
-
#initialize(trie, prefix) ⇒ Search
constructor
A new instance of Search.
- #keys ⇒ Object
- #reset_agent ⇒ Object
- #search(prefix) ⇒ Object
- #size ⇒ Object
- #with_prefixes(&block) ⇒ Object
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
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
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 |
#keys ⇒ Object
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_agent ⇒ Object
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 |
#size ⇒ Object
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 |