Created
July 26, 2019 05:23
-
-
Save ninjakx/d3e89206dbb9651736c87e7a907fbcfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%module myknn | |
#define SWIGPYTHON_BUILTIN | |
%{ | |
#include "numpy/arrayobject.h" | |
#define SWIG_FILE_WITH_INIT /* To import_array() below */ | |
#include "myknn.h" | |
%} | |
%include "std_map.i" | |
%import "std_vector.i" | |
%include "numpy.i" | |
%init %{ | |
import_array(); | |
%} | |
%apply (double* IN_ARRAY2, int DIM1, int DIM2) { | |
(const double* array, int m, int n) | |
} | |
namespace std { | |
%template(IntVector) vector<int>; | |
} | |
namespace std { | |
%template (mapiv) std::map<int,vector<int> >; | |
} | |
%typemap(out) std::map<int,std::vector<int> >{ | |
$result = PyDict_New(); | |
std::map<int,std::vector<int> >::iterator iter; | |
int* theVal; | |
int theKey; | |
for (iter = $1.begin(); iter != $1.end(); ++iter) { | |
theKey = iter->first; | |
int subLength = iter->second.size(); | |
npy_intp dims[1] = {subLength}; | |
int *ans = new int[subLength]; | |
memcpy (ans, iter->second.data(), sizeof(int) * subLength); | |
PyObject *values = PyArray_SimpleNewFromData(1, &dims[0], NPY_INT, ans); | |
PyDict_SetItem($result, PyInt_FromLong(theKey), values); | |
} | |
}; | |
%include "myknn.h" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment