diff options
Diffstat (limited to 'Examples/ruby/std_vector')
-rw-r--r-- | Examples/ruby/std_vector/Makefile | 20 | ||||
-rw-r--r-- | Examples/ruby/std_vector/example.h | 25 | ||||
-rw-r--r-- | Examples/ruby/std_vector/example.i | 17 | ||||
-rw-r--r-- | Examples/ruby/std_vector/runme.rb | 36 |
4 files changed, 98 insertions, 0 deletions
diff --git a/Examples/ruby/std_vector/Makefile b/Examples/ruby/std_vector/Makefile new file mode 100644 index 0000000..15c9d70 --- /dev/null +++ b/Examples/ruby/std_vector/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' ruby_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile ruby_clean + +check: all diff --git a/Examples/ruby/std_vector/example.h b/Examples/ruby/std_vector/example.h new file mode 100644 index 0000000..4f0dac7 --- /dev/null +++ b/Examples/ruby/std_vector/example.h @@ -0,0 +1,25 @@ +/* File : example.h */ + +#include <vector> +#include <algorithm> +#include <functional> +#include <numeric> + +double average(std::vector<int> v) { + return std::accumulate(v.begin(),v.end(),0.0)/v.size(); +} + +std::vector<double> half(const std::vector<double>& v) { + std::vector<double> w(v); + for (unsigned int i=0; i<w.size(); i++) + w[i] /= 2.0; + return w; +} + +void halve_in_place(std::vector<double>& v) { + // would you believe this is the same as the above? + std::transform(v.begin(),v.end(),v.begin(), + std::bind2nd(std::divides<double>(),2.0)); +} + + diff --git a/Examples/ruby/std_vector/example.i b/Examples/ruby/std_vector/example.i new file mode 100644 index 0000000..aa58b66 --- /dev/null +++ b/Examples/ruby/std_vector/example.i @@ -0,0 +1,17 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +%include stl.i +/* instantiate the required template specializations */ +namespace std { + %template(IntVector) vector<int>; + %template(DoubleVector) vector<double>; +} + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/Examples/ruby/std_vector/runme.rb b/Examples/ruby/std_vector/runme.rb new file mode 100644 index 0000000..8511905 --- /dev/null +++ b/Examples/ruby/std_vector/runme.rb @@ -0,0 +1,36 @@ +# file: runme.rb + +require 'example' + +# Call average with a Ruby array... + +puts Example::average([1,2,3,4]) + +# ... or a wrapped std::vector<int> + +v = Example::IntVector.new(4) +0.upto(v.size-1) { |i| v[i] = i+1 } +puts Example::average(v) + + +# half will return a Ruby array. +# Call it with a Ruby array... + +w = Example::half([1.0, 1.5, 2.0, 2.5, 3.0]) +0.upto(w.size-1) { |i| print w[i],"; " } +puts + +# ... or a wrapped std::vector<double> + +v = Example::DoubleVector.new +[1,2,3,4].each { |i| v.push(i) } +w = Example::half(v) +0.upto(w.size-1) { |i| print w[i],"; " } +puts + +# now halve a wrapped std::vector<double> in place + +Example::halve_in_place(v) +0.upto(v.size-1) { |i| print v[i],"; " } +puts + |