lib/rambling/trie/root.rb in rambling-trie-0.4.2 vs lib/rambling/trie/root.rb in rambling-trie-0.5.0
- old
+ new
@@ -1,23 +1,21 @@
module Rambling
module Trie
# A representation of the root node in the Trie data structure.
class Root < Node
# Creates a new Trie.
- # @param [String, nil] filename the file to load the words from (defaults to nil).
- def initialize(filename = nil)
- super(nil)
-
- @filename = filename
+ # @yield [Root] the trie just created.
+ def initialize
+ super
@compressed = false
- add_all_nodes if filename
+ yield self if block_given?
end
# Compresses the existing tree using redundant node elimination. Flags the trie as compressed.
- # @return [Root] same object
+ # @return [Root] self
def compress!
- @compressed = (not compress_tree!.nil?) unless compressed?
+ @compressed = (compressed? or not compress_tree!.nil?)
self
end
# Flag for compressed tries. Overrides {Compressor#compressed?}.
# @return [Boolean] `true` for compressed tries, `false` otherwise.
@@ -26,40 +24,40 @@
end
# Checks if a path for a word or partial word exists in the trie.
# @param [String] word the word or partial word to look for in the trie.
# @return [Boolean] `true` if the word or partial word is found, `false` otherwise.
- def has_branch_for?(word = '')
- fulfills_condition word, :has_branch_for?
+ def branch?(word = '')
+ fulfills_condition? word, :branch?
end
# Checks if a whole word exists in the trie.
# @param [String] word the word to look for in the trie.
# @return [Boolean] `true` only if the word is found and the last character corresponds to a terminal node.
- def is_word?(word = '')
- fulfills_condition word, :is_word?
+ def word?(word = '')
+ fulfills_condition? word, :word?
end
- alias_method :include?, :is_word?
+ alias_method :include?, :word?
# Adds a branch to the trie based on the word, without changing the passed 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.
- # @see Branches#add_branch_from
+ # @see Branches#add
# @note Avoids clearing the contents of the word variable.
- def add_branch_from(word)
+ def add(word)
super word.clone
end
+ alias_method :<<, :add
+
private
- def fulfills_condition(word, method)
- method = compressed? ? "compressed_#{method}" : "uncompressed_#{method}"
- send(method, word.chars.to_a)
- end
- def add_all_nodes
- File.open(@filename).each_line { |line| self << line.chomp }
+ def fulfills_condition?(word, method)
+ method = method.to_s.slice 0...(method.length - 1)
+ method = compressed? ? "#{method}_when_compressed?" : "#{method}_when_uncompressed?"
+ send method, word.chars.to_a
end
end
end
end