Skip to content

Fixed memory leaks in children, children_with_values, has_children?. #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions VERSION.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
---
:major: 0
:minor: 5
:patch: 1
:patch: 2
:build:
57 changes: 11 additions & 46 deletions ext/trie/trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ static VALUE walk_all_paths(Trie *trie, VALUE children, TrieState *state, char *
char *word = (char*) malloc(prefix_size + 2);
memcpy(word, prefix, prefix_size + 2);
rb_ary_push(children, rb_str_new2(word));
free(word);
}

walk_all_paths(trie, children, next_state, prefix, prefix_size + 1);

prefix[prefix_size] = 0;
trie_state_free(next_state);
}
Expand Down Expand Up @@ -211,17 +212,20 @@ static VALUE rb_trie_children(VALUE self, VALUE prefix) {
int prefix_size = RSTRING_LEN(prefix);
TrieState *state = trie_root(trie);
VALUE children = rb_ary_new();

TrieChar *char_prefix = (TrieChar*)RSTRING_PTR(prefix);

if(!traverse(state, char_prefix)) {
trie_state_free(state);
return children;
}

if(trie_state_is_terminal(state))
rb_ary_push(children, prefix);

char prefix_buffer[1024];
memcpy(prefix_buffer, char_prefix, prefix_size);

prefix_buffer[prefix_size] = 0;

walk_all_paths(trie, children, state, prefix_buffer, prefix_size);
Expand All @@ -230,34 +234,6 @@ static VALUE rb_trie_children(VALUE self, VALUE prefix) {
return children;
}

static Bool walk_all_paths_until_first_terminal(Trie *trie, TrieState *state, char *prefix, int prefix_size) {
int c;
Bool ret = FALSE;
for(c = 1; c < 256; c++) {
if(trie_state_is_walkable(state,c)) {
TrieState *next_state = trie_state_clone(state);
trie_state_walk(next_state, c);

prefix[prefix_size] = c;
prefix[prefix_size + 1] = 0;

if(trie_state_is_terminal(next_state)) {
return TRUE;
}

ret = walk_all_paths_until_first_terminal(trie, next_state, prefix, prefix_size + 1);

prefix[prefix_size] = 0;
trie_state_free(next_state);

if (ret == TRUE) {
return ret;
}
}
}

return ret;
}

static VALUE rb_trie_has_children(VALUE self, VALUE prefix) {
if(NIL_P(prefix))
Expand All @@ -272,21 +248,9 @@ static VALUE rb_trie_has_children(VALUE self, VALUE prefix) {
TrieState *state = trie_root(trie);
TrieChar *char_prefix = (TrieChar*)RSTRING_PTR(prefix);

if(!traverse(state, char_prefix)) {
return Qfalse;
}

if(trie_state_is_terminal(state))
return Qtrue;

char prefix_buffer[1024];
memcpy(prefix_buffer, char_prefix, prefix_size);
prefix_buffer[prefix_size] = 0;

Bool ret = walk_all_paths_until_first_terminal(trie, state, prefix_buffer, prefix_size);

trie_state_free(state);
return ret == TRUE ? Qtrue : Qfalse;
Bool ret = traverse(state, char_prefix);
trie_state_free(state);
return ret == TRUE ? Qtrue : Qfalse;
}

static VALUE walk_all_paths_with_values(Trie *trie, VALUE children, TrieState *state, char *prefix, int prefix_size) {
Expand All @@ -308,6 +272,7 @@ static VALUE walk_all_paths_with_values(Trie *trie, VALUE children, TrieState *s

VALUE tuple = rb_ary_new();
rb_ary_push(tuple, rb_str_new2(word));
free(word);

TrieData trie_data = trie_state_get_data(end_state);
rb_ary_push(tuple, (VALUE)trie_data);
Expand Down
6 changes: 3 additions & 3 deletions fast_trie.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: fast_trie 0.5.1 ruby ext
# stub: fast_trie 0.5.2 ruby ext
# stub: ext/trie/extconf.rb

Gem::Specification.new do |s|
s.name = "fast_trie"
s.version = "0.5.1"
s.version = "0.5.2"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.require_paths = ["ext"]
s.authors = ["Tyler McMullen", "Matt Hickford"]
s.date = "2015-07-27"
s.date = "2015-11-09"
s.description = "Ruby Trie based on libdatrie."
s.email = "[email protected]"
s.extensions = ["ext/trie/extconf.rb"]
Expand Down
25 changes: 25 additions & 0 deletions spec/trie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@
children.should include('rocket')
end

it 'returns all children by first character' do
children = @trie.children('r')
children.size.should == 2
children.should include('rock')
children.should include('rocket')

children = @trie.children('f')
children.size.should == 1
children.should include('frederico')
end

it 'returns blank array if prefix does not exist' do
@trie.children('ajsodij').should == []
end
Expand Down Expand Up @@ -176,12 +187,26 @@

@trie.has_children?('rock').should be_true
@trie.has_children?('rocket').should be_true

@trie.has_children?('f').should be_true
@trie.has_children?('fred').should be_true
end

it 'returns false when there are no children matching prefix' do
@trie.has_children?('no').should be_false
@trie.has_children?('rome').should be_false
@trie.has_children?('roc_').should be_false
@trie.has_children?('rocketttt').should be_false
end

it 'handles keys with values' do
@trie.add('value1', 1)
@trie.add('value2', 2)

@trie.has_children?('value1').should be_true
@trie.has_children?('value2').should be_true
@trie.has_children?('value').should be_true
@trie.has_children?('volue').should be_false
end
end
end
Expand Down