Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Begin implementing vector methods

  • Loading branch information...
commit 532039545edef6c7531d5704042765134ae818c3 1 parent dcc10a6
@jamesdabbs authored
Showing with 58 additions and 26 deletions.
  1. +14 −6 lib/steffi/ffi.rb
  2. +24 −3 lib/steffi/vector.rb
  3. +20 −17 spec/steffi/vector_spec.rb
View
20 lib/steffi/ffi.rb
@@ -67,9 +67,9 @@ def self.method_missing name, *args, &block
bind :delete_vertices, [:pointer, :pointer] # graph, vids
# 7.2.2
- bind :vector_init, [:pointer, :long] # vector, size
+ bind :vector_init, [:pointer, :long]
bind :vector_init_copy, [:pointer, :pointer, :long] # vector, data, length
- bind :vector_init_seq, [:pointer, :double, :double] # vector, from, to
+ bind :vector_init_seq, [:pointer, :double, :double]
bind :vector_copy, [:pointer, :pointer] # from, to
bind :vector_destroy, [:pointer] # vector
@@ -78,10 +78,10 @@ def self.method_missing name, *args, &block
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, [:pointer, :long], :double
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
+ bind :vector_set, [:pointer, :long, :double], :void
+ bind :vector_tail, [:pointer], :double
# 7.2.5 Vector views
bind :vector_view, [:pointer, :pointer, :long], :pointer # vector, data, len => vector
@@ -100,8 +100,16 @@ def self.method_missing name, *args, &block
# ...
# 7.2.10 Finding minimum and maximum
# ...
+
# 7.2.11 Vector properties
- # ...
+ bind :vector_empty, [:pointer], :bool
+ bind :vector_size, [:pointer], :long
+ bind :vector_capacity, [:pointer], :long # vector => alloc'd cap
+ bind :vector_sum, [:pointer], :double # vector => sum
+ bind :vector_prod, [:pointer], :double # vector => prod
+ bind :vector_isininterval, [:pointer, :double, :double], :bool # vector, low, high => pass?
+ bind :vector_maxdifference, [:pointer, :pointer], :double # v1, v2 => max termwise diff
+
# 7.2.12 Searching for elements
# ...
# 7.2.13 Resizing operations
View
27 lib/steffi/vector.rb
@@ -26,6 +26,11 @@ def self.from_a ary
end
end
+ def self.from_slice slice
+ # FIXME: this only works for inclusive slices
+ new { |v| Igraph.vector_init_seq v.pointer, slice.first, slice.last }
+ end
+
include Enumerable
attr_reader :pointer
@@ -48,9 +53,13 @@ def [] i
Igraph.vector_e @pointer, i
end
- # def []= i, obj
- # Igraph.vector_set ptr, i, obj.to_f
- # end
+ def []= i, obj
+ Igraph.vector_set @pointer, i, obj.to_f
+ end
+
+ def tail
+ Igraph.vector_tail @pointer
+ end
def each
0.upto length - 1 do |i|
@@ -58,6 +67,18 @@ def each
end
end
+ def empty?
+ Igraph.vector_empty @pointer
+ end
+
+ def sum
+ Igraph.vector_sum @pointer
+ end
+
+ def product
+ Igraph.vector_prod @pointer
+ end
+
# def to_s
# to_a.to_s
# end
View
37 spec/steffi/vector_spec.rb
@@ -1,26 +1,29 @@
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
+ it 'can create empty vectors' do
+ Steffi::Vector.null.should be_empty
end
- # subject do
- # v = Steffi::Vector.new
- # v << 3.1415
- # v << 1.6180
- # v
- # end
+ it 'can create vectors from slices' do
+ Steffi::Vector.from_slice(5..9).should have(5).members
+ end
+
+ # -----
- # its(:size) { should == 2 }
+ subject { Steffi::Vector.from_a [2, 3, 4] }
- # it 'can read items' do
- # subject[1].should == 1.6180
- # end
+ its(:length) { should == 3 }
+ its(:sum) { should == 9 }
+ its(:product) { should == 24 }
+ its(:tail) { should == 4 }
- # it 'can set items' do
- # subject[0] = -1
- # subject[0].should == -1
- # end
+ it 'can read values' do
+ subject[1].should == 3
+ end
+
+ it 'can set and read values' do
+ subject[1] = -1
+ subject[1].should == -1
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.