Hi,

At Mon, 9 Nov 2009 06:41:57 +0900,
James Gray wrote in [ruby-core:26635]:
> RBTree has now been fixed to run on Ruby 1.9:
> 
> http://github.com/skade/rbtree

It can't compile with non-gcc, or with gcc and $DEBUG.


diff --git a/extconf.rb b/extconf.rb index 272790b..02f2e8e 100644 --- a/extconf.rb +++ b/extconf.rb @@ -2,5 +2,7 @@ require 'mkmf' if $DEBUG - $CFLAGS << ' -std=c89 -pedantic -Wall -Wno-long-long' + if CONFIG['GCC'] == 'yes' + $CFLAGS << ' -std=c89 -pedantic -Wno-long-long' + end $defs << ' -Dinline=__inline' else @@ -8,4 +10,4 @@ else end -have_func('rb_enumeratorize') +have_func('rb_exec_recursive', 'ruby.h') create_makefile('rbtree') diff --git a/rbtree.c b/rbtree.c index 9f19613..08bde65 100644 --- a/rbtree.c +++ b/rbtree.c @@ -12,8 +12,15 @@ #define HASH_PROC_DEFAULT FL_USER2 -#ifndef HAVE_RB_ENUMERATORIZE +#ifndef RETURN_ENUMERATOR #define RETURN_ENUMERATOR(obj, argc, argv) ((void)0) #endif +#ifndef RHASH_TBL +#define RHASH_TBL(h) RHASH(h)->tbl +#endif +#ifndef RHASH_IFNONE +#define RHASH_IFNONE(h) RHASH(h)->ifnone +#endif + VALUE RBTree; VALUE MultiRBTree; @@ -428,5 +435,5 @@ static int value_eq(const void* key1, const void* key2) { - return rb_equal((VALUE)key1, (VALUE)key2); + return rb_equal((VALUE)key1, (VALUE)key2) != 0; } @@ -1077,5 +1084,5 @@ rbtree_to_hash(VALUE self) hash = rb_hash_new(); rbtree_for_each(self, to_hash_i, (void*)hash); - RHASH(hash)->ifnone = IFNONE(self); + RHASH_IFNONE(hash) = IFNONE(self); if (FL_TEST(self, RBTREE_PROC_DEFAULT)) FL_SET(hash, HASH_PROC_DEFAULT); @@ -1097,7 +1104,6 @@ rbtree_begin_inspect(VALUE self) { const char* c = rb_class2name(CLASS_OF(self)); - char str [strlen(c) + 5]; - sprintf(str, "#<%s: ", c); - VALUE rb_str = rb_str_new2(str); + VALUE rb_str = rb_str_new(0, strlen(c) + 4); + sprintf(RSTRING_PTR(rb_str), "#<%s: ", c); return rb_str; } @@ -1109,4 +1115,5 @@ to_s_rbtree(VALUE self, VALUE nil) } +#ifdef HAVE_RB_EXEC_RECURSIVE VALUE rbtree_to_s_recursive(VALUE self, VALUE arg, int recursive) @@ -1116,4 +1123,5 @@ rbtree_to_s_recursive(VALUE self, VALUE arg, int recursive) return to_s_rbtree(self, Qnil); } +#endif /* @@ -1123,8 +1131,11 @@ VALUE rbtree_to_s(VALUE self) { +#ifdef HAVE_RB_EXEC_RECURSIVE return rb_exec_recursive(rbtree_to_s_recursive, self, Qnil); - //if (rb_inspecting_p(self)) - // return rb_str_cat2(rbtree_begin_inspect(self), "...>"); - //return rb_protect_inspect(to_s_rbtree, self, Qnil); +#else + if (rb_inspecting_p(self)) + return rb_str_cat2(rbtree_begin_inspect(self), "...>"); + return rb_protect_inspect(to_s_rbtree, self, Qnil); +#endif } @@ -1194,8 +1205,12 @@ VALUE rbtree_inspect(VALUE self) { - /*VALUE str = rbtree_begin_inspect(self); - if (rb_inspecting_p(self)) - return rb_str_cat2(str, "...>");*/ +#ifdef HAVE_RB_EXEC_RECURSIVE return rb_exec_recursive(rbtree_inspect_recursive, self, Qnil); +#else + VALUE str = rbtree_begin_inspect(self); + if (rb_inspecting_p(self)) + return rb_str_cat2(str, "...>"); + return rb_protect_inspect(inspect_rbtree, self, str); +#endif } diff --git a/test.rb b/test.rb index 8c533b8..32fdd25 100644 --- a/test.rb +++ b/test.rb @@ -136,5 +136,5 @@ class RBTreeTest < Test::Unit::TestCase assert_raises(ArgumentError) { rbtree.default("e", "f") } - rbtree = RBTree.new {|rbtree, key| @rbtree[key || "c"] } + rbtree = RBTree.new {|tree, key| @rbtree[key || "c"] } assert_equal("C", rbtree.default(nil)) assert_equal("B", rbtree.default("b")) @@ -182,5 +182,5 @@ class RBTreeTest < Test::Unit::TestCase a = RBTree.new b = RBTree.new - a.readjust {|a, b| a <=> b } + a.readjust {|x, y| x <=> y } assert_not_equal(a, b) b.readjust(a.cmp_proc) @@ -198,5 +198,14 @@ class RBTreeTest < Test::Unit::TestCase assert_equal("E", @rbtree.fetch("e", "E")) assert_equal("E", @rbtree.fetch("e") { "E" }) + class << (stderr = "") + alias write << + end + $stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE + begin assert_equal("E", @rbtree.fetch("e", "F") { "E" }) + ensure + $stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE + end + assert_match(/warning: block supersedes default value argument/, stderr) assert_raises(ArgumentError) { @rbtree.fetch } @@ -535,5 +544,5 @@ class RBTreeTest < Test::Unit::TestCase assert_equal(%({"a"=>"A", "b"=>"B", "c"=>"C", "d"=>"D"}), tree) assert_equal(%("e"), default) - assert_match(/#<Proc:\w+(@test.rb:\d+)?>/, cmp_proc) + assert_match(/#<Proc:\w+(@#{__FILE__}:\d+)?>/o, cmp_proc) rbtree = RBTree.new
-- Nobu Nakada