diff --git a/ext/cIGraph_operators.c b/ext/cIGraph_operators.c new file mode 100644 index 0000000..7f73fff --- /dev/null +++ b/ext/cIGraph_operators.c @@ -0,0 +1,9 @@ +#include "igraph.h" +#include "ruby.h" +#include "cIGraph.h" + +VALUE cIGraph_add_op(VALUE self, VALUE graph){ + + return Qnil; + +} diff --git a/ext/cIGraph_selectors.c b/ext/cIGraph_selectors.c new file mode 100644 index 0000000..12d4d1c --- /dev/null +++ b/ext/cIGraph_selectors.c @@ -0,0 +1,93 @@ +#include "igraph.h" +#include "ruby.h" +#include "cIGraph.h" + +/* call-seq: + * graph.vertices -> Array + * + * Returns an Array containing all the vertices in the graph. Also aliased + * to IGraph#all_vertices + * + * Example: + * + * g = IGraph.new([1,2,3,4],true) + * g.vertices #returns [1,2,3,4] + * + */ +VALUE cIGraph_all_v(VALUE self){ + igraph_t *graph; + + Data_Get_Struct(self, igraph_t, graph); + return rb_funcall(rb_iv_get(self,"@object_ids"),rb_intern("keys"),0); + +} + +/* call-seq: + * graph.adjacent_vertices(v,mode) -> Array + * + * Returns an Array containing all the vertices in the graph that are + * adjacent to vertex v. mode decides the type of the neighborhood for + * directed graphs. Possible values: IGRAPH_OUT, all vertices to which + * there is a directed edge from vid. IGRAPH_IN, all vertices from which + * there is a directed edge from vid. IGRAPH_ALL, all vertices to which + * or from which there is a directed edge from/to vid. This parameter is + * ignored for undirected graphs. + * + * Example: + * + * g = IGraph.new([1,2,3,4],true) + * g.adjacent_vertices(1,IGraph::ALL) #returns [2] + * + */ +VALUE cIGraph_adj_v(VALUE self, VALUE v, VALUE mode){ + + igraph_t *graph; + igraph_integer_t pnode; + VALUE adjacent = 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_adj(&vs,pnode,pmode); + igraph_vit_create(graph, vs, &vit); + + while(!IGRAPH_VIT_END(vit)) { + rb_ary_push(adjacent,cIGraph_get_vertex_object(self,IGRAPH_VIT_GET(vit))); + IGRAPH_VIT_NEXT(vit); + } + + igraph_vit_destroy(&vit); + igraph_vs_destroy(&vs); + + return adjacent; + +} + +VALUE cIGraph_nonadj_v(VALUE self, VALUE v, VALUE mode){ + + return Qnil; + +} + +VALUE cIGraph_all_e(VALUE self, VALUE mode){ + + return Qnil; + +} + +VALUE cIGraph_adj_e(VALUE self, VALUE v, VALUE mode){ + + return Qnil; + +} + +VALUE cIGraph_nonadj_e(VALUE self, VALUE v, VALUE mode){ + + return Qnil; + +} + diff --git a/test/tc_iterators.rb b/test/tc_iterators.rb new file mode 100644 index 0000000..e8e29af --- /dev/null +++ b/test/tc_iterators.rb @@ -0,0 +1,44 @@ +require 'test/unit' +require 'igraph' + +class TestGraph < Test::Unit::TestCase + def test_each_vertex + graph = IGraph.new(['A','B','C','D'],true) + assert_nothing_raised do + graph.each_vertex do |v| + end + end + assert_nothing_raised do + graph.each do |v| + end + end + end + + def test_each_edge + graph = IGraph.new(['A','B','C','D'],true) + assert_nothing_raised do + graph.each_edge(IGraph::EDGEORDER_ID) do |v,w| + end + end + assert_nothing_raised do + graph.each_edge_eid(IGraph::EDGEORDER_ID) do |v| + end + end + edges = [] + graph.each_edge(IGraph::EDGEORDER_ID){|v,w| edges.push([v,w])} + assert_equal [['A','B'],['C','D']], edges + edges = [] + graph.each_edge_eid(IGraph::EDGEORDER_ID){|v| edges.push(v)} + assert_equal [0,1], edges + end + + def test_enumerable + graph = IGraph.new(['A','B','C','D'],true) + assert graph.all?{|v| v.kind_of? String} + assert graph.any?{|v| v == 'B'} + assert_equal ['A','B','C','D'], graph.collect{|v| v} + assert graph.detect(Proc.new{true}){|v| } + assert_equal ['A'], graph.find_all{|v| v < 'B'} + end + +end diff --git a/test/tc_selectors.rb b/test/tc_selectors.rb new file mode 100644 index 0000000..3510e42 --- /dev/null +++ b/test/tc_selectors.rb @@ -0,0 +1,14 @@ +require 'test/unit' +require 'igraph' + +class TestGraph < Test::Unit::TestCase + def test_select_all + graph = IGraph.new(['A','B','C','D'],true) + assert_equal ['A','B','C','D'], graph.all_vertices + assert_equal ['A','B','C','D'], graph.vertices + end + def test_adj + graph = IGraph.new(['A','B','C','D'],true) + assert_equal ['B'], graph.adjacent_vertices('A',IGraph::ALL) + end +end