Skip to content

Commit

Permalink
Simplify current_key implementation to use less memory
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzedge committed Dec 26, 2016
1 parent 168218a commit 218fac2
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions lib/rambling/trie/compressed_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -67,49 +67,48 @@ 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

false
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
Expand Down

0 comments on commit 218fac2

Please sign in to comment.