lib/rambling/trie/branches.rb in rambling-trie-0.4.2 vs lib/rambling/trie/branches.rb in rambling-trie-0.5.0

- old
+ new

@@ -5,12 +5,12 @@ # Adds a branch to the current trie node based on the word # @param [String] word the word to add the branch from. # @return [Node] the just added branch's root node. # @raise [InvalidOperation] if the trie is already compressed. # @note This method clears the contents of the word variable. - def add_branch_from(word) - raise InvalidOperation.new('Cannot add branch to compressed trie') if compressed? + def add(word) + raise InvalidOperation, 'Cannot add branch to compressed trie' if compressed? if word.empty? @terminal = true return end @@ -24,59 +24,52 @@ else @children[first_letter] = Node.new word, self end end - # Alias for {#add_branch_from}. Defined instead of simple `alias_method` for overriding purposes. - # @param [String] word the word to add the branch from. - # @return [Node] the just added branch's root node. - # @raise [InvalidOperation] if the trie is already compressed. - # @see #add_branch_from - def <<(word) - add_branch_from word - end + alias_method :<<, :add protected - def uncompressed_has_branch_for?(chars) - chars.empty? or fulfills_uncompressed_condition?(:uncompressed_has_branch_for?, chars) + def branch_when_uncompressed?(chars) + chars.empty? or fulfills_uncompressed_condition?(:branch_when_uncompressed?, chars) end - def compressed_has_branch_for?(chars) + def branch_when_compressed?(chars) return true if chars.empty? first_letter = chars.slice! 0 current_key, current_key_string = current_key first_letter unless current_key.nil? - return @children[current_key].compressed_has_branch_for?(chars) if current_key_string.length == first_letter.length + return @children[current_key].branch_when_compressed?(chars) if current_key_string.length == first_letter.length while not chars.empty? char = chars.slice! 0 break unless current_key_string[first_letter.length] == char - first_letter += char return true if chars.empty? - return @children[current_key].compressed_has_branch_for?(chars) if current_key_string.length == first_letter.length + first_letter << char + return @children[current_key].branch_when_compressed?(chars) if current_key_string.length == first_letter.length end end false end - def uncompressed_is_word?(chars) - (chars.empty? and terminal?) or fulfills_uncompressed_condition?(:uncompressed_is_word?, chars) + def word_when_uncompressed?(chars) + (chars.empty? and terminal?) or fulfills_uncompressed_condition?(:word_when_uncompressed?, chars) end - def compressed_is_word?(chars) + def word_when_compressed?(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].compressed_is_word?(chars) if @children.has_key? key + return @children[key].word_when_compressed?(chars) if @children.has_key? key end false end