Advertisement
Guest User

Untitled

a guest
Apr 29th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. public class vector_test {
  2. static {
  3. System.loadLibrary("Features");
  4. }
  5.  
  6. public static void main(String argv[]) {
  7. FeaturesJNI.init_shogun__SWIG_4();
  8. double[] vec = {1,2,3,4};
  9. Labels x = new Labels(vec);
  10. double[] y = x.get_labels();
  11. for (int i = 0; i < 4; i++) {
  12. System.out.println(y[i]);
  13. }
  14. FeaturesJNI.exit_shogun();
  15. }
  16. }
  17.  
  18.  
  19. /*
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * Written (W) 2011 Baozeng Ding
  26. *
  27. */
  28.  
  29. /* TYPEMAP_IN macros
  30. *
  31. * This family of typemaps allows pure input C arguments of the form
  32. *
  33. * (type* IN_ARRAY1, int32_t DIM1)
  34. * (type* IN_ARRAY2, int32_t DIM1, int32_t DIM2)
  35. *
  36. * where "type" is any type supported by the numpy module, to be
  37. * called in python with an argument list of a single array (or any
  38. * python object that can be passed to the numpy.array constructor
  39. * to produce an arrayof te specified shape). This can be applied to
  40. * a existing functions using the %apply directive:
  41. *
  42. * %apply (float64_t* IN_ARRAY1, int32_t DIM1) {float64_t* series, int32_t length}
  43. * %apply (float64_t* IN_ARRAY2, int32_t DIM1, int32_t DIM2) {float64_t* mx, int32_t rows, int32_t cols}
  44. * float64_t sum(float64_t* series, int32_t length);
  45. * float64_t max(float64_t* mx, int32_t rows, int32_t cols);
  46. *
  47. * or with
  48. *
  49. * float64_t sum(float64_t* IN_ARRAY1, int32_t DIM1);
  50. * float64_t max(float64_t* IN_ARRAY2, int32_t DIM1, int32_t DIM2);
  51. */
  52.  
  53. /* One dimensional input arrays */
  54. %define TYPEMAP_IN1(SGTYPE, JTYPE, JAVATYPE, JNITYPE, JFUNCNAME, JNIDESC)
  55.  
  56. %typemap(jni) (SGTYPE* IN_ARRAY1, int32_t DIM1) %{JNITYPE##Array%}
  57. %typemap(jtype) (SGTYPE* IN_ARRAY1, int32_t DIM1) %{JTYPE[]%}
  58. %typemap(jstype) (SGTYPE* IN_ARRAY1, int32_t DIM1) %{JTYPE[]%}
  59.  
  60. %typemap(in) (SGTYPE* IN_ARRAY1, int32_t DIM1) (JNITYPE *jarr) {
  61. int i;
  62. SGTYPE *array;
  63. if (!$input) {
  64. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
  65. return $null;
  66. }
  67. $2 = JCALL1(GetArrayLength, jenv, $input);
  68. jarr = JCALL2(Get##JAVATYPE##ArrayElements, jenv, $input, 0);
  69. if (!jarr)
  70. return $null;
  71. array = new SGTYPE[$2];
  72. if (!array) {
  73. SWIG_JavaThrowException(jenv, SWIG_JavaOutOfMemoryError, "array memory allocation failed");
  74. return $null;
  75. }
  76. for (i = 0; i < $2; i++) {
  77. array[i] = jarr[i];
  78. }
  79. $1 = array;
  80. }
  81.  
  82. %typemap(out) (SGTYPE* IN_ARRAY1, int32_t DIM1) {
  83. JNITYPE *arr;
  84. int i;
  85. JNITYPE##Array jresult = JCALL1(New##JAVATYPE##Array, jenv, $2);
  86. if (!jresult)
  87. return NULL;
  88. arr = JCALL2(Get##JAVATYPE##ArrayElements, jenv, jresult, 0);
  89. if (!arr)
  90. return NULL;
  91. for (i=0; i < $2; i++)
  92. arr[i] = (JNITYPE)$1[i];
  93. JCALL3(Release##JAVATYPE##ArrayElements, jenv, jresult, arr, 0);
  94. $result = jresult;
  95. }
  96.  
  97. %typemap(freearg) (SGTYPE* IN_ARRAY1, int32_t DIM1) {
  98. delete [] $1;
  99. }
  100.  
  101. %typemap(javain) (SGTYPE* IN_ARRAY1, int32_t DIM1) "$javainput"
  102. %typemap(javaout) (SGTYPE* IN_ARRAY1, int32_t DIM1) {
  103. return $jnicall;
  104. }
  105.  
  106. %enddef
  107.  
  108.  
  109. //TYPEMAP_IN1(bool, boolean, Boolean, jboolean, Bool, "[Z") /* bool[ANY] */
  110. TYPEMAP_IN1(char, byte, Byte, jbyte, Schar, "[B") /* signed char[ANY] */
  111. TYPEMAP_IN1(uint8_t, short, Short, jshort, Uchar, "[S") /* unsigned char[ANY] */
  112. TYPEMAP_IN1(int16_t, short, Short, jshort, Short, "[S") /* short[ANY] */
  113. TYPEMAP_IN1(uint16_t, int, Int, jint, Ushort, "[I") /* unsigned short[ANY] */
  114. TYPEMAP_IN1(int32_t, int, Int, jint, Int, "[I") /* int[ANY] */
  115. TYPEMAP_IN1(uint32_t, long, Long, jlong, Uint, "[J") /* unsigned int[ANY] */
  116. TYPEMAP_IN1(int64_t, int, Int, jint, Long, "[I") /* long[ANY] */
  117. TYPEMAP_IN1(uint64_t, long, Long, jlong, Ulong, "[J") /* unsigned long[ANY] */
  118. //TYPEMAP_IN1(long long, long, Long, jlong, Longlong, "[J") /* long long[ANY] */
  119. TYPEMAP_IN1(float32_t, float, Float, jfloat, Float, "[F") /* float[ANY] */
  120. TYPEMAP_IN1(float64_t, double, Double, jdouble, Double, "[D") /* double[ANY] */
  121.  
  122. #undef TYPEMAP_IN1
  123.  
  124. /* TYPEMAP_ARGOUT macros
  125. *
  126. * This family of typemaps allows output C arguments of the form
  127. *
  128. * (type** ARGOUT_ARRAY)
  129. *
  130. * where "type" is any type supported by the numpy module, to be
  131. * called in python with an argument list of a single contiguous
  132. * numpy array. This can be applied to an existing function using
  133. * the %apply directive:
  134. *
  135. * %apply (float64_t** ARGOUT_ARRAY1, {(float64_t** series, int32_t* len)}
  136. * %apply (float64_t** ARGOUT_ARRAY2, {(float64_t** matrix, int32_t* d1, int32_t* d2)}
  137. *
  138. * with
  139. *
  140. * void sum(float64_t* series, int32_t* len);
  141. * void sum(float64_t** series, int32_t* len);
  142. * void sum(float64_t** matrix, int32_t* d1, int32_t* d2);
  143. *
  144. * where sum mallocs the array and assigns dimensions and the pointer
  145. *
  146. */
  147.  
  148. /* One dimensional input/output arrays */
  149. %define TYPEMAP_ARRAYOUT1(SGTYPE, JTYPE, JAVATYPE, JNITYPE, JFUNCNAME, JNIDESC)
  150.  
  151. %typemap(jni) (SGTYPE** ARGOUT1, int32_t* DIM1) %{JNITYPE##Array%}
  152. %typemap(jtype) (SGTYPE** ARGOUT1, int32_t* DIM1) %{JTYPE[]%}
  153. %typemap(jstype) (SGTYPE** ARGOUT1, int32_t* DIM1) %{JTYPE[]%}
  154.  
  155. %typemap(in, numinputs=0) (SGTYPE** ARGOUT1, int32_t* DIM1) {
  156. $1 = (SGTYPE**) malloc(sizeof(SGTYPE*));
  157. $2 = (int32_t*) malloc(sizeof(int32_t));
  158. }
  159.  
  160. %typemap(argout) (SGTYPE** ARGOUT1, int32_t* DIM1) {
  161. SGTYPE* vec = *$1;
  162. JNITYPE *arr;
  163. int i;
  164. JNITYPE##Array jresult = JCALL1(New##JAVATYPE##Array, jenv, *$2);
  165. if (!jresult)
  166. return;
  167. arr = JCALL2(Get##JAVATYPE##ArrayElements, jenv, jresult, 0);
  168. if (!arr)
  169. return;
  170. for (i=0; i < *$2; i++)
  171. arr[i] = (JNITYPE)vec[i];
  172. JCALL3(Release##JAVATYPE##ArrayElements, jenv, jresult, arr, 0);
  173. }
  174.  
  175. %typemap(javain) (SGTYPE** ARGOUT1, int32_t* DIM1) "$javainput"
  176. %typemap(javaout) (SGTYPE** ARGOUT1, int32_t* DIM1) {
  177. return $jnicall;
  178. }
  179.  
  180. %enddef
  181.  
  182.  
  183. //TYPEMAP_ARRAYOUT1(bool, boolean, Boolean, jboolean, Bool, "[Z") /* bool[ANY] */
  184. TYPEMAP_ARRAYOUT1(char, byte, Byte, jbyte, Schar, "[B") /* signed char[ANY] */
  185. TYPEMAP_ARRAYOUT1(uint8_t, short, Short, jshort, Uchar, "[S") /* unsigned char[ANY] */
  186. TYPEMAP_ARRAYOUT1(int16_t, short, Short, jshort, Short, "[S") /* short[ANY] */
  187. TYPEMAP_ARRAYOUT1(uint16_t, int, Int, jint, Ushort, "[I") /* unsigned short[ANY] */
  188. TYPEMAP_ARRAYOUT1(int32_t, int, Int, jint, Int, "[I") /* int[ANY] */
  189. TYPEMAP_ARRAYOUT1(uint32_t, long, Long, jlong, Uint, "[J") /* unsigned int[ANY] */
  190. TYPEMAP_ARRAYOUT1(int64_t, int, Int, jint, Long, "[I") /* long[ANY] */
  191. TYPEMAP_ARRAYOUT1(uint64_t, long, Long, jlong, Ulong, "[J") /* unsigned long[ANY] */
  192. //TYPEMAP_ARRAYOUT1(long long, long, Long, jlong, Longlong, "[J") /* long long[ANY] */
  193. TYPEMAP_ARRAYOUT1(float32_t, float, Float, jfloat, Float, "[F") /* float[ANY] */
  194. TYPEMAP_ARRAYOUT1(float64_t, double, Double, jdouble, Double, "[D") /* double[ANY] */
  195.  
  196. #undef TYPEMAP_ARRAYOUT1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement