Skip to content

Commit

Permalink
Adding free routine to DupleIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchu committed Sep 21, 2011
1 parent 573f984 commit e53752a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
45 changes: 31 additions & 14 deletions ext/duple_index/DupleIndex.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,48 @@ VALUE method_alloc_index(VALUE self) {
return Data_Wrap_Struct(self, NULL, method_free_index, duples);
}

/* VALUE method_free_index(struct duple_hash *duples) {*/
/* printf("\n\n**** method_free_index\n");*/
static void method_free_index(void *duples) {
printf("\n\n**** method_free_index\n");
destroy_index( duples );
}

/* struct duple_hash *d, *d_tmp;*/
void destroy_index(struct duples_hash *duples) {
printf("\n\n**** destroy_index\n");

struct duples_hash *d, *d_tmp;

/* [> HASH_ITER(hh, duples, d, d_tmp) {<]*/
/* [> printf("Freeing hash id = %d\n", d->id);<]*/
HASH_ITER(hh, duples, d, d_tmp) {
printf("Freeing hash id = %d\n", d->id);

/* [> destroy_duple_pos(d->strings);<]*/
destroy_duple_pos(d->strings);

/* [> HASH_DEL(duples, d);<]*/
/* [> n_free_calls++;<]*/
/* [> free(d);<]*/
/* [> }<]*/
HASH_DEL(duples, d);
n_free_calls++;
free(d);
}

printf("malloc called %d times\n", n_malloc_calls);
printf("free called %d times\n", n_free_calls);
}

void destroy_duple_pos(struct duple_pos *head) {
struct duple_pos *c_pos, *n_pos;

c_pos = head;

while (1) {
n_pos = c_pos->next;
n_free_calls++;
free(c_pos);

/* printf("malloc called %d times\n", n_malloc_calls);*/
/* printf("free called %d times\n", n_free_calls);*/
if (n_pos == NULL) {
break;
} else {
c_pos = n_pos;
}
}
}

/* return Qnil;*/
/* }*/

VALUE method_add(VALUE self, VALUE r_str_id, VALUE r_str) {
printf("\n\n**** method_add\n");
Expand Down
4 changes: 3 additions & 1 deletion ext/duple_index/DupleIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ VALUE DupleIndex = Qnil;

void Init_duple_index();
VALUE method_alloc_index(VALUE self);
VALUE method_free_index(struct duple_hash *duples);
static void method_free_index(void *duples);
VALUE method_add(VALUE self, VALUE r_str_id, VALUE r_str);
VALUE method_query(VALUE self, VALUE r_a, VALUE r_b);

Expand All @@ -34,3 +34,5 @@ void add_duple(struct duples_hash *duples, int c_a, int c_b, int index, int pos)
struct duples_hash *duple_at(struct duples_hash *duples, int c_a, int c_b);
int duple_id(int c_a, int c_b);
struct duple_pos *create_duple_pos(int index, int pos, struct duple_pos *next, struct duple_pos *prev);
void destroy_index(struct duples_hash *duples);
void destroy_duple_pos(struct duple_pos *head);
Binary file modified ext/smith_waterman/smith_waterman.bundle
Binary file not shown.
27 changes: 27 additions & 0 deletions spec/lib/duple_index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe FuzzBall::DupleIndex do
before(:each) do
@index = FuzzBall::DupleIndex.new
end

describe "#add" do
it "should be able to add a string" do
lambda {
@index.add(0, [1, 2, 3]);
}.should_not raise_exception
end
end

describe "#query" do
before(:each) do
@index.add(0, [1, 2, 3]);
@index.add(1, [0, 1, 2]);
@index.add(2, [3, 4, 3, 4, 3, 4])
end

it "should tell you where a given duple is found" do
@index.query(1, 2).should == [[1, 1], [0, 0]]
end
end
end

0 comments on commit e53752a

Please sign in to comment.