Fixed selectors.

This commit is contained in:
alexgutteridge 2007-08-07 07:24:12 +00:00
parent 26a9171cdf
commit 29797b430b
5 changed files with 231 additions and 115 deletions

View File

@ -23,6 +23,31 @@ VALUE cIGraph_alloc(VALUE klass){
}
/* Document-method: initialize_copy
*
* Internal method for copying IGraph objects.
*/
VALUE cIGraph_init_copy(VALUE copy, VALUE orig){
igraph_t *orig_graph;
igraph_t *copy_graph;
if (copy == orig)
return copy;
if(TYPE(orig) != T_DATA || RDATA(orig)->dfree != (RUBY_DATA_FUNC)cIGraph_free){
rb_raise(rb_eTypeError, "Wrong argument type.");
}
Data_Get_Struct(copy, igraph_t, copy_graph);
Data_Get_Struct(orig, igraph_t, orig_graph);
igraph_copy(copy_graph,orig_graph);
return copy;
}
/* call-seq:
* IGraph.new(edges,directed) -> IGraph
*
@ -113,27 +138,6 @@ VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){
}
VALUE cIGraph_init_copy(VALUE copy, VALUE orig){
igraph_t *orig_graph;
igraph_t *copy_graph;
if (copy == orig)
return copy;
if(TYPE(orig) != T_DATA || RDATA(orig)->dfree != (RUBY_DATA_FUNC)cIGraph_free){
rb_raise(rb_eTypeError, "Wrong argument type.");
}
Data_Get_Struct(copy, igraph_t, copy_graph);
Data_Get_Struct(orig, igraph_t, orig_graph);
igraph_copy(copy_graph,orig_graph);
return copy;
}
/* Interface to the iGraph[http://cneurocvs.rmki.kfki.hu/igraph/] library
* for graph and network computation. See IGraph#new for how to create a
* graph and get started.
@ -141,96 +145,85 @@ VALUE cIGraph_init_copy(VALUE copy, VALUE orig){
void Init_igraph(){
cIGraph = rb_define_class("IGraph", rb_cObject);
igraph_i_set_attribute_table(&cIGraph_attribute_table);
igraph_set_error_handler(cIGraph_error_handler);
igraph_set_warning_handler(cIGraph_warning_handler);
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, "initialize_copy", cIGraph_init_copy, 1);
rb_define_method(cIGraph, "initialize", cIGraph_initialize, -1);
rb_define_method(cIGraph, "initialize_copy", cIGraph_init_copy, 1);
rb_define_method(cIGraph, "[]", cIGraph_get_edge_attr, 2); /* in cIGraph_attribute_handler.c */
rb_define_method(cIGraph, "[]=", cIGraph_set_edge_attr, 3); /* in cIGraph_attribute_handler.c */
rb_define_alias(cIGraph, "get_edge_attr", "[]");
//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 */
rb_define_method(cIGraph, "each_edge_eid", cIGraph_each_edge_eid,1); /* in cIGraph_iterators.c */
rb_include_module(cIGraph, rb_mEnumerable);
rb_define_const(cIGraph, "EDGEORDER_ID", INT2NUM(1));
rb_define_const(cIGraph, "EDGEORDER_FROM", INT2NUM(2));
rb_define_const(cIGraph, "EDGEORDER_TO", INT2NUM(3));
rb_define_method(cIGraph, "each", cIGraph_each_vertex, 0);
rb_include_module(cIGraph, rb_mEnumerable);
rb_define_method(cIGraph, "include?", cIGraph_include, 1);
rb_define_method(cIGraph, "all_vertices", cIGraph_all_v, 0);
rb_define_method(cIGraph, "vertices", cIGraph_all_v, 0); /* in cIGraph_selectors.c */
rb_define_method(cIGraph, "adjacent_vertices", cIGraph_adj_v, 2); /* in cIGraph_selectors.c */
rb_define_method(cIGraph, "nonadjacent_vertices", cIGraph_nonadj_v, 2); /* in cIGraph_selectors.c */
rb_define_method(cIGraph, "all_edges", cIGraph_all_e, 1);
rb_define_method(cIGraph, "edges", cIGraph_all_e, 1);
rb_define_method(cIGraph, "adjacent_edges", cIGraph_adj_e, 2);
rb_define_method(cIGraph, "nonadjacent_edges", cIGraph_nonadj_e, 2);
rb_define_method(cIGraph, "vcount", cIGraph_vcount, 0); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "ecount", cIGraph_ecount, 0); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "edge", cIGraph_edge, 1); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "get_eid", cIGraph_get_eid, 2); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "neighbors", cIGraph_neighbors,2);
rb_define_method(cIGraph, "neighbours", cIGraph_neighbors,2); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "adjacent", cIGraph_adjacent,2); /* in cIGraph_basic_query.c */
rb_define_const(cIGraph, "OUT", INT2NUM(1));
rb_define_const(cIGraph, "IN", INT2NUM(2));
rb_define_const(cIGraph, "ALL", INT2NUM(3));
rb_define_const(cIGraph, "TOTAL", INT2NUM(4));
rb_define_method(cIGraph, "is_directed", cIGraph_is_directed,0);
rb_define_method(cIGraph, "is_directed?", cIGraph_is_directed,0); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "degree", cIGraph_degree,3); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "[]", cIGraph_get_edge_attr, 2); /* in cIGraph_attribute_handler.c */
rb_define_method(cIGraph, "[]=", cIGraph_set_edge_attr, 3); /* in cIGraph_attribute_handler.c */
rb_define_alias (cIGraph, "get_edge_attr", "[]");
rb_define_alias (cIGraph, "set_edge_attr", "[]=");
rb_define_method(cIGraph, "add_edges", cIGraph_add_edges, -1); /* in cIGraph_add_delete.c */
rb_define_method(cIGraph, "add_vertices", cIGraph_add_vertices, 1); /* in cIGraph_add_delete.c */
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 */
rb_define_method(cIGraph, "each_edge_eid", cIGraph_each_edge_eid,1); /* in cIGraph_iterators.c */
rb_define_alias (cIGraph, "each", "each_vertex");
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, "vertices", cIGraph_all_v, 0); /* in cIGraph_selectors.c */
rb_define_method(cIGraph, "adjacent_vertices", cIGraph_adj_v, 2); /* in cIGraph_selectors.c */
rb_define_method(cIGraph, "nonadjacent_vertices", cIGraph_nonadj_v, 2); /* in cIGraph_selectors.c */
rb_define_alias (cIGraph, "all_vertices", "vertices");
rb_define_method(cIGraph, "delete_edge", cIGraph_delete_edge, 2); /* in cIGraph_add_delete.c */
rb_define_method(cIGraph, "edges", cIGraph_all_e, 1); /* in cIGraph_selectors.c */
rb_define_method(cIGraph, "adjacent_edges", cIGraph_adj_e, 2); /* in cIGraph_selectors.c */
rb_define_alias (cIGraph, "all_edges", "edges");
rb_define_method(cIGraph, "vcount", cIGraph_vcount, 0); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "ecount", cIGraph_ecount, 0); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "edge", cIGraph_edge, 1); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "get_eid", cIGraph_get_eid, 2); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "neighbours", cIGraph_neighbors, 2); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "adjacent", cIGraph_adjacent, 2); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "degree", cIGraph_degree, 3); /* in cIGraph_basic_query.c */
rb_define_method(cIGraph, "is_directed?", cIGraph_is_directed, 0); /* in cIGraph_basic_query.c */
rb_define_alias (cIGraph, "is_directed", "is_directed?");
rb_define_alias (cIGraph, "neighbors", "neighbours");
rb_define_method(cIGraph, "add_edges", cIGraph_add_edges, -1); /* in cIGraph_add_delete.c */
rb_define_method(cIGraph, "add_vertices", cIGraph_add_vertices, 1); /* in cIGraph_add_delete.c */
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, "delete_vertex", cIGraph_delete_vertex, 1); /* 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 */
rb_define_method(cIGraph, "are_connected", cIGraph_are_connected,2); /* in cIGraph_basic_properties.c */
rb_define_alias (cIGraph, "are_connected?", "are_connected");
rb_define_method(cIGraph, "shortest_paths", cIGraph_shortest_paths,2); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "get_shortest_paths", cIGraph_get_shortest_paths,3); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "get_all_shortest_paths", cIGraph_get_all_shortest_paths,3); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "average_path_length", cIGraph_average_path_length,2); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "diameter", cIGraph_diameter,2); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "girth", cIGraph_girth,0); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "shortest_paths", cIGraph_shortest_paths, 2); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "get_shortest_paths", cIGraph_get_shortest_paths, 3); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "get_all_shortest_paths", cIGraph_get_all_shortest_paths, 3); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "average_path_length", cIGraph_average_path_length, 2); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "diameter", cIGraph_diameter, 2); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "girth", cIGraph_girth, 0); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph, "neighbourhood_size", cIGraph_neighborhood_size, 3); /* in cIGraph_vertex_neighbourhood.c */
rb_define_method(cIGraph, "neighborhood_size", cIGraph_neighborhood_size, 3);
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, "neighbourhood_size", cIGraph_neighborhood_size, 3); /* in cIGraph_vertex_neighbourhood.c */
rb_define_method(cIGraph, "neighbourhood", cIGraph_neighborhood, 3); /* in cIGraph_vertex_neighbourhood.c */
rb_define_method(cIGraph, "neighbourhood_graphs", cIGraph_neighborhood_graphs, 3); /* in cIGraph_vertex_neighbourhood.c */
rb_define_alias (cIGraph, "neighborhood_size", "neighbourhood_size");
rb_define_alias (cIGraph, "neighborhood", "neighbourhood");
rb_define_alias (cIGraph, "neighborhood_graphs", "neighbourhood_graphs");
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);
igraph_set_error_handler(cIGraph_error_handler);
igraph_set_warning_handler(cIGraph_warning_handler);
rb_define_method(cIGraph, "write_graph_edgelist", cIGraph_write_graph_edgelist, 1); /* in cIGraph_file.c */
}

