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_attribute_handler.c 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "igraph.h"
  2. #include "ruby.h"
  3. #include "cIGraph.h"
  4. /* call-seq:
  5. * graph[u,v] -> Object
  6. *
  7. * Returns the object associated with the edge connecting vertices u and v.
  8. * Aliased to graph.get_edge_attr(u,v)
  9. */
  10. VALUE cIGraph_get_edge_attr(VALUE self, VALUE from, VALUE to){
  11. int idx;
  12. igraph_t *graph;
  13. VALUE e_ary;
  14. Data_Get_Struct(self, igraph_t, graph);
  15. e_ary = ((VALUE*)graph->attr)[1];
  16. idx = NUM2INT(cIGraph_get_eid(self, from, to, 1));
  17. return rb_ary_entry(e_ary,idx);
  18. }
  19. /* call-seq:
  20. * graph[u,v] = w
  21. *
  22. * Sets the object associated with the edge connecting vertices u and v.
  23. * Aliased to graph.set_edge_attr(u,v)
  24. */
  25. VALUE cIGraph_set_edge_attr(VALUE self, VALUE from, VALUE to, VALUE attr){
  26. int idx;
  27. igraph_t *graph;
  28. VALUE e_ary;
  29. Data_Get_Struct(self, igraph_t, graph);
  30. e_ary = ((VALUE*)graph->attr)[1];
  31. idx = NUM2INT(cIGraph_get_eid(self, from, to, 1));
  32. rb_ary_store(e_ary,idx,attr);
  33. return Qtrue;
  34. }
  35. igraph_attribute_table_t cIGraph_attribute_table = {
  36. cIGraph_attribute_init,
  37. cIGraph_attribute_destroy,
  38. cIGraph_attribute_copy,
  39. cIGraph_attribute_add_vertices,
  40. cIGraph_attribute_delete_vertices,
  41. cIGraph_attribute_add_edges,
  42. cIGraph_attribute_delete_edges,
  43. cIGraph_attribute_permute_edges,
  44. cIGraph_attribute_get_info,
  45. cIGraph_attribute_has_attr,
  46. cIGraph_attribute_get_type,
  47. cIGraph_get_numeric_graph_attr,
  48. cIGraph_get_string_graph_attr,
  49. cIGraph_get_numeric_vertex_attr,
  50. cIGraph_get_string_vertex_attr,
  51. cIGraph_get_numeric_edge_attr,
  52. cIGraph_get_string_edge_attr,
  53. };
  54. int cIGraph_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr) {
  55. VALUE* attrs;
  56. //attrs = (VALUE*)calloc(2, sizeof(VALUE));
  57. attrs = ALLOC_N(VALUE, 2);
  58. if(!attrs)
  59. IGRAPH_ERROR("Error allocating Arrays\n", IGRAPH_ENOMEM);
  60. //[0] is vertex array, [1] is edge array
  61. attrs[0] = rb_ary_new();
  62. attrs[1] = rb_ary_new();
  63. graph->attr = attrs;
  64. return IGRAPH_SUCCESS;
  65. }
  66. /* Destruction */
  67. void cIGraph_attribute_destroy(igraph_t *graph) {
  68. free(graph->attr);
  69. return;
  70. }
  71. /* Copying */
  72. int cIGraph_attribute_copy(igraph_t *to, const igraph_t *from) {
  73. VALUE* attrs;
  74. VALUE vertex_array = ((VALUE*)from->attr)[0];
  75. VALUE edge_array = ((VALUE*)from->attr)[1];
  76. //VALUE* attrs = (VALUE*)calloc(2, sizeof(VALUE));
  77. attrs = ALLOC_N(VALUE, 2);
  78. attrs[0] = rb_ary_dup(vertex_array);
  79. attrs[1] = rb_ary_dup(edge_array);
  80. to->attr = attrs;
  81. return IGRAPH_SUCCESS;
  82. }
  83. /* Adding vertices */
  84. int cIGraph_attribute_add_vertices(igraph_t *graph, long int nv, igraph_vector_ptr_t *attr) {
  85. int i;
  86. VALUE vertex_array = ((VALUE*)graph->attr)[0];
  87. if(attr){
  88. for(i=0;i<nv;i++){
  89. rb_ary_push(vertex_array,(VALUE)VECTOR(*attr)[i]);
  90. }
  91. }
  92. return IGRAPH_SUCCESS;
  93. }
  94. /* Deleting vertices */
  95. void cIGraph_attribute_delete_vertices(igraph_t *graph,
  96. const igraph_vector_t *eidx,
  97. const igraph_vector_t *vidx) {
  98. int i;
  99. VALUE vertex_array = ((VALUE*)graph->attr)[0];
  100. VALUE edge_array = ((VALUE*)graph->attr)[1];
  101. VALUE n_v_ary = rb_ary_new();
  102. VALUE n_e_ary = rb_ary_new();
  103. for(i=0;i<igraph_vector_size(vidx);i++){
  104. if(VECTOR(*vidx)[i] != 0)
  105. rb_ary_store(n_v_ary,VECTOR(*vidx)[i]-1,rb_ary_entry(vertex_array,i));
  106. }
  107. for(i=0;i<igraph_vector_size(eidx);i++){
  108. if(VECTOR(*eidx)[i] != 0)
  109. rb_ary_store(n_e_ary,VECTOR(*eidx)[i]-1,rb_ary_entry(edge_array,i));
  110. }
  111. ((VALUE*)graph->attr)[0] = n_v_ary;
  112. ((VALUE*)graph->attr)[1] = n_e_ary;
  113. return;
  114. }
  115. /* Adding edges */
  116. int cIGraph_attribute_add_edges(igraph_t *graph,
  117. const igraph_vector_t *edges,
  118. igraph_vector_ptr_t *attr) {
  119. int i;
  120. VALUE edge_array = ((VALUE*)graph->attr)[1];
  121. if(attr){
  122. for(i=0;i<igraph_vector_size(edges)/2;i++){
  123. rb_ary_push(edge_array,(VALUE)VECTOR(*attr)[i]);
  124. }
  125. }
  126. return IGRAPH_SUCCESS;
  127. }
  128. /* Deleting edges */
  129. void cIGraph_attribute_delete_edges(igraph_t *graph, const igraph_vector_t *idx) {
  130. int i;
  131. VALUE edge_array = ((VALUE*)graph->attr)[1];
  132. VALUE n_e_ary = rb_ary_new();
  133. for(i=0;i<igraph_vector_size(idx);i++){
  134. if(VECTOR(*idx)[i] != 0)
  135. rb_ary_store(n_e_ary,VECTOR(*idx)[i]-1,rb_ary_entry(edge_array,i));
  136. }
  137. ((VALUE*)graph->attr)[1] = n_e_ary;
  138. return;
  139. }
  140. /* Permuting edges */
  141. int cIGraph_attribute_permute_edges(igraph_t *graph,
  142. const igraph_vector_t *idx) {
  143. int i;
  144. VALUE edge_array = ((VALUE*)graph->attr)[1];
  145. VALUE n_e_ary = rb_ary_new();
  146. for(i=0;i<igraph_vector_size(idx);i++){
  147. rb_ary_push(n_e_ary,rb_ary_entry(edge_array,VECTOR(*idx)[i]));
  148. }
  149. ((VALUE*)graph->attr)[1] = n_e_ary;
  150. return 0;
  151. }
  152. /* Getting attribute names and types */
  153. int cIGraph_attribute_get_info(const igraph_t *graph,
  154. igraph_strvector_t *gnames,
  155. igraph_vector_t *gtypes,
  156. igraph_strvector_t *vnames,
  157. igraph_vector_t *vtypes,
  158. igraph_strvector_t *enames,
  159. igraph_vector_t *etypes) {
  160. return 0;
  161. }
  162. /* Checks whether the graph has a graph/vertex/edge attribute with the given name */
  163. igraph_bool_t cIGraph_attribute_has_attr(const igraph_t *graph,
  164. igraph_attribute_elemtype_t type,
  165. const char* name) {
  166. return 0;
  167. }
  168. /* Returns the type of a given attribute */
  169. int cIGraph_attribute_get_type(const igraph_t *graph,
  170. igraph_attribute_type_t *type,
  171. igraph_attribute_elemtype_t elemtype,
  172. const char *name) {
  173. return 0;
  174. }
  175. /* Getting numeric graph attributes */
  176. int cIGraph_get_numeric_graph_attr(const igraph_t *graph,
  177. const char *name, igraph_vector_t *value) {
  178. return 0;
  179. }
  180. /* Getting string graph attributes */
  181. int cIGraph_get_string_graph_attr(const igraph_t *graph,
  182. const char *name, igraph_strvector_t *value) {
  183. return 0;
  184. }
  185. /* Getting numeric vertex attributes */
  186. int cIGraph_get_numeric_vertex_attr(const igraph_t *graph,
  187. const char *name,
  188. igraph_vs_t vs,
  189. igraph_vector_t *value) {
  190. return 0;
  191. }
  192. /* Getting string vertex attributes */
  193. int cIGraph_get_string_vertex_attr(const igraph_t *graph,
  194. const char *name,
  195. igraph_vs_t vs,
  196. igraph_strvector_t *value) {
  197. return 0;
  198. }
  199. /* Getting numeric edge attributes */
  200. int cIGraph_get_numeric_edge_attr(const igraph_t *graph,
  201. const char *name,
  202. igraph_es_t es,
  203. igraph_vector_t *value) {
  204. return 0;
  205. }
  206. /* Getting string edge attributes */
  207. int cIGraph_get_string_edge_attr(const igraph_t *graph,
  208. const char *name,
  209. igraph_es_t es,
  210. igraph_strvector_t *value) {
  211. return 0;
  212. }

Ruby binding for the igraph library.