lib/rambling/trie/compressed_node.rb in rambling-trie-0.9.0 vs lib/rambling/trie/compressed_node.rb in rambling-trie-0.9.1
- old
+ new
@@ -20,93 +20,78 @@
# Checks if a path for set of characters represents a word in the trie.
# @param [Array] chars the characters to look for in the trie.
# @return [Boolean] `true` if the characters are found and form a word,
# `false` otherwise.
def word? chars
- if chars.empty?
- terminal?
- else
- has_word? chars
- end
+ chars.empty? ? terminal? : has_word?(chars)
end
# Returns all words that start with the specified characters.
# @param [Array] chars the characters to look for in the trie.
# @return [Array] all the words contained in the trie that start with the specified characters.
def scan chars
- closest_node(chars).to_a
+ node = chars.empty? ? self : closest_node(chars)
+ node.to_a
end
# Always return `true` for a raw (compressed) node.
# @return [Boolean] always true for a raw (compressed) node.
def compressed?
true
end
- protected
+ private
- def closest_node chars
- if chars.empty?
- self
- else
- current_length = 0
- current_key, current_key_string = current_key chars.slice!(0)
+ def has_partial_word? chars
+ recursive_get(:partial_word?, chars) || false
+ end
- begin
- current_length += 1
+ def has_word? chars
+ current_key = nil
- if current_key_string.length == current_length || chars.empty?
- return children_tree[current_key].closest_node chars
- end
- end while current_key_string[current_length] == chars.slice!(0)
+ while !chars.empty?
+ if current_key
+ current_key << chars.slice!(0)
+ else
+ current_key = chars.slice!(0)
+ end
- Rambling::Trie::MissingNode.new
+ child = children_tree[current_key.to_sym]
+ return child.word? chars if child
end
+
+ false
end
- private
+ def closest_node chars
+ recursive_get(:scan, chars) || Rambling::Trie::MissingNode.new
+ end
- def has_partial_word? chars
+ def recursive_get method, chars
current_length = 0
- current_key, current_key_string = current_key chars.slice!(0)
+ current_key = current_key chars.slice!(0)
begin
current_length += 1
- if current_key_string.length == current_length || chars.empty?
- return children_tree[current_key].partial_word? chars
+ if (current_key && current_key.length == current_length) || chars.empty?
+ return children_tree[current_key.to_sym].send method, chars
end
- end while current_key_string[current_length] == chars.slice!(0)
-
- false
+ end while current_key && current_key[current_length] == chars.slice!(0)
end
- def has_word? chars
- current_key_string = ''
-
- while !chars.empty?
- current_key_string << chars.slice!(0)
- current_key = current_key_string.to_sym
- child = children_tree[current_key]
- return child.word? chars if child
- end
-
- false
- end
-
def current_key letter
- current_key_string = current_key = ''
+ current_key = nil
children_tree.keys.each do |key|
key_string = key.to_s
if key_string.start_with? letter
- current_key = key
- current_key_string = key_string
+ current_key = key_string
break
end
end
- [current_key, current_key_string]
+ current_key
end
end
end
end