Skip to content

Commit

Permalink
Merge pull request #474 from gsomix/big_deal_with_sgvector
Browse files Browse the repository at this point in the history
add SGVector& typemaps for 'smooth' transition
  • Loading branch information
Soeren Sonnenburg committed Apr 20, 2012
2 parents 48b3ffd + b9a0b96 commit d6cc3eb
Show file tree
Hide file tree
Showing 7 changed files with 512 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/interfaces/csharp_modular/swig_typemaps.i
Expand Up @@ -87,6 +87,52 @@ TYPEMAP_SGVECTOR(float64_t, double, double)

#undef TYPEMAP_SGVECTOR

%define TYPEMAP_SGVECTOR_REF(SGTYPE, CTYPE, CSHARPTYPE)

%typemap(ctype, out="CTYPE*") shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& %{int size, CTYPE*%}
%typemap(imtype, out="IntPtr", inattributes="int size, [MarshalAs(UnmanagedType.LPArray)]") shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& %{CSHARPTYPE[]%}
%typemap(cstype) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& %{CSHARPTYPE[]%}

%typemap(in) shogun::SGVector<SGTYPE>& (SGVector<SGTYPE> temp), const shogun::SGVector<SGTYPE>& (SGVector<SGTYPE> temp) {
int32_t i;
SGTYPE *array;

if (!$input) {
SWIG_CSharpSetPendingException(SWIG_CSharpNullReferenceException, "null array");
return $null;
}

array = SG_MALLOC(SGTYPE, size);

if (!array) {
SWIG_CSharpSetPendingException(SWIG_CSharpOutOfMemoryException, "array memory allocation failed");
return $null;
}
for (i = 0; i < size; i++) {
array[i] = (SGTYPE)$input[i];
}

temp = shogun::SGVector<SGTYPE>((SGTYPE *)array, size);
$1 = &temp;
}

%typemap(csin) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>&"$csinput.Length, $csinput"
%enddef

TYPEMAP_SGVECTOR_REF(char, signed char, byte)
TYPEMAP_SGVECTOR_REF(uint8_t, unsigned char, byte)
TYPEMAP_SGVECTOR_REF(int16_t, short, short)
TYPEMAP_SGVECTOR_REF(uint16_t, unsigned short, short)
TYPEMAP_SGVECTOR_REF(int32_t, int, int)
TYPEMAP_SGVECTOR_REF(uint32_t, unsigned int, int)
TYPEMAP_SGVECTOR_REF(int64_t, long, int)
TYPEMAP_SGVECTOR_REF(uint64_t, unsigned long, long)
TYPEMAP_SGVECTOR_REF(long long, long long, long)
TYPEMAP_SGVECTOR_REF(float32_t, float, float)
TYPEMAP_SGVECTOR_REF(float64_t, double, double)

#undef TYPEMAP_SGVECTOR_REF

%define TYPEMAP_SGMATRIX(SGTYPE, CTYPE, CSHARPTYPE)

%typemap(ctype, out="CTYPE*") shogun::SGMatrix<SGTYPE> %{int rows, int cols, CTYPE*%}
Expand Down
178 changes: 178 additions & 0 deletions src/interfaces/java_modular/swig_typemaps.i
Expand Up @@ -165,6 +165,89 @@ TYPEMAP_SGVECTOR(float64_t, double, Double, jdouble, "()[D", "org/jblas/DoubleMa

#undef TYPEMAP_SGVECTOR

/* Two dimensional input/output arrays (references) */
%define TYPEMAP_SGVECTOR_REF(SGTYPE, JTYPE, JAVATYPE, JNITYPE, TOARRAY, CLASSDESC, CONSTRUCTOR)

%typemap(jni) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& %{jobject%}
%typemap(jtype) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& %{##JAVATYPE##Matrix%}
%typemap(jstype) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& %{##JAVATYPE##Matrix%}

%typemap(in) shogun::SGVector<SGTYPE>& (SGVector<SGTYPE> temp), const shogun::SGVector<SGTYPE>& (SGVector<SGTYPE> temp)
{
jclass cls;
jmethodID mid;
SGTYPE *array;
##JNITYPE##Array jarr;
JNITYPE *carr;
int32_t i, cols;
bool isVector;

if (!$input) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
return $null;
}

cls = JCALL1(GetObjectClass, jenv, $input);
if (!cls)
return $null;

mid = JCALL3(GetMethodID, jenv, cls, "isVector", "()Z");
if (!mid)
return $null;

isVector = (int32_t)JCALL2(CallIntMethod, jenv, $input, mid);
if (!isVector) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "vector expected");
return $null;
}

mid = JCALL3(GetMethodID, jenv, cls, "getColumns", "()I");
if (!mid)
return $null;

cols = (int32_t)JCALL2(CallIntMethod, jenv, $input, mid);
if (cols < 1) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null vector");
return $null;
}

mid = JCALL3(GetMethodID, jenv, cls, "toArray", TOARRAY);
if (!mid)
return $null;

