Fix example for STL vectors

This commit is contained in:
Simon Marchetto 2013-06-03 15:21:46 +02:00
commit a61d829676
6 changed files with 84 additions and 90 deletions

View file

@ -4,13 +4,13 @@ SRCS = example.cpp
TARGET = example
INTERFACE = example.i
all::
all:
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean::
clean:
$(MAKE) -f $(TOP)/Makefile scilab_clean
rm -f *.sce *.so lib*lib.c *_wrap.cpp
rm -f *.sce *.so lib*lib.c *_wrap.cxx
check: all
$(MAKE) -f $(TOP)/Makefile scilab_run

View file

@ -1,19 +1,47 @@
/* File : example.cpp */
#include <iostream>
#include <vector>
#include "example.hxx"
class opt {
public:
void set_lower_bound(const std::vector<double> &v) {
double sum = 0;
std::cout << "coucou" << std::endl;
for (int i = 0; i < v.size(); i++) {
sum += v[i];
std::cout << v[i] << std::endl;
}
//return sum;
std::vector<double> create_dvector(const int size, const double value)
{
return std::vector<double>(size, value);
}
std::vector<int> create_ivector(const int size, const int value)
{
return std::vector<int>(size, value);
}
double sum_dvector(const std::vector<double> dvector)
{
double sum = 0;
for (int i = 0; i < dvector.size(); i++)
{
sum += dvector[i];
}
};
return sum;
}
int sum_ivector(const std::vector<int> ivector)
{
int sum = 0;
for (int i = 0; i < ivector.size(); i++)
{
sum += ivector[i];
}
return sum;
}
void concat_dvector(std::vector<double>& dvector, const std::vector<double> other_dvector)
{
dvector.insert(dvector.end(), other_dvector.begin(), other_dvector.end());
}
void concat_ivector(std::vector<int>& ivector, const std::vector<int> other_ivector)
{
ivector.insert(ivector.end(), other_ivector.begin(), other_ivector.end());
}

View file

@ -1,18 +1,16 @@
/* File : example.cpp */
/* File : example.hxx */
#include <vector>
namespace nlopt {
class opt {
public:
void set_lower_bound(const std::vector<double> &v) {
double sum = 0;
for (int i = 0; i < v.size(); i++) {
sum += v[i];
}
//return sum;
}
};
}
std::vector<double> create_dvector(const int size, const double value);
std::vector<int> create_ivector(const int size, const int value);
double sum_dvector(const std::vector<double> dvector);
int sum_ivector(const std::vector<int> ivector);
void concat_dvector(std::vector<double>& dvector, const std::vector<double> other_dvector);
void concat_ivector(std::vector<int>& ivector, const std::vector<int> other_ivector);

View file

@ -1,4 +1,5 @@
/* File : example.i */
%module example
%{
@ -6,8 +7,5 @@
%}
%include "std_vector.i"
namespace std {
%template(nlopt_doublevector) vector<double>;
};
%include "example.hxx";

View file

@ -1,58 +0,0 @@
// This file is released under the 3-clause BSD license. See COPYING-BSD.
// Generated by builder.sce : Please, do not edit this file
// ----------------------------------------------------------------------------
//
libexamplelib_path = get_absolute_file_path('loader.sce');
//
// ulink previous function with same name
[bOK, ilib] = c_link('libexamplelib');
if bOK then
ulink(ilib);
end
//
list_functions = [ 'delete_SciSwigIterator';
'SciSwigIterator_value';
'SciSwigIterator_incr';
'SciSwigIterator_decr';
'SciSwigIterator_distance';
'SciSwigIterator_equal';
'SciSwigIterator_copy';
'SciSwigIterator_next';
'SciSwigIterator_previous';
'SciSwigIterator_advance';
'nlopt_doublevector_pop';
'nlopt_doublevector___paren__';
'nlopt_doublevector___paren_asgn__';
'nlopt_doublevector_append';
'nlopt_doublevector_empty';
'nlopt_doublevector_size';
'nlopt_doublevector_clear';
'nlopt_doublevector_swap';
'nlopt_doublevector_get_allocator';
'nlopt_doublevector_begin';
'nlopt_doublevector_end';
'nlopt_doublevector_rbegin';
'nlopt_doublevector_rend';
'nlopt_doublevector_pop_back';
'nlopt_doublevector_erase';
'new_nlopt_doublevector';
'nlopt_doublevector_push_back';
'nlopt_doublevector_front';
'nlopt_doublevector_back';
'nlopt_doublevector_assign';
'nlopt_doublevector_resize';
'nlopt_doublevector_insert';
'nlopt_doublevector_reserve';
'nlopt_doublevector_capacity';
'delete_nlopt_doublevector';
'opt_set_lower_bound';
'new_opt';
'delete_opt';
];
addinter(libexamplelib_path + filesep() + 'libexamplelib' + getdynlibext(), 'libexamplelib', list_functions);
// remove temp. variables on stack
clear libexamplelib_path;
clear bOK;
clear ilib;
clear list_functions;
// ----------------------------------------------------------------------------

View file

@ -0,0 +1,28 @@
exec loader.sce;
disp("this is an example with vectors of double.");
disp("create the vector of double {2.0, 2.0, 2.0, 2.0}:");
dv = create_dvector(4, 2.0);
disp(dv);
ds = sum_dvector(dv);
disp("sum of this vector elements is:")
disp(ds);
dv2 = create_dvector(2, 5.0);
disp("concat this vector with the vector of double {5.0, 5.0}:");
dv3 = concat_dvector(dv, dv2);
disp(dv3);
disp("now an example with vectors of int.");
disp("create the vector of int {3, 3, 3}:");
iv = create_ivector(3, 3);
disp(iv);
is = sum_ivector(iv);
disp("sum of this vector elements is:");
disp(is);
iv2 = create_ivector(2, 1);
disp("concat this vector with the vector of int {1, 1}:");
iv3 = concat_ivector(iv, iv2);
disp(iv3);