Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jamesdabbs/steffi
base: aa1c10e261
...
head fork: jamesdabbs/steffi
compare: 33066671d4
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
117 lib/steffi/constructors.rb
@@ -1,77 +1,82 @@
module Steffi
module Igraph
+ enum :erdos_renyi, [:gnp, :gnm]
+ enum :star_mode, [:out, :in, :undirected, :mutual]
+ enum :tree_mode, [:out, :in, :undirected]
+
bind :read_graph_edgelist, [:pointer, :pointer, :int, :bool], :int
- bind :read_graph_gml, [:pointer, :pointer], :int
- bind :atlas, [:pointer, :int], :int
- bind :cited_type_game, [:pointer, :int, :pointer, :pointer, :int, :bool], :int
- bind :erdos_renyi_game, [:pointer, :int, :int, :double, :bool, :bool], :int
- bind :forest_fire_game, [:pointer, :int, :double, :double, :int, :bool], :int
- bind :famous, [:pointer, :string], :int
- bind :full, [:pointer, :int, :bool, :bool], :int
- bind :ring, [:pointer, :int, :bool, :bool, :bool], :int
- bind :star, [:pointer, :int, :int, :int], :int
- bind :tree, [:pointer, :int, :int, :int], :int
+ bind :read_graph_gml, [:pointer, :pointer], :int
+ bind :atlas, [:pointer, :int], :int
+ bind :cited_type_game, [:pointer, :int, :pointer, :pointer, :int, :bool], :int
+ bind :erdos_renyi_game, [:pointer, :int, :int, :double, :bool, :bool], :int
+ bind :forest_fire_game, [:pointer, :int, :double, :double, :int, :bool], :int
+ bind :famous, [:pointer, :string], :int
+ bind :full, [:pointer, :int, :bool, :bool], :int
+ bind :ring, [:pointer, :int, :bool, :bool, :bool], :int
+ bind :star, [:pointer, :int, :star_mode, :int], :int
+ bind :tree, [:pointer, :int, :int, :int], :int
end
class Graph
- constructor :load do |path, format=nil|
- format ||= file_format path
- C.open(path, 'r') do |stream|
- case format.to_sym
- when :edgelist
- Igraph.read_graph_edgelist ptr, stream, 0, directed?
- when :gml
- Igraph.read_graph_gml ptr, stream
- else
- raise "Unrecognized file format: #{format}"
- end
+ class << self
+
+ # constructor :load do |path, format=nil|
+ # format ||= file_format path
+ # C.open(path, 'r') do |stream|
+ # case format.to_sym
+ # when :edgelist
+ # Igraph.read_graph_edgelist ptr, stream, 0, directed?
+ # when :gml
+ # Igraph.read_graph_gml ptr, stream
+ # else
+ # raise "Unrecognized file format: #{format}"
+ # end
+ # end
+ # end
+
+ def atlas i
+ new { |graph| Igraph.atlas graph.pointer, i }
end
- end
- constructor :atlas do |i|
- Igraph.atlas ptr, i
- end
+ def citations n, types=nil, pref=nil, edges_per_step=1, directed=false
+ # TODO: better handling of vector arguments, defaults
+ types ||= (0...n).to_a
+ types = Vector.from_a(types).pointer
+ pref ||= [1.0 / (n+1)] * (n+1)
+ pref = Vector.from_a(pref).pointer
+ new { |graph| Igraph.cited_type_game graph.pointer, n, types, pref, edges_per_step, directed }
+ end
- constructor :citations do |n, p|
- types = (0...n).to_a
- types = Vector.from_a(types).ptr
- prefs = [1.0 / p] * (n+1)
- prefs = Vector.from_a(prefs).ptr
- Igraph.cited_type_game ptr, n, types, prefs, 1, directed?
- end
+ def erdos_renyi n, k, type=:gnm, directed=false, loops=false
+ new { |graph| Igraph.erdos_renyi_game graph.pointer, type, n, k, directed, loops }
+ end
- constructor :erdos_renyi do |n, k|
- Igraph.erdos_renyi_game ptr, 1, n, k, directed?, false
- end
+ def famous name
+ new { |graph| Igraph.famous graph.pointer, name.to_s }
+ end
- constructor :famous do |name|
- Igraph.famous ptr, name.to_s
- end
+ def forest_fire n, fw, bw, ambassadors, directed=false
+ new { |graph| Igraph.forest_fire_game graph.pointer, n, fw, bw, ambassadors, directed }
+ end
- constructor :forest_fire do |n, k|
- Igraph.forest_fire_game ptr, n, 0.01/k, 1, 2, directed?
- end
+ def full n, directed=false, loops=false
+ new { |graph| Igraph.full graph.pointer, n, directed, loops }
+ end
- # 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
+ def ring n, directed=false, mutual=false, circular=true
+ new { |graph| Igraph.ring graph.pointer, n, directed, mutual, circular }
+ end
- constructor :ring do |n|
- Igraph.ring ptr, n, directed?, false, true
- end
+ def star n, mode=:undirected, center=0
+ new { |graph| Igraph.star graph.pointer, n, mode, center }
+ end
- constructor :star do |n|
- Igraph.star ptr, n, 2, 0
- end
+ def tree n, children, mode=:undirected
+ new { |graph| Igraph.tree graph.pointer, n, children, mode }
+ end
- constructor :tree do |n, k|
- Igraph.tree ptr, n, k, 2
end
-
end
end
View
12 lib/steffi/edge.rb
@@ -1,15 +1,13 @@
module Steffi
class Edge
- attr_accessor :graph, :from, :to
+ attr_reader :graph, :from, :to
- def initialize graph,from,to
- @graph = graph
- @from = from
- @to = to
+ def initialize graph, from, to
+ @graph, @from, @to = graph, from, to
end
- def to_s
- "(#{from},#{to})"
+ def to_pair
+ [@from, @to]
end
end
end
View
2  lib/steffi/edge_set.rb
@@ -24,7 +24,7 @@ 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)]
+ Edge.new @graph, from.get_int(0), to.get_int(0)
end
def each
View
13 lib/steffi/graph.rb
@@ -82,18 +82,5 @@ def initialize
# }
# end
- private #-------------------------------------------------------
-
- def self.constructor name, &block
- define_method name, &block
-
- Graph.define_singleton_method name do |*args|
- opts = args.last.is_a?(Hash) ? args.pop : {}
- g = Graph.new opts
- g.send name, *args
- g
- end
- end
-
end
end
View
16 lib/steffi/metrics.rb
@@ -13,21 +13,5 @@ def diameter
def stats
Hash[ %w{ diameter ecount vcount }.map { |n| [ n, send(n) ] } ]
end
-
- def edge i
- from = FFI::MemoryPointer.new :int
- to = FFI::MemoryPointer.new :int
- Igraph.edge ptr, i, from, to
- Edge.new self, from.get_int(0), to.get_int(0)
- end
-
- # def edges
- # 0.upto(ecount - 1).map { |i| edge i }
- # end
-
- # def vertices
- # 0.upto(vcount - 1).to_a
- # end
-
end
end
View
12 lib/steffi/vector.rb
@@ -27,7 +27,7 @@ def self.from_a ary
end
def self.from_slice slice
- # FIXME: this only works for inclusive slices
+ raise "Slices should be inclusive" if slice.exclude_end?
new { |v| Igraph.vector_init_seq v.pointer, slice.first, slice.last }
end
@@ -78,15 +78,5 @@ def sum
def product
Igraph.vector_prod @pointer
end
-
- # def to_s
- # to_a.to_s
- # end
-
- # def self.from_a ary
- # v = new
- # ary.each { |i| v << i }
- # v
- # end
end
end
View
2  lib/steffi/vertex.rb
@@ -1,5 +1,7 @@
module Steffi
class Vertex
+ attr_reader :graph, :id
+
def initialize graph, id
@graph, @id = graph, id
end
View
46 spec/steffi/constructors_spec.rb
@@ -0,0 +1,46 @@
+require 'helper'
+
+describe 'Graph constructors' do
+ def self.constructor name, *args
+ it "- #{name}" do
+ # FIXME: why is `should have(n).vertices` syntax not working here?
+ yield Steffi::Graph.send name, *args
+ end
+ end
+
+ constructor :atlas, 1 do |graph|
+ graph.vertices.size.should == 1
+ end
+
+ constructor :citations, 55 do |graph|
+ graph.vertices.size.should == 55
+ end
+
+ constructor :erdos_renyi, 100, 123 do |graph|
+ graph.edges.size.should == 123
+ end
+
+ constructor :famous, 'meredith' do |graph|
+ graph.vertices.size.should == 70
+ end
+
+ constructor :forest_fire, 111, 0.01, 3, 20 do |graph|
+ graph.vertices.size.should == 111
+ end
+
+ constructor :full, 4 do |graph|
+ graph.edges.size.should == 6
+ end
+
+ constructor :ring, 9 do |graph|
+ graph.edges.size.should == 9
+ end
+
+ constructor :star, 6 do |graph|
+ graph.edges.size.should == 5
+ end
+
+ constructor :tree, 16, 2 do |graph|
+ graph.edges.size.should == 15
+ end
+end
View
12 spec/steffi/graph_spec.rb
@@ -3,10 +3,6 @@
describe Steffi::Graph do
# subject { Steffi::Graph.famous 'meredith' }
- # its(:diameter) { should == 8 }
- # its(:vcount) { should == 70 }
- # its(:ecount) { should == 140 }
-
# it 'can find communities' do
# subject.communities.should have(70).items
# end
@@ -19,7 +15,7 @@
it 'can create empty graphs' do
g = Steffi::Graph.empty 5
- g.vertices.should have(5).members
+ g.should have(5).vertices
g.edges.should be_empty
end
@@ -35,7 +31,7 @@
it 'can copy a graph' do
g = Steffi::Graph.empty 7
c = Steffi::Graph.copy g
- c.vertices.should have(7).members
+ c.should have(7).vertices
end
it 'can destroy a graph' do
@@ -46,12 +42,12 @@
it 'can create edges' do
g = Steffi::Graph.empty 2
g.edges << [0, 1]
- g.edges.should have(1).member
+ g.should have(1).edges
end
it 'can look up edges by id' do
g = Steffi::Graph.full 5
- g.edges.first.should == [0, 1]
+ g.edges.first.to_pair.should == [0, 1]
end
it 'can look up edges by vertex' do
View
4 spec/steffi/vector_spec.rb
@@ -9,6 +9,10 @@
Steffi::Vector.from_slice(5..9).should have(5).members
end
+ it 'errors on exclusive slices' do
+ expect { Steffi::Vector.from_slice(5...9) }.to raise_error
+ end
+
# -----
subject { Steffi::Vector.from_a [2, 3, 4] }

No commit comments for this range

Something went wrong with that request. Please try again.