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_map/Makefile | 25 ++++++++++ trunk/Examples/python/std_map/example.h | 17 +++++++ trunk/Examples/python/std_map/example.i | 27 +++++++++++ trunk/Examples/python/std_map/runme.py | 82 +++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 trunk/Examples/python/std_map/Makefile create mode 100644 trunk/Examples/python/std_map/example.h create mode 100644 trunk/Examples/python/std_map/example.i create mode 100644 trunk/Examples/python/std_map/runme.py (limited to 'trunk/Examples/python/std_map') diff --git a/trunk/Examples/python/std_map/Makefile b/trunk/Examples/python/std_map/Makefile new file mode 100644 index 000000000..5d13da764 --- /dev/null +++ b/trunk/Examples/python/std_map/Makefile @@ -0,0 +1,25 @@ +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 + +run: + python runme.py + +check: all + $(MAKE) -f $(TOP)/Makefile python_run diff --git a/trunk/Examples/python/std_map/example.h b/trunk/Examples/python/std_map/example.h new file mode 100644 index 000000000..79ccc6478 --- /dev/null +++ b/trunk/Examples/python/std_map/example.h @@ -0,0 +1,17 @@ +/* File : example.h */ + +#include +#include + +template +std::map half_map(const std::map& v) { + typedef typename std::map::const_iterator iter; + std::map w; + for (iter i = v.begin(); i != v.end(); ++i) { + w[i->first] = (i->second)/2; + } + return w; +} + + + diff --git a/trunk/Examples/python/std_map/example.i b/trunk/Examples/python/std_map/example.i new file mode 100644 index 000000000..6a7af7108 --- /dev/null +++ b/trunk/Examples/python/std_map/example.i @@ -0,0 +1,27 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +%include std_string.i +%include std_pair.i +%include std_map.i + +/* instantiate the required template specializations */ +namespace std { + /* remember to instantiate the key,value pair! */ + %template(DoubleMap) map; + %template() map; +} + +/* Let's just grab the original header file here */ +%include "example.h" + +%template(halfd) half_map; +%template(halfi) half_map; + + +%template() std::pair; +%template(pymap) std::map; diff --git a/trunk/Examples/python/std_map/runme.py b/trunk/Examples/python/std_map/runme.py new file mode 100644 index 000000000..b521c9c9c --- /dev/null +++ b/trunk/Examples/python/std_map/runme.py @@ -0,0 +1,82 @@ +# file: runme.py + +import example + +pmap = example.pymap() +pmap["hi"] = 1 +pmap["hello"] = 2 + + + + +dmap = {} +dmap["hello"] = 1.0 +dmap["hi"] = 2.0 + +print dmap.items() +print dmap.keys() +print dmap.values() + +print dmap +hmap = example.halfd(dmap) +dmap = hmap + +print dmap +for i in dmap.iterkeys(): + print "key", i + +for i in dmap.itervalues(): + print "val", i + +for k,v in dmap.iteritems(): + print "item", k,v + +dmap = example.DoubleMap() +dmap["hello"] = 1.0 +dmap["hi"] = 2.0 + +for i in dmap.iterkeys(): + print "key", i + +for i in dmap.itervalues(): + print "val", i + +for k,v in dmap.iteritems(): + print "item", k,v + + +print dmap.items() +print dmap.keys() +print dmap.values() + +hmap = example.halfd(dmap) +print hmap.keys() +print hmap.values() + + + +dmap = {} +dmap["hello"] = 2 +dmap["hi"] = 4 + +hmap = example.halfi(dmap) +print hmap +print hmap.keys() +print hmap.values() + + +dmap = hmap + +for i in dmap.iterkeys(): + print "key", i + +for i in dmap.itervalues(): + print "val", i + +for i in dmap.iteritems(): + print "item", i + +for k,v in dmap.iteritems(): + print "item", k,v + +print dmap -- cgit v1.2.1