Skip to content

Commit

Permalink
Handle gsl_permutation* in SWIG.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencwilliams committed Jan 10, 2011
1 parent 27db9e4 commit ffdb4b2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions bct.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ namespace bct {
void gsl_free(gsl_vector* v);
void gsl_free(gsl_matrix* m);
void gsl_free(std::vector<gsl_matrix*>& m);
void gsl_free(gsl_permutation* p);
void init();
int number_of_edges_dir(const gsl_matrix* m);
int number_of_edges_und(const gsl_matrix* m);
Expand Down
3 changes: 3 additions & 0 deletions bct_gsl.i
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ namespace bct {
void gsl_free(gsl_vector* v);
void gsl_free(gsl_matrix* m);
void gsl_free(std::vector<gsl_matrix*>& m);
void gsl_free(gsl_permutation* p);
void init();
int number_of_edges_dir(const gsl_matrix* m);
int number_of_edges_und(const gsl_matrix* m);
Expand Down Expand Up @@ -380,6 +381,8 @@ namespace matlab {
PyObject* from_gsl(const gsl_vector* v);
PyObject* from_gsl(const gsl_matrix* m);
PyObject* from_gsl(const std::vector<gsl_matrix*>& m);
PyObject* from_gsl(const gsl_permutation* p);
gsl_vector* to_gslv(PyObject* list);
gsl_matrix* to_gslm(PyObject* list);
std::vector<gsl_matrix*> to_gsl3dm(PyObject* list);
gsl_permutation* to_gslp(PyObject* list);
5 changes: 5 additions & 0 deletions bct_py.i
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
%typemap(freearg) std::vector<gsl_matrix*> { bct::gsl_free($1); }
%typemap(out) std::vector<gsl_matrix*> { %append_output(from_gsl($1)); bct::gsl_free($1); }

%typemap(typecheck) gsl_permutation* { $1 = is_gslp($input) ? 1 : 0; }
%typemap(in) gsl_permutation* { $1 = to_gslp($input); }
%typemap(freearg) gsl_permutation* { bct::gsl_free($1); }
%typemap(out) gsl_permutation* { %append_output(from_gsl($1)); bct::gsl_free($1); }

%typemap(in, numinputs = 0) gsl_vector** (gsl_vector* temp) { $1 = &temp; }
%typemap(argout) gsl_vector** { %append_output(from_gsl(*$1)); bct::gsl_free(*$1); }

Expand Down
61 changes: 61 additions & 0 deletions swig.i
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
%{
#include <cmath>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_permutation.h>
#include <gsl/gsl_vector.h>
#include <limits>
#include <Python.h>
#include <vector>

Expand Down Expand Up @@ -43,6 +46,35 @@
return is_ndim_list(object, 3);
}

/*
* Checks if a PyObject* can be converted to a gsl_permutation*.
*/
bool is_gslp(PyObject* object) {
if (!is_ndim_list(object, 1)) {
return false;
}
int n = PyList_Size(object);
std::vector<bool> found(n, false);
for (int i = 0; i < n; i++) {
double value = PyFloat_AsDouble(PyList_GetItem(object, i));
if (value < 0.0) {
return false;
}
double int_part;
double frac_part = std::modf(value, &int_part);
if (frac_part > std::numeric_limits<double>::epsilon()) {
return false;
}
int index = (int)int_part;
if (index >= n || found[index]) {
return false;
} else {
found[index] = true;
}
}
return true;
}

/*
* Converts a gsl_vector* to a Python list.
*/
Expand Down Expand Up @@ -95,6 +127,18 @@
return list;
}

/*
* Converts a gsl_permutation* to a Python list.
*/
PyObject* from_gsl(const gsl_permutation* p) {
PyObject* list = PyList_New(p->size);
for (int i = 0; i < (int)p->size; i++) {
PyObject* value = PyFloat_FromDouble(gsl_permutation_get(p, i));
PyList_SetItem(list, i, value);
}
return list;
}

/*
* Converts a Python list to a gsl_vector*.
*/
Expand Down Expand Up @@ -151,4 +195,21 @@
}
return m;
}

/*
* Converts a Python list to a gsl_permutation*.
*/
gsl_permutation* to_gslp(PyObject* list) {
int n = PyList_Size(list);
gsl_permutation* p = gsl_permutation_alloc(n);
for (int i = 0; i < n; i++) {
p->data[i] = (int)PyFloat_AsDouble(PyList_GetItem(list, i));
}
if (gsl_permutation_valid(p) == 1) {
gsl_permutation_free(p);
return NULL;
} else {
return p;
}
}
%}
2 changes: 2 additions & 0 deletions utility.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "bct.h"
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_permutation.h>
#include <gsl/gsl_vector.h>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -28,6 +29,7 @@ void bct::gsl_free(std::vector<gsl_matrix*>& m) {
}
}
}
void bct::gsl_free(gsl_permutation* p) { gsl_permutation_free(p); }

/*
* Initializes the BCT library for external use.
Expand Down

0 comments on commit ffdb4b2

Please sign in to comment.