lib/rambling/trie/compressor.rb in rambling-trie-0.9.3 vs lib/rambling/trie/compressor.rb in rambling-trie-1.0.0
- old
+ new
@@ -1,12 +1,12 @@
module Rambling
module Trie
- # Responsible for the compression process of a Trie data structure.
+ # Responsible for the compression process of a trie data structure.
class Compressor
- # Compresses a node from a Trie data structure.
- # @param [RawNode] node the node to compress
- # @return [CompressedNode] node the compressed version of the node
+ # Compresses a {Node Node} from a trie data structure.
+ # @param [RawNode] node the node to compress.
+ # @return [CompressedNode] node the compressed version of the node.
def compress node
if node.compressable?
merge_with_child_and_compress node
else
copy_node_and_compress_children node
@@ -16,29 +16,33 @@
private
def merge_with_child_and_compress node
child = node.children.first
- new_node = Rambling::Trie::CompressedNode.new node.parent
- new_node.letter = node.letter.to_s << child.letter.to_s
- new_node.terminal! if child.terminal?
+ letter = node.letter.to_s << child.letter.to_s
+ new_node = new_compressed_node node, letter, child.terminal?
new_node.children_tree = child.children_tree
compress new_node
end
def copy_node_and_compress_children node
- new_node = Rambling::Trie::CompressedNode.new node.parent
- new_node.letter = node.letter
- new_node.terminal! if node.terminal?
+ new_node = new_compressed_node node, node.letter, node.terminal?
node.children.each do |child|
compressed_child = compress child
compressed_child.parent = new_node
new_node[compressed_child.letter] = compressed_child
end
+ new_node
+ end
+
+ def new_compressed_node node, letter, terminal
+ new_node = Rambling::Trie::CompressedNode.new node.parent
+ new_node.letter = letter
+ new_node.terminal! if terminal
new_node
end
end
end
end