summaryrefslogtreecommitdiff
path: root/bordeaux/learning/multiclass_pa/jni/jni_multiclass_pa.cpp
blob: 3825586ede12ab41659bf3fae2238b5c2d010030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "jni/jni_multiclass_pa.h"
#include "native/multiclass_pa.h"

#include <vector>

using learningfw::MulticlassPA;
using std::vector;
using std::pair;

void CreateIndexValuePairs(const int* indices, const float* values,
                           const int length, vector<pair<int, float> >* pairs) {
  pairs->clear();

  for (int i = 0; i < length; ++i) {
    pair<int, float> new_pair(indices[i], values[i]);
    pairs->push_back(new_pair);
  }
}

jlong Java_android_bordeaux_learning_MulticlassPA_initNativeClassifier(JNIEnv* /* env */,
                                                       jobject /* thiz */,
                                                       jint num_classes,
                                                       jint num_dims,
                                                       jfloat aggressiveness) {
  MulticlassPA* classifier = new MulticlassPA(num_classes,
                                              num_dims,
                                              aggressiveness);
  return ((jlong) classifier);
}


jboolean Java_android_bordeaux_learning_MulticlassPA_deleteNativeClassifier(JNIEnv* /* env */,
                                                             jobject /* thiz */,
                                                             jlong paPtr) {
  MulticlassPA* classifier = (MulticlassPA*) paPtr;
  delete classifier;
  return JNI_TRUE;
}

jboolean Java_android_bordeaux_learning_MulticlassPA_nativeSparseTrainOneExample(JNIEnv* env,
                                                                  jobject /* thiz */,
                                                                  jintArray index_array,
                                                                  jfloatArray value_array,
                                                                  jint target,
                                                                  jlong paPtr) {
  MulticlassPA* classifier = (MulticlassPA*) paPtr;

  if (classifier && index_array && value_array) {

    jfloat* values = env->GetFloatArrayElements(value_array, NULL);
    jint* indices = env->GetIntArrayElements(index_array, NULL);
    const int value_len = env->GetArrayLength(value_array);
    const int index_len = env->GetArrayLength(index_array);

    if (values && indices && value_len == index_len) {
      vector<pair<int, float> > inputs;

      CreateIndexValuePairs(indices, values, value_len, &inputs);
      classifier->SparseTrainOneExample(inputs, target);
      env->ReleaseIntArrayElements(index_array, indices, JNI_ABORT);
      env->ReleaseFloatArrayElements(value_array, values, JNI_ABORT);

      return JNI_TRUE;
    }
    env->ReleaseIntArrayElements(index_array, indices, JNI_ABORT);
    env->ReleaseFloatArrayElements(value_array, values, JNI_ABORT);
  }

  return JNI_FALSE;
}


jint Java_android_bordeaux_learning_MulticlassPA_nativeSparseGetClass(JNIEnv* env,
                                                       jobject /* thiz */,
                                                       jintArray index_array,
                                                       jfloatArray value_array,
                                                       jlong paPtr) {

  MulticlassPA* classifier = (MulticlassPA*) paPtr;

  if (classifier && index_array && value_array) {

    jfloat* values = env->GetFloatArrayElements(value_array, NULL);
    jint* indices = env->GetIntArrayElements(index_array, NULL);
    const int value_len = env->GetArrayLength(value_array);
    const int index_len = env->GetArrayLength(index_array);

    if (values && indices && value_len == index_len) {
      vector<pair<int, float> > inputs;
      CreateIndexValuePairs(indices, values, value_len, &inputs);
      env->ReleaseIntArrayElements(index_array, indices, JNI_ABORT);
      env->ReleaseFloatArrayElements(value_array, values, JNI_ABORT);
      return classifier->SparseGetClass(inputs);
    }
    env->ReleaseIntArrayElements(index_array, indices, JNI_ABORT);
    env->ReleaseFloatArrayElements(value_array, values, JNI_ABORT);
  }

  return -1;
}