diff options
| author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2009-08-18 20:56:02 +0000 |
|---|---|---|
| committer | Lorry <lorry@roadtrain.codethink.co.uk> | 2012-09-25 16:59:08 +0000 |
| commit | 9f8a09ed743cedd9547bf0661d518647966ab114 (patch) | |
| tree | 9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/ocaml/std_vector | |
| download | swig-tarball-9f8a09ed743cedd9547bf0661d518647966ab114.tar.gz | |
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/ocaml/std_vector')
| -rw-r--r-- | Examples/ocaml/std_vector/Makefile | 23 | ||||
| -rw-r--r-- | Examples/ocaml/std_vector/example.h | 25 | ||||
| -rw-r--r-- | Examples/ocaml/std_vector/example.i | 15 | ||||
| -rw-r--r-- | Examples/ocaml/std_vector/runme.ml | 35 |
4 files changed, 98 insertions, 0 deletions
diff --git a/Examples/ocaml/std_vector/Makefile b/Examples/ocaml/std_vector/Makefile new file mode 100644 index 0000000..e5a8017 --- /dev/null +++ b/Examples/ocaml/std_vector/Makefile @@ -0,0 +1,23 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = +TARGET = example +INTERFACE = example.i +PROGFILE = runme.ml + +all default:: static + +static:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \ + ocaml_static_cpp + +dynamic:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \ + ocaml_dynamic_cpp + +clean:: + $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' ocaml_clean + +check: all diff --git a/Examples/ocaml/std_vector/example.h b/Examples/ocaml/std_vector/example.h new file mode 100644 index 0000000..b753592 --- /dev/null +++ b/Examples/ocaml/std_vector/example.h @@ -0,0 +1,25 @@ +/* File : example.h -- stolen from the guile std_vector example */ + +#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/ocaml/std_vector/example.i b/Examples/ocaml/std_vector/example.i new file mode 100644 index 0000000..60285e5 --- /dev/null +++ b/Examples/ocaml/std_vector/example.i @@ -0,0 +1,15 @@ +/* -*- C++ -*- */ +/* File : example.i -- stolen from the guile std_vector example */ +%module example + +%{ +#include "example.h" +%} + +%include stl.i +/* instantiate the required template specializations */ +%template(IntVector) std::vector<int>; +%template(DoubleVector) std::vector<double>; + +/* Let's just grab the original header file here */ +%include "example.h" diff --git a/Examples/ocaml/std_vector/runme.ml b/Examples/ocaml/std_vector/runme.ml new file mode 100644 index 0000000..feb70dd --- /dev/null +++ b/Examples/ocaml/std_vector/runme.ml @@ -0,0 +1,35 @@ +(* This example was mostly lifted from the guile example directory *) + +open Swig +open Example + +let with_vector v f = + for i = 0 to ((v -> size()) as int) - 1 do + f v i + done + +let print_DoubleVector v = + begin + with_vector v + (fun v i -> + print_float ((v '[i to int]) as float) ; + print_string " ") ; + print_endline + end + +(* Call average with a Ocaml array... *) + +let v = new_DoubleVector '() +let rec fill_dv v x = + if x < 0.0001 then v else + begin + v -> push_back ((x to float)) ; + fill_dv v (x *. x) + end +let _ = fill_dv v 0.999 +let _ = print_DoubleVector v ; print_endline "" +let u = new_IntVector '() +let _ = for i = 1 to 4 do + u -> push_back ((i to int)) +done +let _ = (print_float ((_average u) as float) ; print_newline ()) |
