Skip to content
Browse files

Add vectors, neighbor and incident lookups

  • Loading branch information...
1 parent 7c09ddc commit 5a182a6095be43a99a9f7c44f942f6e17dcea8f5 @jamesdabbs committed Apr 10, 2013
Showing with 157 additions and 43 deletions.
  1. +15 −1 lib/steffi/edge_set.rb
  2. +51 −7 lib/steffi/ffi.rb
  3. +36 −31 lib/steffi/vector.rb
  4. +16 −2 lib/steffi/vertex.rb
  5. +12 −0 lib/steffi/vertex_set.rb
  6. +22 −2 spec/steffi/graph_spec.rb
  7. +5 −0 spec/steffi/vector_spec.rb
View
16 lib/steffi/edge_set.rb
@@ -26,7 +26,7 @@ def [] n
end
def each
- 0.upto size do |i|
+ 0.upto size - 1 do |i|
yield self[i]
end
end
@@ -35,5 +35,19 @@ def find pair, error=false
id = FFI::MemoryPointer.new :int
Igraph.get_eid @graph.pointer, id, pair.first, pair.last, @graph.directed?, error
end
+
+ # def find_pairs *pairs
+ # edges = Vector.new
+ # error = false
+ # Igraph.get_eids @graph.pointer, edges.pointer, Vector.from_a(pairs), nil, @graph.directed?, error
+ # edges
+ # end
+
+ # def find_path *vertices
+ # edges = Vector.new
+ # error = false
+ # Igraph.get_eids @graph.pointer, edges.pointer, nil, Vector.from_a(pairs), nil, @graph.directed?, error
+ # edges
+ # end
end
end
View
58 lib/steffi/ffi.rb
@@ -34,13 +34,13 @@ def self.method_missing name, *args, &block
end
end
- # 2.1 Graph constructors and deconstructors
+ # 4.2.1 Graph constructors and deconstructors
bind :empty, [:pointer, :int, :bool]
bind :empty_attrs, [:pointer, :int, :bool, :pointer] # graph, vertex count, directed?, (void *) attributes
bind :copy, [:pointer, :pointer]
bind :destroy, [:pointer]
- # 2.2 Basic query operations
+ # 4.2.2 Basic query operations
bind :vcount, [:pointer], :int
bind :ecount, [:pointer], :int
bind :edge, [:pointer, :int, :pointer, :pointer]
@@ -52,21 +52,65 @@ def self.method_missing name, *args, &block
# graph, &[edge #]. &[pairs], &[path], directed?, error?
bind :get_eids_multi, [:pointer, :pointer, :pointer, :pointer, :bool, :bool]
- bind :neighbors, [:pointer, :pointer, :int, :int] # graph, &[result], node, mode
- bind :incident, [:pointer, :pointer, :int, :int] # graph, &[result], node, mode
+ bind :neighbors, [:pointer, :pointer, :int, :int]
+ bind :incident, [:pointer, :pointer, :int, :int]
bind :is_directed, [:pointer], :bool
# graph, &[results], vids, mode, loops?
bind :degree, [:pointer, :pointer, :int, :bool]
- # 2.3 Adding and deleting vertices and edges
+ # 4.2.3 Adding and deleting vertices and edges
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
bind :delete_vertices, [:pointer, :pointer] # graph, vids
- # 2.4 Deprecated
- # igraph_adjacent
+ # 7.2.2
+ bind :vector_init, [:pointer, :long] # vector, size
+ bind :vector_init_copy, [:pointer, :pointer, :long] # vector, data, length
+ bind :vector_init_seq, [:pointer, :double, :double] # vector, from, to
+ bind :vector_copy, [:pointer, :pointer] # from, to
+ bind :vector_destroy, [:pointer] # vector
+
+ # 7.2.3 Initializing elements
+ bind :vector_null, [:pointer], :void # vector
+ bind :vector_fill, [:pointer, :double], :void # vector, value
+
+ # 7.2.4 Accessing elements
+ bind :vector_e, [:pointer, :long], :double # vector, pos => value
+ bind :vector_e_ptr, [:pointer, :long], :pointer # vector, pos => address
+ bind :vector_set, [:pointer, :long, :double], :void # vector, pos, value
+ bind :vector_tail, [:pointer], :double # vector => value
+
+ # 7.2.5 Vector views
+ bind :vector_view, [:pointer, :pointer, :long], :pointer # vector, data, len => vector
+
+ # 7.2.6 Copying vectors
+ bind :vector_copy_to, [:pointer, :pointer], :void # vector, array
+ bind :vector_update, [:pointer, :pointer] # to, from
+ bind :vector_append, [:pointer, :pointer] # to, from
+ bind :vector_swap, [:pointer, :pointer] # v1, v2
+
+ # 7.2.7 Exchanging elements
+ # ...
+ # 7.2.8 Vector operations
+ # ...
+ # 7.2.9 Vector comparisons
+ # ...
+ # 7.2.10 Finding minimum and maximum
+ # ...
+ # 7.2.11 Vector properties
+ # ...
+ # 7.2.12 Searching for elements
+ # ...
+ # 7.2.13 Resizing operations
+ # ...
+ # 7.2.14 Sorting
+ # ...
+ # 7.2.15 Set operations on sorted vectors
+ # ...
+ # 7.2.16 Pointer vectors
+ # ...
end
end
View
67 lib/steffi/vector.rb
@@ -1,16 +1,10 @@
module Steffi
-
module Igraph
- bind :vector_init, [:pointer, :long], :int
- bind :vector_e, [:pointer, :long], :double
- bind :vector_set, [:pointer, :long, :double], :void
- bind :vector_size, [:pointer], :long
- bind :vector_push_back, [:pointer, :double], :int
- bind :vector_destroy, [:pointer], :void
+ bind :vector_push_back, [:pointer, :double]
+ bind :vector_size, [:pointer], :int
end
class Vector
-
class Struct < FFI::ManagedStruct
layout :stor_begin, :pointer,
:stor_end, :pointer,
@@ -21,45 +15,56 @@ def self.release ptr
end
end
+ def self.null
+ new { |v| Igraph.vector_init v.pointer, 0 }
+ end
+
+ def self.from_a ary
+ v = new { |v| Igraph.vector_init v.pointer, 0 }
+ ary.each { |i| v << i}
+ v
+ end
+
include Enumerable
- attr_reader :ptr
+ attr_reader :pointer
def initialize
- @ptr = FFI::MemoryPointer.new Vector::Struct
- Igraph.vector_init ptr, 0
+ @pointer = FFI::MemoryPointer.new Vector::Struct
+ yield self if block_given?
+ self
end
- def << obj
- Igraph.vector_push_back ptr, obj.to_f
+ def length
+ Igraph.vector_size @pointer
end
- def [] i
- Igraph.vector_e ptr, i
+ def << obj
+ Igraph.vector_push_back @pointer, obj.to_f
end
- def []= i, obj
- Igraph.vector_set ptr, i, obj.to_f
+ def [] i
+ Igraph.vector_e @pointer, i
end
- def size
- Igraph.vector_size ptr
- end
+ # def []= i, obj
+ # Igraph.vector_set ptr, i, obj.to_f
+ # end
def each
- 0.upto(size-1) { |i| yield self[i] }
- end
-
- def to_s
- to_a.to_s
+ 0.upto length - 1 do |i|
+ yield self[i]
+ end
end
- def self.from_a ary
- v = new
- ary.each { |i| v << i }
- v
- 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
18 lib/steffi/vertex.rb
@@ -1,7 +1,21 @@
module Steffi
class Vertex
- def initialize graph
- @graph = graph
+ def initialize graph, id
+ @graph, @id = graph, id
+ end
+
+ def neighbors
+ mode = 3 # IGRAPH_ALL / FIXME: define and use enum
+ v = Vector.null
+ Igraph.neighbors @graph.pointer, v.pointer, @id, mode
+ v.to_a
+ end
+
+ def incident
+ mode = 3 # IGRAPH_ALL / FIXME: define and use enum
+ v = Vector.null
+ Igraph.incident @graph.pointer, v.pointer, @id, mode
+ v.map &:to_i
end
end
end
View
12 lib/steffi/vertex_set.rb
@@ -1,5 +1,7 @@
module Steffi
class VertexSet
+ include Enumerable
+
def initialize graph
@graph = graph
end
@@ -11,5 +13,15 @@ def size
def empty?
size == 0
end
+
+ def [] n
+ Vertex.new @graph, n
+ end
+
+ def each
+ 0.upto size - 1 do |i|
+ yield self[i]
+ end
+ end
end
end
View
24 spec/steffi/graph_spec.rb
@@ -15,7 +15,7 @@
# expect { Steffi::Graph.famous 'non-existent' }.to raise_error(Steffi::Igraph::Error)
# end
- # -- 2 -----
+ # -- 4.1-3 -----
it 'can create empty graphs' do
g = Steffi::Graph.empty 5
@@ -60,5 +60,25 @@
end
pending 'can lookup directed edges'
- pending 'can handle errors when looking up edges'
+ pending 'can handle errors when looking up edges (eid, eids, eids_multi)'
+
+ # it 'can look up a list of edges by vertex' do
+ # g = Steffi::Graph.full 3
+ # g.edges.find_pairs([0,1], [1,2]).should == [0, 1]
+ # end
+
+ # it 'can look up a list of edges in a path' do
+ # g = Steffi::Graph.full 3
+ # g.edges.find_path([0,1,2]).should == [0, 1]
+ # end
+
+ it 'can look up neighbors' do
+ g = Steffi::Graph.full 5
+ g.vertices.first.neighbors.should == [1, 2, 3, 4]
+ end
+
+ it 'knows incident edges' do
+ g = Steffi::Graph.full 3
+ g.vertices.first.incident.should == [0, 1]
+ end
end
View
5 spec/steffi/vector_spec.rb
@@ -1,6 +1,11 @@
require 'helper'
describe Steffi::Vector do
+ it 'can be copied from an array' do
+ v = Steffi::Vector.from_a [0, 1, 2]
+ v.length.should == 3
+ end
+
# subject do
# v = Steffi::Vector.new
# v << 3.1415

0 comments on commit 5a182a6

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