lib/rambling-trie/branches.rb in rambling-trie-0.4.0 vs lib/rambling-trie/branches.rb in rambling-trie-0.4.1
- old
+ new
@@ -7,86 +7,95 @@
# @return [Node] the just added branch's root node.
# @raise [InvalidOperation] if the trie is already compressed.
def add_branch_from(word)
raise InvalidOperation.new('Cannot add branch to compressed trie') if compressed?
if word.empty?
- @is_terminal = true
+ @terminal = true
return
end
first_letter = word.slice(0).to_sym
- if @children.has_key?(first_letter)
- word.slice!(0)
+ if @children.has_key? first_letter
+ word.slice! 0
child = @children[first_letter]
- child.add_branch_from(word)
+ child << word
child
else
@children[first_letter] = Node.new word, self
end
end
+ alias_method :<<, :add_branch_from
+
protected
- def has_uncompressed_branch_for?(chars)
- chars.empty? or fulfills_uncompressed_condition?(:has_uncompressed_branch_for?, chars)
+ def uncompressed_has_branch_for?(chars)
+ chars.empty? or fulfills_uncompressed_condition?(:uncompressed_has_branch_for?, chars)
end
- def has_compressed_branch_for?(chars)
+ def compressed_has_branch_for?(chars)
return true if chars.empty?
- first_letter = chars.slice!(0)
- key = nil
- @children.keys.each do |x|
- x = x.to_s
- if x.start_with?(first_letter)
- key = x
- break
- end
- end
+ first_letter = chars.slice! 0
+ current_key, current_key_string = current_key first_letter
- unless key.nil?
- sym_key = key.to_sym
- return @children[sym_key].has_compressed_branch_for?(chars) if key.length == first_letter.length
+ unless current_key.nil?
+ return @children[current_key].compressed_has_branch_for?(chars) if current_key_string.length == first_letter.length
while not chars.empty?
- char = chars.slice!(0)
+ char = chars.slice! 0
- break unless key[first_letter.length] == char
+ break unless current_key_string[first_letter.length] == char
first_letter += char
return true if chars.empty?
- return @children[sym_key].has_compressed_branch_for?(chars) if key.length == first_letter.length
+ return @children[current_key].compressed_has_branch_for?(chars) if current_key_string.length == first_letter.length
end
end
false
end
- def is_uncompressed_word?(chars)
- (chars.empty? and terminal?) or fulfills_uncompressed_condition?(:is_uncompressed_word?, chars)
+ def uncompressed_is_word?(chars)
+ (chars.empty? and terminal?) or fulfills_uncompressed_condition?(:uncompressed_is_word?, chars)
end
- def is_compressed_word?(chars)
+ def compressed_is_word?(chars)
return true if chars.empty? and terminal?
first_letter = ''
while not chars.empty?
- first_letter += chars.slice!(0)
+ first_letter += chars.slice! 0
key = first_letter.to_sym
- return @children[key].is_compressed_word?(chars) if @children.has_key?(key)
+ return @children[key].compressed_is_word?(chars) if @children.has_key? key
end
false
end
private
+ def current_key(letter)
+ current_key_string = current_key = nil
+
+ @children.keys.each do |key|
+ key_string = key.to_s
+ if key_string.start_with? letter
+ current_key = key
+ current_key_string = key_string
+ break
+ end
+ end
+
+ [current_key, current_key_string]
+ end
+
def fulfills_uncompressed_condition?(method, chars)
- first_letter = chars.slice!(0)
+ first_letter = chars.slice! 0
unless first_letter.nil?
first_letter_sym = first_letter.to_sym
- return @children[first_letter_sym].send(method, chars) if @children.has_key?(first_letter_sym)
+ return @children[first_letter_sym].send(method, chars) if @children.has_key? first_letter_sym
end
false
end
end