Permalink
Browse files

removing stuff

  • Loading branch information...
tenderlove committed Dec 17, 2009
1 parent 2e2c5e1 commit cd3e395410077f2709e25aa26cdfea1a50e6bdf5
Showing with 30 additions and 55 deletions.
  1. +3 −5 ext/stree/stree.c
  2. +17 −34 ext/stree/stree_stringset.c
  3. +5 −5 test/test_stree.rb
  4. +5 −11 test/test_string_set.rb
View
@@ -20,13 +20,11 @@ static VALUE allocate(VALUE klass)
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_String * string = lst_string_new(
+ StringValuePtr(o), sizeof(char), RSTRING_LEN(o));
lst_stree_add_string(tree, string);
@@ -5,8 +5,7 @@ VALUE cStreeStringSet;
static void dealloc(void *ptr)
{
LST_StringSet *set = (LST_StringSet *)ptr;
-
- free(set);
+ lst_stringset_free(set);
}
static VALUE allocate(VALUE klass)
@@ -22,23 +21,16 @@ VALUE Stree_Wrap_StringSet(LST_StringSet * set)
return ss;
}
-static VALUE push(VALUE self, VALUE sstring)
+static VALUE push(VALUE self, VALUE string)
{
- if(cStreeString != rb_obj_class(sstring))
- rb_raise(rb_eArgError, "must be of type Stree::String");
-
- VALUE weakrefs = rb_iv_get(self, "@weak_refs");
+ LST_String * string_p = lst_string_new(
+ StringValuePtr(string), sizeof(char), RSTRING_LEN(string));
- LST_String * sstring_p;
LST_StringSet * ss;
- Data_Get_Struct(sstring, LST_String, sstring_p);
Data_Get_Struct(self, LST_StringSet, ss);
- VALUE key = INT2NUM((int)sstring_p);
- rb_hash_aset(weakrefs, key, sstring);
-
- lst_stringset_add(ss, sstring_p);
+ lst_stringset_add(ss, string_p);
return self;
}
@@ -51,38 +43,29 @@ static VALUE length(VALUE self)
return INT2NUM(ss->size);
}
-static VALUE delete(VALUE self, VALUE obj)
+static VALUE delete(VALUE self, VALUE string)
{
- if(cStreeString != rb_obj_class(obj))
- rb_raise(rb_eArgError, "must be of type Stree::String");
-
- LST_String * sstring_p;
LST_StringSet * ss;
+ LST_String * ss_string;
- Data_Get_Struct(obj, LST_String, sstring_p);
Data_Get_Struct(self, LST_StringSet, ss);
- int before = ss->size;
- lst_stringset_remove(ss, sstring_p);
+ for(ss_string = ss->members.lh_first; ss_string; ss_string = ss_string->set.le_next)
+ {
+ VALUE tmp_string = rb_str_new2(lst_string_print(ss_string));
+ if(Qtrue == rb_funcall(string, rb_intern("=="), 1, tmp_string)) {
+ LIST_REMOVE(ss_string, set);
+ ss->size--;
+ return string;
+ }
+ }
- if(ss->size != before) return obj;
return Qnil;
}
static void each_cb(LST_String * string, void * ctx)
{
- VALUE self = (VALUE)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))
- value = Data_Wrap_Struct(cStreeString, NULL, NULL, string);
-
- rb_yield(value);
+ rb_yield(rb_str_new2(lst_string_print(string)));
}
static VALUE each(VALUE self)
View
@@ -11,20 +11,20 @@ def test_new
end
def test_push_argerror
- assert_raises(ArgumentError) do
+ assert_raises(TypeError) do
@tree.push Object.new
end
end
def test_push
- @tree.push Stree::String.new('foo')
+ @tree.push 'foo'
end
def test_longest_common_substr
- @tree.push Stree::String.new('yokatta')
- @tree.push Stree::String.new('nemukatta')
+ @tree.push 'yokatta'
+ @tree.push 'nemukatta'
list = @tree.longest_common_substr(0, 10)
- assert_equal 'katta', list.first.to_s
+ assert_equal 'katta', list.first
end
end
View
@@ -8,37 +8,31 @@ def setup
end
def test_push_arg_error
- assert_raises(ArgumentError) do
+ assert_raises(TypeError) do
@ss << Object.new
end
end
def test_push
- @ss << Stree::String.new('foo')
+ @ss << 'foo'
assert_equal 1, @ss.length
end
def test_delete
- foo = Stree::String.new('foo')
+ foo = 'foo'
@ss << foo
assert_equal 1, @ss.length
assert_equal foo, @ss.delete(foo)
end
def test_delete_not_there
- foo = Stree::String.new('foo')
+ foo = 'foo'
assert_nil @ss.delete(foo)
end
- def test_delete_arg_error
- assert_raises(ArgumentError) do
- @ss.delete Object.new
- end
- end
-
def test_each
- foo = Stree::String.new('foo')
+ foo = 'foo'
@ss << foo
list = []
@ss.each do |thing|

0 comments on commit cd3e395

Please sign in to comment.