From 2824b0cbb66e715490e1ef13250bd675d87b32d9 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 2 Jun 2010 20:53:17 +0000 Subject: rel-2.0.0 git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/tags/rel-2.0.0@12089 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- trunk/Examples/python/std_vector/Makefile | 22 ++++++++++++++++++ trunk/Examples/python/std_vector/example.h | 25 +++++++++++++++++++++ trunk/Examples/python/std_vector/example.i | 17 ++++++++++++++ trunk/Examples/python/std_vector/runme.py | 36 ++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 trunk/Examples/python/std_vector/Makefile create mode 100644 trunk/Examples/python/std_vector/example.h create mode 100644 trunk/Examples/python/std_vector/example.i create mode 100644 trunk/Examples/python/std_vector/runme.py (limited to 'trunk/Examples/python/std_vector') diff --git a/trunk/Examples/python/std_vector/Makefile b/trunk/Examples/python/std_vector/Makefile new file mode 100644 index 000000000..ba5c79827 --- /dev/null +++ b/trunk/Examples/python/std_vector/Makefile @@ -0,0 +1,22 @@ +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)' python_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile python_clean + rm -f $(TARGET).py + +check: all + $(MAKE) -f $(TOP)/Makefile python_run diff --git a/trunk/Examples/python/std_vector/example.h b/trunk/Examples/python/std_vector/example.h new file mode 100644 index 000000000..4f0dac70d --- /dev/null +++ b/trunk/Examples/python/std_vector/example.h @@ -0,0 +1,25 @@ +/* File : example.h */ + +#include +#include +#include +#include + +double average(std::vector v) { + return std::accumulate(v.begin(),v.end(),0.0)/v.size(); +} + +std::vector half(const std::vector& v) { + std::vector w(v); + for (unsigned int i=0; i& v) { + // would you believe this is the same as the above? + std::transform(v.begin(),v.end(),v.begin(), + std::bind2nd(std::divides(),2.0)); +} + + diff --git a/trunk/Examples/python/std_vector/example.i b/trunk/Examples/python/std_vector/example.i new file mode 100644 index 000000000..aa58b66e0 --- /dev/null +++ b/trunk/Examples/python/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; + %template(DoubleVector) vector; +} + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/trunk/Examples/python/std_vector/runme.py b/trunk/Examples/python/std_vector/runme.py new file mode 100644 index 000000000..d248ccbbb --- /dev/null +++ b/trunk/Examples/python/std_vector/runme.py @@ -0,0 +1,36 @@ +# file: runme.py + +import example + +# Call average with a Python list... + +print example.average([1,2,3,4]) + +# ... or a wrapped std::vector + +v = example.IntVector(4) +for i in range(len(v)): + v[i] = i+1 +print example.average(v) + + +# half will return a Python list. +# Call it with a Python tuple... + +print example.half((1.0, 1.5, 2.0, 2.5, 3.0)) + +# ... or a wrapped std::vector + +v = example.DoubleVector() +for i in [1,2,3,4]: + v.append(i) +print example.half(v) + + +# now halve a wrapped std::vector in place + +example.halve_in_place(v) +for i in range(len(v)): + print v[i], "; ", +print + -- cgit v1.2.1