Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add edge creation and lookup

  • Loading branch information...
commit 2f97b264e630f58068df6ca40906ef1e030725ee 1 parent 64b1d45
James Dabbs authored
7 lib/steffi/constructors.rb
View
@@ -54,8 +54,11 @@ class Graph
Igraph.forest_fire_game ptr, n, 0.01/k, 1, 2, directed?
end
- constructor :full do |n|
- Igraph.full ptr, n, directed?, false
+ # constructor :full do |n|
+ # Igraph.full ptr, n, directed?, false
+ # end
+ def self.full n, directed=false, loops=false
+ new { |graph| Igraph.full graph.pointer, n, directed, loops }
end
constructor :ring do |n|
39 lib/steffi/edge_set.rb
View
@@ -1,13 +1,34 @@
-class EdgeSet
- def initialize graph
- @graph = graph
- end
+module Steffi
+ class EdgeSet
+ include Enumerable
- def size
- Steffi::Igraph.ecount @graph.pointer
- end
+ def initialize graph
+ @graph = graph
+ end
+
+ def size
+ Igraph.ecount @graph.pointer
+ end
+
+ def empty?
+ size == 0
+ end
+
+ def << pair
+ Igraph.add_edge @graph.pointer, pair.first, pair.last
+ end
+
+ def [] n
+ from = FFI::MemoryPointer.new :int
+ to = FFI::MemoryPointer.new :int
+ Igraph.edge @graph.pointer, n, from, to
+ [from.get_int(0), to.get_int(0)]
+ end
- def empty?
- size == 0
+ def each
+ 0.upto size do |i|
+ yield self[i]
+ end
+ end
end
end
12 lib/steffi/ffi.rb
View
@@ -35,14 +35,14 @@ def self.method_missing name, *args, &block
end
# 2.1 Graph constructors and deconstructors
- bind :empty, [:pointer, :int, :bool] # graph, vertex count, directed?
+ bind :empty, [:pointer, :int, :bool]
bind :empty_attrs, [:pointer, :int, :bool, :pointer] # graph, vertex count, directed?, (void *) attributes
- bind :copy, [:pointer, :pointer] # to, from
- bind :destroy, [:pointer] # graph
+ bind :copy, [:pointer, :pointer]
+ bind :destroy, [:pointer]
# 2.2 Basic query operations
- bind :vcount, [:pointer], :int # graph => vertex count
- bind :ecount, [:pointer], :int # graph => edge count
+ bind :vcount, [:pointer], :int
+ bind :ecount, [:pointer], :int
bind :edge, [:pointer, :int, :pointer, :pointer] # graph, edge #, &from, &to
# graph, &edge #, from, to, directed?, error?
@@ -62,7 +62,7 @@ def self.method_missing name, *args, &block
bind :degree, [:pointer, :pointer, :int, :bool]
# 2.3 Adding and deleting vertices and edges
- bind :add_edge, [:pointer, :int, :int] # graph, from, to
+ bind :add_edge, [:pointer, :int, :int]
bind :add_edges, [:pointer, :pointer, :pointer] # graph, [edges], (void *) attrs
bind :add_vertices, [:pointer, :int, :pointer] # graph, id, (void *) attr
bind :delete_edges, [:pointer, :pointer] # graph, eids
20 lib/steffi/vertex_set.rb
View
@@ -1,13 +1,15 @@
-class VertexSet
- def initialize graph
- @graph = graph
- end
+module Steffi
+ class VertexSet
+ def initialize graph
+ @graph = graph
+ end
- def size
- Steffi::Igraph.vcount @graph.pointer
- end
+ def size
+ Igraph.vcount @graph.pointer
+ end
- def empty?
- size == 0
+ def empty?
+ size == 0
+ end
end
end
11 spec/steffi/graph_spec.rb
View
@@ -35,4 +35,15 @@
# I don't see a great way to check that C memory has been freed
Steffi::Graph.empty(1).should respond_to :destroy
end
+
+ it 'can create edges' do
+ g = Steffi::Graph.empty 2
+ g.edges << [0, 1]
+ g.edges.should have(1).member
+ end
+
+ it 'can look up edges' do
+ g = Steffi::Graph.full 5
+ g.edges.first.should == [0, 1]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.