jarr = (##JNITYPE##Array)JCALL2(CallObjectMethod, jenv, $input, mid);
carr = JCALL2(Get##JAVATYPE##ArrayElements, jenv, jarr, 0);
array = SG_MALLOC(SGTYPE, cols);
for (i = 0; i < cols; i++) {
array[i] = (SGTYPE)carr[i];
}

JCALL3(Release##JAVATYPE##ArrayElements, jenv, jarr, carr, 0);

temp = shogun::SGVector<SGTYPE>((SGTYPE *)array, cols);
$1 = &temp;
}

%typemap(javain) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& "$javainput"

%enddef

/*Define concrete examples of the TYPEMAP_SGVECTOR_REF macros */
TYPEMAP_SGVECTOR_REF(bool, boolean, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(char, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(uint8_t, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(int16_t, short, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(uint16_t, int, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(int32_t, int, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(uint32_t, long, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(int64_t, int, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(uint64_t, long, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(long long, long, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR_REF(float32_t, float, Float, jfloat, "()[F", "org/jblas/FloatMatrix","(II[F)V")
TYPEMAP_SGVECTOR_REF(float64_t, double, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")

#undef TYPEMAP_SGVECTOR_REF

#else
#ifdef HAVE_UJMP
/* Two dimensional input/output arrays */
Expand Down Expand Up @@ -301,6 +384,101 @@ TYPEMAP_SGVECTOR(float64_t, double, Double, jdouble, "toDoubleArray", "()[[D", "

#undef TYPEMAP_SGVECTOR

/* Two dimensional input/output arrays */
%define TYPEMAP_SGVECTOR_REF(SGTYPE, JTYPE, JAVATYPE, JNITYPE, TOARRAYMETHOD, TOARRAYDESC, CLASSDESC, CONSTRUCTOR)

%typemap(jni) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& %{jobject%}
%typemap(jtype) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& %{Matrix%}
%typemap(jstype) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& %{Matrix%}

%typemap(in) shogun::SGVector<SGTYPE>& (SGVector<SGTYPE> temp), const shogun::SGVector<SGTYPE>& (SGVector<SGTYPE> temp)
{
jclass cls;
jmethodID mid;
SGTYPE *array;
jobjectArray jobj;
##JNITYPE##Array jarr;
JNITYPE *carr;
bool isVector;
int32_t i, rows, cols;

if (!$input) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
return $null;
}

cls = JCALL1(GetObjectClass, jenv, $input);
if (!cls)
return $null;

mid = JCALL3(GetMethodID, jenv, cls, "isRowVector", "()Z");
if (!mid)
return $null;

isVector = (int32_t)JCALL2(CallIntMethod, jenv, $input, mid);
if (!isVector) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "vector expected");
return $null;
}

mid = JCALL3(GetMethodID, jenv, cls, " getColumnCount", "()I");
if (!mid)
return $null;

cols = (int32_t)JCALL2(CallIntMethod, jenv, $input, mid);
if (cols < 1) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null vector");
return $null;
}

mid = JCALL3(GetMethodID, jenv, cls, TOARRAYMETHOD, TOARRAYDESC);
if (!mid)
return $null;

jobj = (jobjectArray)JCALL2(CallObjectMethod, jenv, $input, mid);
jarr = (JNITYPE##Array)JCALL2(GetObjectArrayElement, jenv, jobj, 0);
if (!jarr) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
return $null;
}

carr = JCALL2(Get##JAVATYPE##ArrayElements, jenv, jarr, 0);
array = SG_MALLOC(SGTYPE, cols);
if (!array) {
SWIG_JavaThrowException(jenv, SWIG_JavaOutOfMemoryError, "array memory allocation failed");
return $null;
}

for (i = 0; i < cols; i++) {
array[j] = carr[i];
}

JCALL3(Release##JAVATYPE##ArrayElements, jenv, jarr, carr, 0);

temp = shogun::SGVector<SGTYPE>((SGTYPE *)array, cols);
$1 = &temp;
}

%typemap(javain) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& "$javainput"

%enddef

/*Define concrete examples of the TYPEMAP_SGVECTOR_REF macros */
TYPEMAP_SGVECTOR_REF(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V")
TYPEMAP_SGVECTOR_REF(char, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V")
TYPEMAP_SGVECTOR_REF(uint8_t, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V")
TYPEMAP_SGVECTOR_REF(int16_t, short, Short, jshort, "toShortArray", "()[[S", "org/ujmp/core/shortmatrix/impl/DefaultDenseShortMatrix2D", "([SII)V")
TYPEMAP_SGVECTOR_REF(uint16_t, int, Int, jint, "toIntArray", "()[[I", "org/ujmp/core/intmatrix/impl/DefaultDenseIntMatrix2D", "([III)V")
TYPEMAP_SGVECTOR_REF(int32_t, int, Int, jint, "toIntArray", "()[[I", "org/ujmp/core/intmatrix/impl/DefaultDenseIntMatrix2D", "([III)V")
TYPEMAP_SGVECTOR_REF(uint32_t, long, Long, jlong, "toLongArray", "()[[J", "org/ujmp/core/longmatrix/impl/DefaultDenseLongMatrix2D", "([JII)V")
TYPEMAP_SGVECTOR_REF(int64_t, int, Int, jint, "toIntArray", "()[[I", "org/ujmp/core/intmatrix/impl/DefaultDenseIntMatrix2D", "([III)V")
TYPEMAP_SGVECTOR_REF(uint64_t, long, Long, jlong, "toLongArray", "()[[J", "org/ujmp/core/longmatrix/impl/DefaultDenseLongMatrix2D", "([JII)V")
TYPEMAP_SGVECTOR_REF(long long, long, Long, jlong, "toLongArray", "()[[J", "org/ujmp/core/longmatrix/impl/DefaultDenseLongMatrix2D", "([JII)V")
TYPEMAP_SGVECTOR_REF(float32_t, float, Float, jfloat, "toFloatArray", "()[[F", "org/ujmp/core/floatmatrix/impl/DefaultDenseFloatMatrix2D", "([FII)V")
TYPEMAP_SGVECTOR_REF(float64_t, double, Double, jdouble, "toDoubleArray", "()[[D", "org/ujmp/core/doublematrix/impl/DefaultDenseDoubleMatrix2D", "([DII)V")

#undef TYPEMAP_SGVECTOR_REF

#endif
#endif

Expand Down
120 changes: 120 additions & 0 deletions src/interfaces/lua_modular/swig_typemaps.i
Expand Up @@ -151,6 +151,126 @@ TYPEMAP_SGVECTOR(float64_t)
SWIG_arg++;
}

/* One dimensional input arrays (references) */
%define TYPEMAP_SGVECTOR_REF(SGTYPE)

%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) shogun::SGVector<SGTYPE>&, const shogun::SGVector<SGTYPE>& {
if(!lua_istable(L, $input)) {
luaL_typerror(L, $input, "vector");
$1 = 0;
}
else {
$1 = 1;
int numitems = 0;
numitems = lua_objlen(L, $input);
if(numitems == 0) {
luaL_argerror(L, $input, "empty vector");
$1 = 0;
}
}
}

%typemap(in) shogun::SGVector<SGTYPE>& (SGVector<SGTYPE> temp), const shogun::SGVector<SGTYPE>& (SGVector<SGTYPE> temp) {
SGTYPE *array;
int32_t i, len;

if (!lua_istable(L, $input)) {
luaL_typerror(L, $input, "vector");
return 0;
}

len = lua_objlen(L, $input);
if (len == 0){
luaL_argerror(L, $input, "empty vector");
return 0;
}

array = SG_MALLOC(SGTYPE, len);
for ( i = 0; i < len; i++) {
lua_rawgeti(L, $input, i + 1);
if (lua_isnumber(L, -1)){
array[i] = (SGTYPE)lua_tonumber(L, -1);
}
else {
lua_pop(L, 1);
luaL_argerror(L, $input, "vector must contain numbers");
delete [] array;
return 0;
}
lua_pop(L,1);
}

temp = shogun::SGVector<SGTYPE>((SGTYPE *)array, len);
$1 = &temp;
}

%enddef

/* Define concrete examples of the TYPEMAP_SGVECTOR_REF macros */
TYPEMAP_SGVECTOR_REF(uint8_t)
TYPEMAP_SGVECTOR_REF(int32_t)
TYPEMAP_SGVECTOR_REF(int16_t)
TYPEMAP_SGVECTOR_REF(uint16_t)
TYPEMAP_SGVECTOR_REF(float32_t)
TYPEMAP_SGVECTOR_REF(float64_t)

#undef TYPEMAP_SGVECTOR_REF

%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) shogun::SGVector<char>&, const shogun::SGVector<char>& {
if(!lua_istable(L, $input)) {
luaL_typerror(L, $input, "vector");
$1 = 0;
}
else {
$1 = 1;
int numitems = 0;
numitems = lua_objlen(L, $input);
if(numitems == 0) {
luaL_argerror(L, $input, "empty vector");
$1 = 0;
}
}
}

%typemap(in) shogun::SGVector<char>& (SGVector<char> temp), const shogun::SGVector<char>& (SGVector<char> temp) {
char *array;
int32_t i, len;

if (!lua_istable(L, $input)) {
luaL_typerror(L, $input, "vector");
return 0;
}

len = lua_objlen(L, $input);
if (len == 0){
luaL_argerror(L, $input, "empty vector");
return 0;
}

array = SG_MALLOC(char, len);
for (i = 0; i < len; i++) {
lua_rawgeti(L, $input, i + 1);
if (lua_isstring(L, -1)){
len = 0;
const char *str = lua_tolstring(L, -1, (size_t *)&len);
if (len != 1) {
luaL_argerror(L, $input, "no more than one charactor expected");
}
array[i] = (char)*str;
}
else {
lua_pop(L, 1);
luaL_argerror(L, $input, "char vector expected");
delete [] array;
return 0;
}
lua_pop(L,1);
}

temp = shogun::SGVector<char>((char *)array, len);
$1 = &temp;
}

/* Two dimensional input/output arrays */
%define TYPEMAP_SGMATRIX(SGTYPE)

Expand Down

0 comments on commit d6cc3eb

Please sign in to comment.