Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add edge creation and lookup

  • Loading branch information...
commit 2f97b264e630f58068df6ca40906ef1e030725ee 1 parent 64b1d45
James Dabbs authored
7 lib/steffi/constructors.rb
@@ -54,8 +54,11 @@ class Graph
54 54 Igraph.forest_fire_game ptr, n, 0.01/k, 1, 2, directed?
55 55 end
56 56
57   - constructor :full do |n|
58   - Igraph.full ptr, n, directed?, false
  57 + # constructor :full do |n|
  58 + # Igraph.full ptr, n, directed?, false
  59 + # end
  60 + def self.full n, directed=false, loops=false
  61 + new { |graph| Igraph.full graph.pointer, n, directed, loops }
59 62 end
60 63
61 64 constructor :ring do |n|
39 lib/steffi/edge_set.rb
... ... @@ -1,13 +1,34 @@
1   -class EdgeSet
2   - def initialize graph
3   - @graph = graph
4   - end
  1 +module Steffi
  2 + class EdgeSet
  3 + include Enumerable
5 4
6   - def size
7   - Steffi::Igraph.ecount @graph.pointer
8   - end
  5 + def initialize graph
  6 + @graph = graph
  7 + end
  8 +
  9 + def size
  10 + Igraph.ecount @graph.pointer
  11 + end
  12 +
  13 + def empty?
  14 + size == 0
  15 + end
  16 +
  17 + def << pair
  18 + Igraph.add_edge @graph.pointer, pair.first, pair.last
  19 + end
  20 +
  21 + def [] n
  22 + from = FFI::MemoryPointer.new :int
  23 + to = FFI::MemoryPointer.new :int
  24 + Igraph.edge @graph.pointer, n, from, to
  25 + [from.get_int(0), to.get_int(0)]
  26 + end
9 27
10   - def empty?
11   - size == 0
  28 + def each
  29 + 0.upto size do |i|
  30 + yield self[i]
  31 + end
  32 + end
12 33 end
13 34 end
12 lib/steffi/ffi.rb
@@ -35,14 +35,14 @@ def self.method_missing name, *args, &block
35 35 end
36 36
37 37 # 2.1 Graph constructors and deconstructors
38   - bind :empty, [:pointer, :int, :bool] # graph, vertex count, directed?
  38 + bind :empty, [:pointer, :int, :bool]
39 39 bind :empty_attrs, [:pointer, :int, :bool, :pointer] # graph, vertex count, directed?, (void *) attributes
40   - bind :copy, [:pointer, :pointer] # to, from
41   - bind :destroy, [:pointer] # graph
  40 + bind :copy, [:pointer, :pointer]
  41 + bind :destroy, [:pointer]
42 42
43 43 # 2.2 Basic query operations
44   - bind :vcount, [:pointer], :int # graph => vertex count
45   - bind :ecount, [:pointer], :int # graph => edge count
  44 + bind :vcount, [:pointer], :int
  45 + bind :ecount, [:pointer], :int
46 46 bind :edge, [:pointer, :int, :pointer, :pointer] # graph, edge #, &from, &to
47 47
48 48 # graph, &edge #, from, to, directed?, error?
@@ -62,7 +62,7 @@ def self.method_missing name, *args, &block
62 62 bind :degree, [:pointer, :pointer, :int, :bool]
63 63
64 64 # 2.3 Adding and deleting vertices and edges
65   - bind :add_edge, [:pointer, :int, :int] # graph, from, to
  65 + bind :add_edge, [:pointer, :int, :int]
66 66 bind :add_edges, [:pointer, :pointer, :pointer] # graph, [edges], (void *) attrs
67 67 bind :add_vertices, [:pointer, :int, :pointer] # graph, id, (void *) attr
68 68 bind :delete_edges, [:pointer, :pointer] # graph, eids
20 lib/steffi/vertex_set.rb
... ... @@ -1,13 +1,15 @@
1   -class VertexSet
2   - def initialize graph
3   - @graph = graph
4   - end
  1 +module Steffi
  2 + class VertexSet
  3 + def initialize graph
  4 + @graph = graph
  5 + end
5 6
6   - def size
7   - Steffi::Igraph.vcount @graph.pointer
8   - end
  7 + def size
  8 + Igraph.vcount @graph.pointer
  9 + end
9 10
10   - def empty?
11   - size == 0
  11 + def empty?
  12 + size == 0
  13 + end
12 14 end
13 15 end
11 spec/steffi/graph_spec.rb
@@ -35,4 +35,15 @@
35 35 # I don't see a great way to check that C memory has been freed
36 36 Steffi::Graph.empty(1).should respond_to :destroy
37 37 end
  38 +
  39 + it 'can create edges' do
  40 + g = Steffi::Graph.empty 2
  41 + g.edges << [0, 1]
  42 + g.edges.should have(1).member
  43 + end
  44 +
  45 + it 'can look up edges' do
  46 + g = Steffi::Graph.full 5
  47 + g.edges.first.should == [0, 1]
  48 + end
38 49 end

0 comments on commit 2f97b26

Please sign in to comment.
Something went wrong with that request. Please try again.