Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding push and longest common substr to Stree

  • Loading branch information...
commit 2e2c5e1d6e94bced4a461b2499413fe0ca81758e 1 parent b003997
@tenderlove authored
View
48 ext/stree/stree.c
@@ -2,9 +2,57 @@
VALUE cStree;
+static void dealloc(void *ptr)
+{
+ // FIXME: Strings don't seem to be duped when adding to the tree, so we
+ // need to figure out a tree free strategy. We'll just leak for now.
+ //lst_stree_free((LST_STree *)ptr);
+}
+
+static VALUE allocate(VALUE klass)
+{
+ LST_STree * ptr;
+ VALUE rb = Data_Make_Struct(klass, LST_STree, NULL, dealloc, ptr);
+ lst_stree_init(ptr);
+ return rb;
+}
+
+static VALUE push(VALUE self, VALUE o)
+{
+ LST_STree * tree;
+ LST_String * string;
+
+ if(cStreeString != rb_obj_class(o))
+ rb_raise(rb_eArgError, "must be of type Stree::String");
+
+ Data_Get_Struct(self, LST_STree, tree);
+ Data_Get_Struct(o, LST_String, string);
+
+ lst_stree_add_string(tree, string);
+
+ return self;
+}
+
+static VALUE longest_common_substr(VALUE self, VALUE min_len, VALUE max_len)
+{
+ LST_STree * ptr;
+
+ Data_Get_Struct(self, LST_STree, ptr);
+
+ LST_StringSet * set =
+ lst_alg_longest_common_substring(ptr, NUM2INT(min_len), NUM2INT(max_len));
+
+ return Stree_Wrap_StringSet(set);
+}
+
void Init_stree()
{
cStree = rb_define_class("Stree", rb_cObject);
+
+ rb_define_alloc_func(cStree, allocate);
+ rb_define_method(cStree, "push", push, 1);
+ rb_define_method(cStree, "longest_common_substr", longest_common_substr, 2);
+
Init_stree_string();
Init_stree_stringset();
}
View
6 ext/stree/stree.h
@@ -2,11 +2,13 @@
#define RUBY_STREE
#include <ruby.h>
+#include <stree/lst_string.h>
+#include <stree/lst_stree.h>
+#include <stree/lst_algorithms.h>
+
#include <stree_string.h>
#include <stree_stringset.h>
-#include <stree/lst_string.h>
-
extern VALUE cStree;
#endif
View
17 ext/stree/stree_stringset.c
@@ -15,6 +15,13 @@ static VALUE allocate(VALUE klass)
return Data_Wrap_Struct(klass, NULL, dealloc, ss);
}
+VALUE Stree_Wrap_StringSet(LST_StringSet * set)
+{
+ VALUE ss = Data_Wrap_Struct(cStreeStringSet, NULL, dealloc, set);
+ rb_obj_call_init(ss, 0, NULL);
+ return ss;
+}
+
static VALUE push(VALUE self, VALUE sstring)
{
if(cStreeString != rb_obj_class(sstring))
@@ -69,10 +76,13 @@ static void each_cb(LST_String * string, void * ctx)
VALUE weakrefs = rb_iv_get(self, "@weak_refs");
VALUE key = INT2NUM((int)string);
VALUE value = rb_hash_aref(weakrefs, key);
+
+ // If this wasn't in a weak ref table, we'll assume that some other Ruby
+ // object created it and is in charge of freeing it's memory.
if(NIL_P(value))
- rb_raise(rb_eRuntimeError, "fixme!");
- else
- rb_yield(value);
+ value = Data_Wrap_Struct(cStreeString, NULL, NULL, string);
+
+ rb_yield(value);
}
static VALUE each(VALUE self)
@@ -91,7 +101,6 @@ void Init_stree_stringset()
rb_define_method(cStreeStringSet, "push", push, 1);
rb_define_method(cStreeStringSet, "length", length, 0);
- rb_define_method(cStreeStringSet, "push", push, 1);
rb_define_method(cStreeStringSet, "delete", delete, 1);
rb_define_method(cStreeStringSet, "each", each, 0);
}
View
1  ext/stree/stree_stringset.h
@@ -4,5 +4,6 @@
#include <stree.h>
void Init_stree_stringset();
+VALUE Stree_Wrap_StringSet(LST_StringSet * set);
#endif
View
2  lib/stree/string_set.rb
@@ -1,5 +1,7 @@
class Stree
class StringSet
+ include Enumerable
+
def initialize
@weak_refs = {}
end
View
22 test/test_stree.rb
@@ -2,7 +2,29 @@
require "stree"
class TestStree < Test::Unit::TestCase
+ def setup
+ @tree = Stree.new
+ end
+
def test_new
assert Stree.new
end
+
+ def test_push_argerror
+ assert_raises(ArgumentError) do
+ @tree.push Object.new
+ end
+ end
+
+ def test_push
+ @tree.push Stree::String.new('foo')
+ end
+
+ def test_longest_common_substr
+ @tree.push Stree::String.new('yokatta')
+ @tree.push Stree::String.new('nemukatta')
+
+ list = @tree.longest_common_substr(0, 10)
+ assert_equal 'katta', list.first.to_s
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.