View File

@ -20,6 +20,7 @@ void Init_igraph(void);
void cIGraph_free(void *p);
VALUE cIGraph_alloc(VALUE klass);
VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self);
VALUE cIGraph_init_copy(VALUE copy, VALUE orig);
//Attribute accessors
VALUE cIGraph_get_edge_attr(VALUE self, VALUE from, VALUE to);
@ -37,7 +38,6 @@ VALUE cIGraph_nonadj_v(VALUE self, VALUE v, VALUE mode);
VALUE cIGraph_all_e (VALUE self, VALUE mode);
VALUE cIGraph_adj_e (VALUE self, VALUE v, VALUE mode);
VALUE cIGraph_nonadj_e(VALUE self, VALUE v, VALUE mode);
//Basic query operations
VALUE cIGraph_vcount (VALUE self);
@ -70,13 +70,11 @@ VALUE cIGraph_average_path_length (VALUE self, VALUE directed, VALUE unconn);
VALUE cIGraph_diameter (VALUE self, VALUE directed, VALUE unconn);
VALUE cIGraph_girth (VALUE self);
//Vertex neighbourhood functions
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);
@ -85,25 +83,33 @@ 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);
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);
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);
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);
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_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);
@ -112,9 +118,11 @@ int cIGraph_attribute_get_type(const igraph_t *graph,
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);
const char *name,
igraph_vector_t *value);
int cIGraph_get_string_graph_attr(const igraph_t *graph,
const char *name, igraph_strvector_t *value);
const char *name,
igraph_strvector_t *value);
int cIGraph_get_numeric_vertex_attr(const igraph_t *graph,
const char *name,
igraph_vs_t vs,

View File

@ -2,6 +2,12 @@
#include "ruby.h"
#include "cIGraph.h"
/* call-seq:
* graph[u,v] -> Object
*
* Returns the object associated with the edge connecting vertices u and v.
* Aliased to graph.get_edge_attr(u,v)
*/
VALUE cIGraph_get_edge_attr(VALUE self, VALUE from, VALUE to){
int idx;
@ -16,6 +22,12 @@ VALUE cIGraph_get_edge_attr(VALUE self, VALUE from, VALUE to){
}
/* call-seq:
* graph[u,v] = w
*
* Sets the object associated with the edge connecting vertices u and v.
* Aliased to graph.set_edge_attr(u,v)
*/
VALUE cIGraph_set_edge_attr(VALUE self, VALUE from, VALUE to, VALUE attr){
int idx;

View File

@ -67,27 +67,115 @@ VALUE cIGraph_adj_v(VALUE self, VALUE v, VALUE mode){
}
/* call-seq:
* graph.noadjacent_vertices(v,mode) -> Array
*
* Returns all non-neighboring vertices of a given vertex (v). The mode
* argument controls the type of neighboring vertics not to select. Possible
* values: IGRAPH_OUT, all vertices will be selected except those to which
* there is a directed edge from vid. IGRAPH_IN, all vertices will be
* selected except those from which there is a directed edge to vid.
* IGRAPH_ALL, all vertices will be selected except those from or to which
* there is a directed edge to or from vid.
*/
VALUE cIGraph_nonadj_v(VALUE self, VALUE v, VALUE mode){
return Qnil;
igraph_t *graph;
igraph_integer_t pnode;
VALUE nonadjacent = rb_ary_new();
igraph_neimode_t pmode = NUM2INT(mode);
igraph_vs_t vs;
igraph_vit_t vit;
Data_Get_Struct(self, igraph_t, graph);
pnode = cIGraph_get_vertex_id(self,v);
igraph_vs_nonadj(&vs,pnode,pmode);
igraph_vit_create(graph, vs, &vit);
while(!IGRAPH_VIT_END(vit)) {
rb_ary_push(nonadjacent,cIGraph_get_vertex_object(self,IGRAPH_VIT_GET(vit)));
IGRAPH_VIT_NEXT(vit);
}
igraph_vit_destroy(&vit);
igraph_vs_destroy(&vs);
return nonadjacent;
}
/* call-seq:
* graph.edges(mode) -> Array
*
* Returns an Array of all edge ids in the graph. The mode argument specifies
* the order the eids are returned. Possible values: IGRAPH_EDGEORDER_ID,
* edge id order. IGRAPH_EDGEORDER_FROM, vertex id order, the id of the
* source vertex counts for directed graphs. The order of the adjacent edges
* of a given vertex is arbitrary. IGRAPH_EDGEORDER_TO, vertex id order, the
* id of the target vertex counts for directed graphs. The order of the
* adjacent edges of a given vertex is arbitrary. For undirected graph the
* latter two is the same.
*/
VALUE cIGraph_all_e(VALUE self, VALUE mode){
return Qnil;
igraph_t *graph;
igraph_es_t es;
igraph_eit_t eit;
igraph_edgeorder_type_t pmode = NUM2INT(mode);
VALUE edge_ids = rb_ary_new();
Data_Get_Struct(self, igraph_t, graph);
igraph_es_all(&es,pmode);
igraph_eit_create(graph, es, &eit);
while(!IGRAPH_EIT_END(eit)) {
rb_ary_push(edge_ids,INT2NUM(IGRAPH_EIT_GET(eit)));
IGRAPH_EIT_NEXT(eit);
}
igraph_eit_destroy(&eit);
igraph_es_destroy(&es);
return edge_ids;
}
/* call-seq:
* graph.adjacent_edges(v,mode) -> Array
*
* Returns an Array of the eids of the adjacent edges of a vertex (v). The
* mode argument gives the type of edges to select. Possible values:
* IGRAPH_OUT, outgoing edges IGRAPH_IN, incoming edges IGRAPH_ALL, all edges
*/
VALUE cIGraph_adj_e(VALUE self, VALUE v, VALUE mode){
return Qnil;
igraph_t *graph;
igraph_es_t es;
igraph_eit_t eit;
VALUE adjacent = rb_ary_new();
Data_Get_Struct(self, igraph_t, graph);
igraph_es_none(&es);
igraph_es_adj(&es,cIGraph_get_vertex_id(self,v),NUM2INT(mode));
igraph_eit_create(graph, es, &eit);
while(!IGRAPH_EIT_END(eit)) {
rb_ary_push(adjacent,INT2NUM(IGRAPH_EIT_GET(eit)));
IGRAPH_EIT_NEXT(eit);
}
igraph_eit_destroy(&eit);
igraph_es_destroy(&es);
return adjacent;
}
VALUE cIGraph_nonadj_e(VALUE self, VALUE v, VALUE mode){
return Qnil;
}

View File

@ -11,4 +11,19 @@ class TestGraph < Test::Unit::TestCase
graph = IGraph.new(['A','B','C','D'],true)
assert_equal ['B'], graph.adjacent_vertices('A',IGraph::ALL)
end
def test_non_adj
graph = IGraph.new(['A','B','C','D'],true)
assert_equal ['A','C','D'], graph.nonadjacent_vertices('A',IGraph::ALL)
end
def test_e_all
graph = IGraph.new(['A','B','C','D'],true)
assert_equal [0,1], graph.edges(IGraph::EDGEORDER_ID)
end
def test_e_adj
graph = IGraph.new(['A','B','C','D'],true,[1,2])
assert_equal [0], graph.adjacent_edges('B',IGraph::ALL)
end
end