[BioRuby] patch for ruby-cluster 1.4

GOTO Naohisa ngoto at gen-info.osaka-u.ac.jp
Tue Jun 28 09:59:26 EDT 2005


Hi,

I want to use ruby-cluster (interface for hierarchical clustering methods
for Ruby. http://bioruby.org/archive/contrib/ruby-cluster/ )
but it did not work well in Ruby 1.8.x.
So, I wrote a patch for ruby-cluster 1.4.

ChangeLog

 * Added rb_require("hcluster/hcluster") to work "require 'hcluster'"
   properly, as written in the README. Previously, we must have wrote
   "require 'hcluster/hcluster'".
 * Changed to define 'initialize' and 'alloc' methods insted of 'new'
   class method.
 * Changed to use rb_obj_is_kind_of().
 * Changed to use rb_const_get() to obtain HCluster::Tree class.

-- 
Naohisa GOTO
ngoto at gen-info.osaka-u.ac.jp
Department of Genome Informatics, Genome Information Research Center,
Research Institute for Microbial Diseases, Osaka University, Japan
-------------- next part --------------
--- ruby-cluster-1.4/ext/hclusterso/hcluster.c.ORIG	2002-12-04 08:19:53.000000000 +0900
+++ ruby-cluster-1.4/ext/hclusterso/hcluster.c	2005-06-27 20:25:48.459180000 +0900
@@ -41,15 +41,15 @@
 
 static VALUE mHCluster;
 static VALUE cRelation;
-static VALUE cTree;
 static ID id_new;
-static ID id_kind_of;
+static ID id_tree;
 
 static void
 free_rel (struct reldata *relp)
 {
   if (relp->rel)
     xfree (relp->rel);
+  xfree(relp);
 }
 
 static void
@@ -82,14 +82,20 @@
 }
 
 static VALUE
-Relation_new (int argc, VALUE *argv, VALUE klass)
+Relation_alloc (VALUE klass)
+{
+  struct reldata *relp = ALLOC(struct reldata);
+  return Data_Wrap_Struct(klass, 0, free_rel, relp);
+}
+
+static VALUE
+Relation_initialize (int argc, VALUE *argv, VALUE self)
 {
   VALUE vsize;
   VALUE vfill;
   int size;
   double fill;
   struct reldata *relp;
-  VALUE obj;
   
   rb_scan_args (argc, argv, "02", &vsize, &vfill);
   
@@ -104,10 +110,10 @@
   else
     fill = NUM2DBL (vfill);
 
-  obj = Data_Make_Struct (klass, struct reldata, 0, free_rel, relp);
+  Data_Get_Struct (self, struct reldata, relp);
   init_rel (relp, size, fill);
   
-  return obj;
+  return Qnil;
 }
 
 static VALUE
@@ -220,7 +226,7 @@
   
   vleft = create_tree (left, rel, tree, size);
   vright = create_tree (right, rel, tree, size);
-  return rb_funcall (cTree, id_new, 3,
+  return rb_funcall (rb_const_get(mHCluster, id_tree), id_new, 3,
 		     vleft, vright, rb_float_new (tmp));
 }
 
@@ -252,7 +258,7 @@
   while (num_member > 1)
     {
       int i1, i2;
-      int max_i1 = -1, max_i2;
+      int max_i1 = -1, max_i2 = -1;
       int max_m1, max_m2;
       int max_size1, max_size2;
       double max = -1.0 / 0.0;
@@ -290,7 +296,7 @@
 	      num_member, num_tree, max);
 #endif
       
-      if (max < threshold || max_i1 == -1)
+      if (max < threshold || max_i1 == -1 || max_i2 == -1)
 	break;
 
       max_m1 = member[max_i1];
@@ -397,7 +403,7 @@
   
   rb_scan_args (argc, argv, "11", &vrel, &vthr);
   
-  if (rb_funcall (vrel, id_kind_of, 1, cRelation) == Qfalse)
+  if (RTEST (rb_obj_is_kind_of (vrel, cRelation)) == 0)
     rb_raise (rb_eArgError, "the first argument must be Relation");
   
   if (NIL_P (vthr))
@@ -431,7 +437,7 @@
   
   rb_scan_args (argc, argv, "11", &vrel, &vthr);
   
-  if (rb_funcall (vrel, id_kind_of, 1, cRelation) == Qfalse)
+  if (RTEST (rb_obj_is_kind_of (vrel, cRelation)) == 0)
     rb_raise (rb_eArgError, "the first argument must be Relation");
   
   if (NIL_P (vthr))
@@ -459,7 +465,7 @@
   
   rb_scan_args (argc, argv, "11", &vrel, &vthr);
   
-  if (rb_funcall (vrel, id_kind_of, 1, cRelation) == Qfalse)
+  if (RTEST (rb_obj_is_kind_of (vrel, cRelation)) == 0)
     rb_raise (rb_eArgError, "the first argument must be Relation");
   
   if (NIL_P (vthr))
@@ -478,19 +484,19 @@
   mHCluster = rb_define_module ("HCluster");
   
   cRelation = rb_define_class_under (mHCluster, "Relation", rb_cObject);
-  rb_define_singleton_method (cRelation, "new", Relation_new, -1);
+  rb_define_alloc_func(cRelation, Relation_alloc);
+  rb_define_private_method (cRelation, "initialize", Relation_initialize, -1);
   rb_define_method (cRelation, "size", Relation_size, 0);
   rb_define_method (cRelation, "get", Relation_get, 2);
   rb_define_method (cRelation, "set", Relation_set, 3);
   rb_define_method (cRelation, "clear", Relation_clear, 2);
 
-  /* Dummy. If I knew a method to obtain a class object by name in C...  */
-  cTree = rb_define_class_under (mHCluster, "Tree", rb_cObject);
-  
   rb_define_module_function (mHCluster, "scluster", scluster, -1);
   rb_define_module_function (mHCluster, "ccluster", ccluster, -1);
   rb_define_module_function (mHCluster, "mcluster", mcluster, -1);
 
   id_new = rb_intern ("new");
-  id_kind_of = rb_intern ("kind_of?");
+  id_tree = rb_intern ("Tree");
+
+  rb_require("hcluster/hcluster");
 }


More information about the BioRuby mailing list