You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cIGraph_basic_query.c 3.3 kB

19 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "igraph.h"
  2. #include "ruby.h"
  3. #include "cIGraph.h"
  4. VALUE cIGraph_vcount(VALUE self){
  5. igraph_t *graph;
  6. Data_Get_Struct(self, igraph_t, graph);
  7. return INT2NUM(igraph_vcount(graph));
  8. }
  9. VALUE cIGraph_ecount(VALUE self){
  10. igraph_t *graph;
  11. Data_Get_Struct(self, igraph_t, graph);
  12. return INT2NUM(igraph_ecount(graph));
  13. }
  14. VALUE cIGraph_edge(VALUE self, VALUE eid){
  15. igraph_t *graph;
  16. igraph_integer_t from = 0;
  17. igraph_integer_t to = 0;
  18. VALUE from_r;
  19. VALUE to_r;
  20. Data_Get_Struct(self, igraph_t, graph);
  21. igraph_edge(graph,NUM2INT(eid),&from,&to);
  22. from_r = cIGraph_get_vertex_object(self,from);
  23. to_r = cIGraph_get_vertex_object(self,to);
  24. return rb_ary_new3(2, from_r, to_r);
  25. }
  26. VALUE cIGraph_get_eid(VALUE self, VALUE from, VALUE to, VALUE directed){
  27. igraph_t *graph;
  28. igraph_integer_t eid = 0;
  29. int from_i;
  30. int to_i;
  31. igraph_bool_t directed_b = 0;
  32. Data_Get_Struct(self, igraph_t, graph);
  33. from_i = cIGraph_get_vertex_id(self,from);
  34. to_i = cIGraph_get_vertex_id(self,to);
  35. if(directed)
  36. directed_b = 1;
  37. igraph_get_eid(graph,&eid,from_i,to_i,directed_b);
  38. return INT2NUM(eid);
  39. }
  40. VALUE cIGraph_neighbors(VALUE self, VALUE v, VALUE mode){
  41. igraph_t *graph;
  42. igraph_integer_t pnode;
  43. igraph_neimode_t pmode = NUM2INT(mode);
  44. igraph_vector_t neis;
  45. int i;
  46. VALUE neighbors = rb_ary_new();
  47. igraph_vector_init_int(&neis,0);
  48. Data_Get_Struct(self, igraph_t, graph);
  49. pnode = cIGraph_get_vertex_id(self,v);
  50. igraph_neighbors(graph,&neis,pnode,pmode);
  51. for(i=0;i<igraph_vector_size(&neis);i++){
  52. rb_ary_push(neighbors,cIGraph_get_vertex_object(self,VECTOR(neis)[i]));
  53. }
  54. igraph_vector_destroy(&neis);
  55. return neighbors;
  56. }
  57. VALUE cIGraph_adjacent(VALUE self, VALUE v, VALUE mode){
  58. igraph_t *graph;
  59. igraph_integer_t pnode;
  60. igraph_neimode_t pmode = NUM2INT(mode);
  61. igraph_vector_t eids;
  62. int i;
  63. VALUE eids_r = rb_ary_new();
  64. igraph_vector_init_int(&eids,0);
  65. Data_Get_Struct(self, igraph_t, graph);
  66. pnode = cIGraph_get_vertex_id(self,v);
  67. igraph_adjacent(graph,&eids,pnode,pmode);
  68. for(i=0;i<igraph_vector_size(&eids);i++){
  69. rb_ary_push(eids_r,INT2NUM(VECTOR(eids)[i]));
  70. }
  71. igraph_vector_destroy(&eids);
  72. return eids_r;
  73. }
  74. VALUE cIGraph_is_directed(VALUE self){
  75. igraph_t *graph;
  76. Data_Get_Struct(self, igraph_t, graph);
  77. return igraph_is_directed(graph) ? Qtrue : Qfalse;
  78. }
  79. VALUE cIGraph_degree(VALUE self, VALUE v, VALUE mode, VALUE loops){
  80. igraph_t *graph;
  81. igraph_vs_t vids;
  82. igraph_vector_t vidv;
  83. igraph_neimode_t pmode = NUM2INT(mode);
  84. igraph_bool_t loop_mode = loops ? 1 : 0;
  85. igraph_vector_t res;
  86. int i;
  87. VALUE degree_r = rb_ary_new();
  88. //vector to hold the results of the degree calculations
  89. igraph_vector_init_int(&res,0);
  90. Data_Get_Struct(self, igraph_t, graph);
  91. //Convert an array of vertices to a vector of vertex ids
  92. cIGraph_vertex_arr_to_id_vec(self,v,&vidv);
  93. //create vertex selector from the vecotr of ids
  94. igraph_vs_vector(&vids,&vidv);
  95. igraph_degree(graph,&res,vids,pmode,loop_mode);
  96. for(i=0;i<igraph_vector_size(&res);i++){
  97. rb_ary_push(degree_r,INT2NUM(VECTOR(res)[i]));
  98. }
  99. igraph_vector_destroy(&vidv);
  100. igraph_vector_destroy(&res);
  101. igraph_vs_destroy(&vids);
  102. return degree_r;
  103. }

Ruby binding for the igraph library.