Adding correct attribute handling.

git-svn-id: http://igraph.rubyforge.org/svn/trunk@20 71f48855-0bbf-4aa5-930d-4df415e86613
This commit is contained in:
alexgutteridge 2007-08-04 05:26:01 +00:00
parent c6f58349bf
commit 633f21c728
5 changed files with 291 additions and 59 deletions

View File

@ -16,6 +16,7 @@ VALUE cIGraph_alloc(VALUE klass){
VALUE obj;
igraph_empty(graph, 0, 1);
obj = Data_Wrap_Struct(klass, 0, cIGraph_free, graph);
return obj;
@ -56,11 +57,12 @@ VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){
int current_vertex_id;
int i;
igraph_vector_ptr_t vertex_attr;
rb_scan_args(argc,argv,"12", &edges, &directed, &attrs);
igraph_set_error_handler(cIGraph_error_handler);
igraph_set_error_handler(cIGraph_error_handler);
igraph_set_warning_handler(cIGraph_warning_handler);
//igraph_i_set_attribute_table(&cIGraph_attribute_table);
//New hash for mapping vertex objects to floats used by iGraph
object_h = rb_iv_set(self,"@object_ids",rb_hash_new());
@ -70,8 +72,10 @@ VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){
//Initialize edge vector
igraph_vector_init_int(&edge_v,0);
igraph_vector_ptr_init(&vertex_attr,0);
Data_Get_Struct(self, igraph_t, graph);
if(!directed)
igraph_to_undirected(graph,IGRAPH_TO_UNDIRECTED_COLLAPSE);
@ -82,30 +86,35 @@ VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){
//If @vertices includes this vertex then look up the vertex number
current_vertex_id = NUM2INT(rb_hash_aref(object_h,vertex));
} else {
//otherwise add a new entry to Hash
//otherwise add a new entadd_vertry to Hash
rb_hash_aset(object_h,vertex,INT2NUM(vertex_n));
rb_hash_aset(id_h, INT2NUM(vertex_n),vertex);
current_vertex_id = vertex_n;
igraph_vector_ptr_push_back(&vertex_attr,(void*)RARRAY(edges)->ptr[i]);
vertex_n++;
}
igraph_vector_push_back(&edge_v,current_vertex_id);
}
if(igraph_vector_size(&edge_v) > 0){
igraph_add_vertices(graph,vertex_n,0);
igraph_add_vertices(graph,vertex_n,&vertex_attr);
igraph_add_edges(graph,&edge_v,0);
}
if(attrs != Qnil){
for (i=0; i<RARRAY(attrs)->len; i++) {
cIGraph_set_edge_attr(self,
RARRAY(edges)->ptr[i*2],
RARRAY(edges)->ptr[(i*2)+1],
RARRAY(attrs)->ptr[i]);
}
}
//if(attrs != Qnil){
//for (i=0; i<RARRAY(attrs)->len; i++) {
// cIGraph_set_edge_attr(self,
// RARRAY(edges)->ptr[i*2],
// RARRAY(edges)->ptr[(i*2)+1],
// RARRAY(attrs)->ptr[i]);
//}
//}
igraph_vector_destroy(&edge_v);
igraph_vector_ptr_destroy(&vertex_attr);
return self;
@ -117,16 +126,17 @@ VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){
*/
void Init_igraph(){
cIGraph = rb_define_class("IGraph", rb_cObject);
cIGraphError = rb_define_class("IGraphError", rb_eRuntimeError);
rb_define_alloc_func(cIGraph, cIGraph_alloc);
rb_define_method(cIGraph, "initialize", cIGraph_initialize, -1);
rb_define_method(cIGraph, "[]", cIGraph_get_edge_attr, 2);
rb_define_method(cIGraph, "[]=", cIGraph_set_edge_attr, 3);
rb_define_method(cIGraph, "get_edge_attr", cIGraph_get_edge_attr, 2);
rb_define_method(cIGraph, "set_edge_attr", cIGraph_set_edge_attr, 3);
//rb_define_method(cIGraph, "[]", cIGraph_get_edge_attr, 2);
//rb_define_method(cIGraph, "[]=", cIGraph_set_edge_attr, 3);
//rb_define_method(cIGraph, "get_edge_attr", cIGraph_get_edge_attr, 2);
//rb_define_method(cIGraph, "set_edge_attr", cIGraph_set_edge_attr, 3);
rb_define_method(cIGraph, "each_vertex", cIGraph_each_vertex, 0); /* in cIGraph_iterators.c */
rb_define_method(cIGraph, "each_edge", cIGraph_each_edge, 1); /* in cIGraph_iterators.c */
@ -177,6 +187,8 @@ void Init_igraph(){
rb_define_method(cIGraph, "add_edge", cIGraph_add_edge, -1); /* in cIGraph_add_delete.c */
rb_define_method(cIGraph, "add_vertex", cIGraph_add_vertex, 1); /* in cIGraph_add_delete.c */
rb_define_method(cIGraph, "delete_edge", cIGraph_delete_edge, 2); /* in cIGraph_add_delete.c */
rb_define_method(cIGraph, "are_connected", cIGraph_are_connected,2);
rb_define_method(cIGraph, "are_connected?", cIGraph_are_connected,2); /* in cIGraph_basic_properties.c */
@ -193,4 +205,13 @@ void Init_igraph(){
rb_define_method(cIGraph, "neighbourhood", cIGraph_neighborhood, 3); /* in cIGraph_vertex_neighbourhood.c */
rb_define_method(cIGraph, "neighborhood", cIGraph_neighborhood, 3);
rb_define_method(cIGraph, "neighbourhood_graphs", cIGraph_neighborhood_graphs, 3); /* in cIGraph_vertex_neighbourhood.c */
rb_define_method(cIGraph, "neighborhood_graphs", cIGraph_neighborhood_graphs, 3);}
rb_define_method(cIGraph, "neighborhood_graphs", cIGraph_neighborhood_graphs, 3);
rb_define_method(cIGraph, "topological_sorting", cIGraph_topological_sorting, 1); /* in cIGraph_topological_sort.c */
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 */
igraph_i_set_attribute_table(&cIGraph_attribute_table);
}

View File

@ -1,6 +1,7 @@
//Classes
extern VALUE cIGraph;
extern VALUE cIGraphError;
extern igraph_attribute_table_t cIGraph_attribute_table;
//Error and warning handling functions
void cIGraph_error_handler(const char *reason, const char *file,
@ -22,8 +23,8 @@ VALUE cIGraph_alloc(VALUE klass);
VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self);
//Attribute accessors
VALUE cIGraph_get_edge_attr(VALUE self, VALUE from, VALUE to);
VALUE cIGraph_set_edge_attr(VALUE self, VALUE from, VALUE to, VALUE attr);
//VALUE cIGraph_get_edge_attr(VALUE self, VALUE from, VALUE to);
//VALUE cIGraph_set_edge_attr(VALUE self, VALUE from, VALUE to, VALUE attr);
//Iterators
VALUE cIGraph_each_vertex (VALUE self);
@ -53,6 +54,7 @@ VALUE cIGraph_degree (VALUE self, VALUE v, VALUE mode, VALUE loops);
VALUE cIGraph_add_edges (int argc, VALUE *argv, VALUE self);
VALUE cIGraph_add_edge (int argc, VALUE *argv, VALUE self);
VALUE cIGraph_add_vertices (VALUE self, VALUE vs);
VALUE cIGraph_delete_edge (VALUE self, VALUE from, VALUE to);
VALUE cIGraph_delete_edges (VALUE self, VALUE edges);
VALUE cIGraph_delete_vertices(VALUE self, VALUE vs);
VALUE cIGraph_add_vertex (VALUE self, VALUE v);
@ -73,3 +75,59 @@ VALUE cIGraph_girth (VALUE self);
VALUE cIGraph_neighborhood_size (VALUE self, VALUE from, VALUE order, VALUE mode);
VALUE cIGraph_neighborhood (VALUE self, VALUE from, VALUE order, VALUE mode);
VALUE cIGraph_neighborhood_graphs(VALUE self, VALUE from, VALUE order, VALUE mode);
//Topological sorting
VALUE cIGraph_topological_sorting(VALUE self, VALUE mode);
//File handling
VALUE cIGraph_read_graph_edgelist (VALUE self, VALUE file, VALUE mode);
VALUE cIGraph_write_graph_edgelist(VALUE self, VALUE file);
//Attributes
int cIGraph_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr);
void cIGraph_attribute_destroy(igraph_t *graph);
int cIGraph_attribute_copy(igraph_t *to, const igraph_t *from);
int cIGraph_attribute_add_vertices(igraph_t *graph, long int nv, igraph_vector_ptr_t *attr);
void cIGraph_attribute_delete_edges(igraph_t *graph, const igraph_vector_t *idx);
void cIGraph_attribute_delete_vertices(igraph_t *graph,
const igraph_vector_t *eidx,
const igraph_vector_t *vidx);
int cIGraph_attribute_add_edges(igraph_t *graph, const igraph_vector_t *edges, igraph_vector_ptr_t *attr);
void cIGraph_attribute_delete_edges(igraph_t *graph, const igraph_vector_t *idx);
int cIGraph_attribute_permute_edges(igraph_t *graph,
const igraph_vector_t *idx);
int cIGraph_attribute_get_info(const igraph_t *graph,
igraph_strvector_t *gnames,
igraph_vector_t *gtypes,
igraph_strvector_t *vnames,
igraph_vector_t *vtypes,
igraph_strvector_t *enames,
igraph_vector_t *etypes);
igraph_bool_t cIGraph_attribute_has_attr(const igraph_t *graph,
igraph_attribute_elemtype_t type,
const char* name);
int cIGraph_attribute_get_type(const igraph_t *graph,
igraph_attribute_type_t *type,
igraph_attribute_elemtype_t elemtype,
const char *name);
int cIGraph_get_numeric_graph_attr(const igraph_t *graph,
const char *name, igraph_vector_t *value);
int cIGraph_get_string_graph_attr(const igraph_t *graph,
const char *name, igraph_strvector_t *value);
int cIGraph_get_numeric_vertex_attr(const igraph_t *graph,
const char *name,
igraph_vs_t vs,
igraph_vector_t *value);
int cIGraph_get_string_vertex_attr(const igraph_t *graph,
const char *name,
igraph_vs_t vs,
igraph_strvector_t *value);
int cIGraph_get_numeric_edge_attr(const igraph_t *graph,
const char *name,
igraph_es_t es,
igraph_vector_t *value);
int cIGraph_get_string_edge_attr(const igraph_t *graph,
const char *name,
igraph_es_t es,
igraph_strvector_t *value);

View File

@ -55,14 +55,14 @@ VALUE cIGraph_add_edges(int argc, VALUE *argv, VALUE self){
code = igraph_add_edges(graph,&edge_v,0);
}
if(attrs != Qnil){
for (i=0; i<RARRAY(attrs)->len; i++) {
cIGraph_set_edge_attr(self,
RARRAY(edges)->ptr[i*2],
RARRAY(edges)->ptr[(i*2)+1],
RARRAY(attrs)->ptr[i]);
}
}
//if(attrs != Qnil){
// for (i=0; i<RARRAY(attrs)->len; i++) {
// cIGraph_set_edge_attr(self,
// RARRAY(edges)->ptr[i*2],
// RARRAY(edges)->ptr[(i*2)+1],
// RARRAY(attrs)->ptr[i]);
//}
//}
igraph_vector_destroy(&edge_v);
@ -178,7 +178,7 @@ VALUE cIGraph_add_edge(int argc, VALUE *argv, VALUE self){
code = igraph_add_edges(graph,&edge_v,0);
if(attr != Qnil){
cIGraph_set_edge_attr(self, from, to, attr);
//cIGraph_set_edge_attr(self, from, to, attr);
}
igraph_vector_destroy(&edge_v);
@ -238,3 +238,28 @@ VALUE cIGraph_add_vertex(VALUE self, VALUE v){
return INT2NUM(code);
}
/* call-seq:
* graph.delete_edge(from,to)
*
* Deletes the edge connecting the two vertices given.
*/
VALUE cIGraph_delete_edge(VALUE self, VALUE from, VALUE to){
igraph_t *graph;
igraph_integer_t eid = 0;
int from_i;
int to_i;
Data_Get_Struct(self, igraph_t, graph);
from_i = cIGraph_get_vertex_id(self,from);
to_i = cIGraph_get_vertex_id(self,to);
igraph_get_eid(graph,&eid,from_i,to_i,1);
igraph_delete_edges(graph,igraph_ess_1(eid));
return Qnil;
}

View File

@ -2,46 +2,172 @@
#include "ruby.h"
#include "cIGraph.h"
igraph_attribute_table_t cIGraph_attribute_table;
igraph_attribute_table_t cIGraph_attribute_table = {
cIGraph_attribute_init,
cIGraph_attribute_destroy,
cIGraph_attribute_copy,
cIGraph_attribute_add_vertices,
cIGraph_attribute_delete_vertices,
cIGraph_attribute_add_edges,
cIGraph_attribute_delete_edges,
cIGraph_attribute_permute_edges,
cIGraph_attribute_get_info,
cIGraph_attribute_has_attr,
cIGraph_attribute_get_type,
cIGraph_get_numeric_graph_attr,
cIGraph_get_string_graph_attr,
cIGraph_get_numeric_vertex_attr,
cIGraph_get_string_vertex_attr,
cIGraph_get_numeric_edge_attr,
cIGraph_get_string_edge_attr,
};
int cIGraph_add_edges_attr(igraph_t *graph, const igraph_vector_t *edges,
igraph_vector_ptr_t *attr){
return 0;
int cIGraph_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr) {
VALUE* attrs;
attrs = (VALUE*)calloc(2, sizeof(VALUE));
if(!attrs)
IGRAPH_ERROR("Error allocating Arrays\n", IGRAPH_ENOMEM);
//[0] is vertex array, [1] is edge array
attrs[0] = rb_ary_new();
attrs[1] = rb_ary_new();
graph->attr = attrs;
return IGRAPH_SUCCESS;
}
int cIGraph_get_numeric_edge_attr(const igraph_t *graph, const char *name,
igraph_es_t es,
igraph_vector_t *value){
return 0;
}
int cIGraph_get_string_edge_attr(const igraph_t *graph, const char *name,
igraph_es_t es,
igraph_strvector_t *value){
return 0;
/* Destruction */
void cIGraph_attribute_destroy(igraph_t *graph) {
free(graph->attr);
return;
}
VALUE cIGraph_get_edge_attr(VALUE self, VALUE from, VALUE to){
/* Copying */
int cIGraph_attribute_copy(igraph_t *to, const igraph_t *from) {
return IGRAPH_SUCCESS;
}
VALUE eid;
VALUE attr_hash;
/* Adding vertices */
int cIGraph_attribute_add_vertices(igraph_t *graph, long int nv, igraph_vector_ptr_t *attr) {
eid = cIGraph_get_eid(self, from, to, 1);
int i;
VALUE vertex_array = ((VALUE*)graph->attr)[0];
attr_hash = rb_iv_get(self,"@edge_attrs");
return rb_hash_aref(attr_hash,eid);
if(attr){
for(i=0;i<nv;i++){
rb_ary_push(vertex_array,(VALUE)VECTOR(*attr)[i]);
}
}
return IGRAPH_SUCCESS;
}
/* Deleting vertices */
void cIGraph_attribute_delete_vertices(igraph_t *graph,
const igraph_vector_t *eidx,
const igraph_vector_t *vidx) {
return;
}
VALUE cIGraph_set_edge_attr(VALUE self, VALUE from, VALUE to, VALUE attr){
/* Adding edges */
int cIGraph_attribute_add_edges(igraph_t *graph,
const igraph_vector_t *edges,
igraph_vector_ptr_t *attr) {
VALUE eid;
VALUE attr_hash;
int i;
VALUE edge_array = ((VALUE*)graph->attr)[1];
eid = cIGraph_get_eid(self, from, to, 1);
attr_hash = rb_iv_get(self,"@edge_attrs");
rb_hash_aset(attr_hash,eid,attr);
return Qnil;
if(attr){
for(i=0;i<igraph_vector_size(edges)/2;i++){
rb_ary_push(edge_array,(VALUE)VECTOR(*attr)[i]);
}
}
return IGRAPH_SUCCESS;
}
/* Deleting edges */
void cIGraph_attribute_delete_edges(igraph_t *graph, const igraph_vector_t *idx) {
return;
}
/* Permuting edges */
int cIGraph_attribute_permute_edges(igraph_t *graph,
const igraph_vector_t *idx) { return 0;
}
/* Getting attribute names and types */
int cIGraph_attribute_get_info(const igraph_t *graph,
igraph_strvector_t *gnames,
igraph_vector_t *gtypes,
igraph_strvector_t *vnames,
igraph_vector_t *vtypes,
igraph_strvector_t *enames,
igraph_vector_t *etypes) {
return 0;
}
/* Checks whether the graph has a graph/vertex/edge attribute with the given name */
igraph_bool_t cIGraph_attribute_has_attr(const igraph_t *graph,
igraph_attribute_elemtype_t type,
const char* name) {
return 0;
}
/* Returns the type of a given attribute */
int cIGraph_attribute_get_type(const igraph_t *graph,
igraph_attribute_type_t *type,
igraph_attribute_elemtype_t elemtype,
const char *name) {
return 0;
}
/* Getting numeric graph attributes */
int cIGraph_get_numeric_graph_attr(const igraph_t *graph,
const char *name, igraph_vector_t *value) {
return 0;
}
/* Getting string graph attributes */
int cIGraph_get_string_graph_attr(const igraph_t *graph,
const char *name, igraph_strvector_t *value) {
return 0;
}
/* Getting numeric vertex attributes */
int cIGraph_get_numeric_vertex_attr(const igraph_t *graph,
const char *name,
igraph_vs_t vs,
igraph_vector_t *value) {
return 0;
}
/* Getting string vertex attributes */
int cIGraph_get_string_vertex_attr(const igraph_t *graph,
const char *name,
igraph_vs_t vs,
igraph_strvector_t *value) {
return 0;
}
/* Getting numeric edge attributes */
int cIGraph_get_numeric_edge_attr(const igraph_t *graph,
const char *name,
igraph_es_t es,
igraph_vector_t *value) {
return 0;
}
/* Getting string edge attributes */
int cIGraph_get_string_edge_attr(const igraph_t *graph,
const char *name,
igraph_es_t es,
igraph_strvector_t *value) {
return 0;
}

View File

@ -4,9 +4,11 @@
igraph_integer_t cIGraph_get_vertex_id(VALUE graph, VALUE v){
VALUE vertex_h;
//VALUE vertex_h;
vertex_h = rb_iv_get(graph,"@object_ids");
//vertex_h = rb_iv_get(graph,"@object_ids");
VALUE vertex_array = ((VALUE*)graph->attr)[0];
if(rb_funcall(vertex_h,rb_intern("has_key?"),1,v))
return NUM2INT(rb_hash_aref(vertex_h,v));