Browse files

adding an iterator to StringSet

  • Loading branch information...
1 parent 3d99c47 commit b0039978b6ec3144420fb54507b3d264cac520b2 @tenderlove committed Dec 16, 2009
Showing with 34 additions and 0 deletions.
  1. +23 −0 ext/stree/stree_stringset.c
  2. +11 −0 test/test_string_set.rb
View
23 ext/stree/stree_stringset.c
@@ -62,6 +62,28 @@ static VALUE delete(VALUE self, VALUE 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(NIL_P(value))
+ rb_raise(rb_eRuntimeError, "fixme!");
+ else
+ rb_yield(value);
+}
+
+static VALUE each(VALUE self)
+{
+ LST_StringSet * ss;
+
+ Data_Get_Struct(self, LST_StringSet, ss);
+ lst_stringset_foreach(ss, each_cb, (void *)self);
+ return self;
+}
+
void Init_stree_stringset()
{
cStreeStringSet = rb_define_class_under(cStree, "StringSet", rb_cObject);
@@ -71,4 +93,5 @@ void Init_stree_stringset()
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
11 test/test_string_set.rb
@@ -36,5 +36,16 @@ def test_delete_arg_error
@ss.delete Object.new
end
end
+
+ def test_each
+ foo = Stree::String.new('foo')
+ @ss << foo
+ list = []
+ @ss.each do |thing|
+ list << thing
+ end
+
+ assert_equal([foo], list)
+ end
end
end

0 comments on commit b003997

Please sign in to comment.