From 2abe2bda3ac7dc1f310bef7b094b94cd1df76bd9 Mon Sep 17 00:00:00 2001 From: alexgutteridge Date: Thu, 9 Aug 2007 05:55:08 +0000 Subject: [PATCH] Fixed GC problem. git-svn-id: http://igraph.rubyforge.org/svn/trunk@26 71f48855-0bbf-4aa5-930d-4df415e86613 --- ext/cIGraph.c | 32 +++++++++++++++++++++++++- ext/cIGraph.h | 40 +++++++++++++++++++++++++++++++++ ext/cIGraph_attribute_handler.c | 23 +++++++++++++++---- 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/ext/cIGraph.c b/ext/cIGraph.c index c0efbaa..42663c1 100644 --- a/ext/cIGraph.c +++ b/ext/cIGraph.c @@ -10,6 +10,11 @@ void cIGraph_free(void *p){ igraph_destroy(p); } +void cIGraph_mark(void *p){ + rb_gc_mark(((VALUE*)((igraph_t*)p)->attr)[0]); + rb_gc_mark(((VALUE*)((igraph_t*)p)->attr)[1]); +} + VALUE cIGraph_alloc(VALUE klass){ igraph_t *graph = malloc(sizeof(igraph_t)); @@ -17,7 +22,7 @@ VALUE cIGraph_alloc(VALUE klass){ igraph_empty(graph, 0, 1); - obj = Data_Wrap_Struct(klass, 0, cIGraph_free, graph); + obj = Data_Wrap_Struct(klass, cIGraph_mark, cIGraph_free, graph); return obj; @@ -228,4 +233,29 @@ void Init_igraph(){ rb_define_singleton_method(cIGraph, "read_graph_edgelist", cIGraph_read_graph_edgelist, 2); /* in cIGraph_file.c */ rb_define_method(cIGraph, "write_graph_edgelist", cIGraph_write_graph_edgelist, 1); /* in cIGraph_file.c */ + rb_define_method(cIGraph, "layout_random", cIGraph_layout_random, 0); + rb_define_method(cIGraph, "layout_circle", cIGraph_layout_circle, 0); + rb_define_method(cIGraph, "layout_fruchterman_reingold", cIGraph_layout_fruchterman_reingold, 6); + + //Matrix class + cIGraphMatrix = rb_define_class("IGraphMatrix", rb_cObject); + + rb_define_alloc_func(cIGraphMatrix, cIGraph_matrix_alloc); + rb_define_method(cIGraphMatrix, "initialize", cIGraph_matrix_initialize, -1); + rb_define_method(cIGraphMatrix, "initialize_copy", cIGraph_matrix_init_copy, 1); + //rb_define_singleton_method(cIGraphMatrix, "[]", cIGraph_matrix_initialize, -1); + rb_include_module(cIGraphMatrix, rb_mEnumerable); + rb_define_method (cIGraphMatrix, "each", cIGraph_matrix_each,0); + + rb_define_method(cIGraphMatrix, "[]", cIGraph_matrix_get, 2); + rb_define_method(cIGraphMatrix, "[]=", cIGraph_matrix_set, 3); + rb_define_method(cIGraphMatrix, "size", cIGraph_matrix_size, 0); + rb_define_method(cIGraphMatrix, "nrow", cIGraph_matrix_nrow, 0); + rb_define_method(cIGraphMatrix, "ncol", cIGraph_matrix_ncol, 0); + rb_define_method(cIGraphMatrix, "max", cIGraph_matrix_max, 0); + + rb_define_method(cIGraphMatrix, "*", cIGraph_matrix_multiply, 1); + + rb_define_method(cIGraphMatrix, "to_a", cIGraph_matrix_toa, 0); + } diff --git a/ext/cIGraph.h b/ext/cIGraph.h index f8d361a..3ae7de9 100644 --- a/ext/cIGraph.h +++ b/ext/cIGraph.h @@ -1,6 +1,7 @@ //Classes extern VALUE cIGraph; extern VALUE cIGraphError; +extern VALUE cIGraphMatrix; extern igraph_attribute_table_t cIGraph_attribute_table; //Error and warning handling functions @@ -18,6 +19,7 @@ VALUE cIGraph_include(VALUE self, VALUE v); //IGraph allocation, destruction and intialization void Init_igraph(void); void cIGraph_free(void *p); +void cIGraph_mark(void *p); VALUE cIGraph_alloc(VALUE klass); VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self); VALUE cIGraph_init_copy(VALUE copy, VALUE orig); @@ -82,6 +84,18 @@ VALUE cIGraph_topological_sorting(VALUE self, VALUE mode); VALUE cIGraph_read_graph_edgelist (VALUE self, VALUE file, VALUE mode); VALUE cIGraph_write_graph_edgelist(VALUE self, VALUE file); + +//Layouts +VALUE cIGraph_layout_random(VALUE self); +VALUE cIGraph_layout_circle(VALUE self); +VALUE cIGraph_layout_fruchterman_reingold(VALUE self, + VALUE niter, + VALUE maxdelta, + VALUE area, + VALUE coolexp, + VALUE repulserad, + VALUE use_seed); + //Attributes int cIGraph_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr); @@ -139,3 +153,29 @@ int cIGraph_get_string_edge_attr(const igraph_t *graph, const char *name, igraph_es_t es, igraph_strvector_t *value); + + +//Matrix functions +void cIGraph_matrix_free(void *p); +VALUE cIGraph_matrix_alloc(VALUE klass); +VALUE cIGraph_matrix_init_copy(VALUE copy, VALUE orig); +VALUE cIGraph_matrix_initialize(int argc, VALUE *argv, VALUE self); +void Init_igraphmatrix(); + +VALUE cIGraph_matrix_each(VALUE self); + +VALUE cIGraph_matrix_get (VALUE self, VALUE i, VALUE j); +VALUE cIGraph_matrix_set (VALUE self, VALUE i, VALUE j, VALUE x); +VALUE cIGraph_matrix_size(VALUE self); +VALUE cIGraph_matrix_nrow(VALUE self); +VALUE cIGraph_matrix_ncol(VALUE self); +VALUE cIGraph_matrix_max (VALUE self); + +VALUE cIGraph_matrix_multiply(VALUE self, VALUE x); + +VALUE cIGraph_matrix_toa(VALUE self); + +//Not implemented yet +//VALUE cIGraph_add_rows(VALUE self, VALUE n); +//VALUE cIGraph_add_cols(VALUE self, VALUE n); +//VALUE cIGraph_matrix_resize(VALUE self, VALUE nrow, VALUE ncol); diff --git a/ext/cIGraph_attribute_handler.c b/ext/cIGraph_attribute_handler.c index d25419e..2c362ab 100644 --- a/ext/cIGraph_attribute_handler.c +++ b/ext/cIGraph_attribute_handler.c @@ -68,7 +68,8 @@ int cIGraph_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr) { VALUE* attrs; - attrs = (VALUE*)calloc(2, sizeof(VALUE)); + //attrs = (VALUE*)calloc(2, sizeof(VALUE)); + attrs = ALLOC_N(VALUE, 2); if(!attrs) IGRAPH_ERROR("Error allocating Arrays\n", IGRAPH_ENOMEM); @@ -76,7 +77,7 @@ int cIGraph_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr) { //[0] is vertex array, [1] is edge array attrs[0] = rb_ary_new(); attrs[1] = rb_ary_new(); - + graph->attr = attrs; return IGRAPH_SUCCESS; @@ -91,10 +92,12 @@ void cIGraph_attribute_destroy(igraph_t *graph) { /* Copying */ int cIGraph_attribute_copy(igraph_t *to, const igraph_t *from) { + VALUE* attrs; VALUE vertex_array = ((VALUE*)from->attr)[0]; VALUE edge_array = ((VALUE*)from->attr)[1]; - VALUE* attrs = (VALUE*)calloc(2, sizeof(VALUE)); + //VALUE* attrs = (VALUE*)calloc(2, sizeof(VALUE)); + attrs = ALLOC_N(VALUE, 2); attrs[0] = rb_ary_dup(vertex_array); attrs[1] = rb_ary_dup(edge_array); @@ -182,7 +185,19 @@ void cIGraph_attribute_delete_edges(igraph_t *graph, const igraph_vector_t *idx) /* Permuting edges */ int cIGraph_attribute_permute_edges(igraph_t *graph, - const igraph_vector_t *idx) { return 0; + const igraph_vector_t *idx) { + + int i; + VALUE edge_array = ((VALUE*)graph->attr)[1]; + VALUE n_e_ary = rb_ary_new(); + + for(i=0;iattr)[1] = n_e_ary; + + return 0; } /* Getting attribute names and types */