From 218fac218a77e70ba04a3672ff5abfddf6544f57 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 26 Dec 2016 13:51:59 -0500 Subject: [PATCH] Simplify current_key implementation to use less memory --- lib/rambling/trie/compressed_node.rb | 33 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/rambling/trie/compressed_node.rb b/lib/rambling/trie/compressed_node.rb index 731370f0..51483f7a 100644 --- a/lib/rambling/trie/compressed_node.rb +++ b/lib/rambling/trie/compressed_node.rb @@ -49,15 +49,15 @@ def closest_node chars self else 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].closest_node chars + if current_key.length == current_length || chars.empty? + return children_tree[current_key.to_sym].closest_node chars end - end while current_key_string[current_length] == chars.slice!(0) + end while current_key[current_length] == chars.slice!(0) Rambling::Trie::MissingNode.new end @@ -67,30 +67,30 @@ def closest_node chars def has_partial_word? 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.length == current_length || chars.empty? + return children_tree[current_key.to_sym].partial_word? chars end - end while current_key_string[current_length] == chars.slice!(0) + end while current_key[current_length] == chars.slice!(0) false end def has_word? chars - current_key_string = nil + current_key = nil while !chars.empty? - if current_key_string - current_key_string << chars.slice!(0) + if current_key + current_key << chars.slice!(0) else - current_key_string = chars.slice!(0) + current_key = chars.slice!(0) end - child = children_tree[current_key_string.to_sym] + child = children_tree[current_key.to_sym] return child.word? chars if child end @@ -98,18 +98,17 @@ def has_word? chars end def current_key letter - current_key_string = current_key = '' + current_key = '' 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