<commit_before>/*
 * Copyright (C) 2017 <NAME>
 *
 * 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 <cstddef>
#include <cstring>
#include <jni.h>

#include "JavaBridge.h"
#include "Raw.h"

using namespace Tehreer;

static jboolean isEqual(JNIEnv *env, jobject obj, jlong pointer, jlong other, jint size)
{
    jbyte *memory = reinterpret_cast<jbyte *>(pointer);
    jbyte *chunk = reinterpret_cast<jbyte *>(other);
    int result = memcmp(memory, chunk, static_cast<size_t>(size));

    return static_cast<jboolean>(result == 0);
}

static jint getHash(JNIEnv *env, jobject obj, jlong pointer, jint size)
{
    jbyte *memory = reinterpret_cast<jbyte *>(pointer);

    jint result = 1;
    for (size_t i = 0; i < size; i++) {
        result = 31 * result + memory[i];
    }

    return result;
}

static jbyte getByte(JNIEnv *env, jobject obj, jlong pointer, jint index)
{
    jbyte *memory = reinterpret_cast<jbyte *>(pointer);
    jbyte value = memory[index];

    return value;
}

static void putByte(JNIEnv *env, jobject obj, jlong pointer, jint index, jbyte value)
{
    jbyte *memory = reinterpret_cast<jbyte *>(pointer);
    memory[index] = value;
}

static jbyteArray toByteArray(JNIEnv *env, jobject obj, jlong pointer, jint size)
{
    jbyte *memory = reinterpret_cast<jbyte *>(pointer);
    jbyteArray array = env->NewByteArray(size);
    env->SetByteArrayRegion(array, 0, size, memory);

    return array;
}

static JNINativeMethod JNI_METHODS[] = {
    { "isEqual", "(JJI)Z", (void *)isEqual },
    { "getHash", "(JI)I", (void *)getHash },
    { "getByte", "(JI)B", (void *)getByte },
    { "putByte", "(JIB)V", (void *)putByte },
    { "toByteArray", "(JI)[B", (void *)toByteArray },
};

jint register_com_mta_tehreer_internal_Raw(JNIEnv *env)
{
    return JavaBridge::registerClass(env, "com/mta/tehreer/internal/Raw", JNI_METHODS, sizeof(JNI_METHODS) / sizeof(JNI_METHODS[0]));
}<commit_msg>[jni] Made raw class compatible with java...<commit_after>/*
 * Copyright (C) 2017 <NAME>
 *
 * 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 <cstddef>
#include <cstdint>
#include <cstring>
#include <jni.h>

#include "JavaBridge.h"
#include "Raw.h"

using namespace Tehreer;

static jboolean isEqual(JNIEnv *env, jobject obj, jlong pointer, jlong other, jint bufferSize)
{
    jbyte *memory = reinterpret_cast<jbyte *>(pointer);
    jbyte *chunk = reinterpret_cast<jbyte *>(other);
    int result = memcmp(memory, chunk, static_cast<size_t>(bufferSize));

    return static_cast<jboolean>(result == 0);
}

static jint getHash(JNIEnv *env, jobject obj, jlong pointer, jint bufferSize)
{
    jbyte *memory = reinterpret_cast<jbyte *>(pointer);

    jint result = 1;
    for (size_t i = 0; i < bufferSize; i++) {
        result = 31 * result + memory[i];
    }

    return result;
}

static jbyte getInt8(JNIEnv *env, jobject obj, jlong pointer, jint arrayIndex)
{
    int8_t *memory = reinterpret_cast<int8_t *>(pointer);
    jbyte value = memory[arrayIndex];

    return value;
}

static jint getUInt16(JNIEnv *env, jobject obj, jlong pointer, jint arrayIndex)
{
    uint16_t *memory = reinterpret_cast<uint16_t *>(pointer);
    jint value = memory[arrayIndex];

    return value;
}

static void putInt8(JNIEnv *env, jobject obj, jlong pointer, jint arrayIndex, jbyte value)
{
    int8_t *memory = reinterpret_cast<int8_t *>(pointer);
    memory[arrayIndex] = static_cast<int8_t>(value);
}

static void putUInt16(JNIEnv *env, jobject obj, jlong pointer, jint arrayIndex, jint value)
{
    uint16_t *memory = reinterpret_cast<uint16_t *>(pointer);
    memory[arrayIndex] = static_cast<uint16_t>(value);
}

static jbyteArray toInt8Array(JNIEnv *env, jobject obj, jlong arrayIndex, jint size)
{
    int8_t *memory = reinterpret_cast<int8_t *>(arrayIndex);
    jbyteArray array = env->NewByteArray(size);
    env->SetByteArrayRegion(array, 0, size, memory);

    return array;
}

static jintArray toUInt16Array(JNIEnv *env, jobject obj, jlong arrayIndex, jint size)
{
    uint16_t *memory = reinterpret_cast<uint16_t *>(arrayIndex);
    jintArray array = env->NewIntArray(size);
    jint *elements = env->GetIntArrayElements(array, nullptr);

    for (size_t i = 0; i < size; i++) {
        elements[i] = memory[i];
    }

    env->ReleaseIntArrayElements(array, elements, 0);

    return array;
}

static JNINativeMethod JNI_METHODS[] = {
    { "isEqual", "(JJI)Z", (void *)isEqual },
    { "getHash", "(JI)I", (void *)getHash },
    { "getInt8", "(JI)B", (void *)getInt8 },
    { "getUInt16", "(JI)I", (void *)getUInt16 },
    { "putInt8", "(JIB)V", (void *)putInt8 },
    { "putUInt16", "(JII)V", (void *)putUInt16 },
    { "toInt8Array", "(JI)[B", (void *)toInt8Array },
    { "toUInt16Array", "(JI)[I", (void *)toUInt16Array },
};

jint register_com_mta_tehreer_internal_Raw(JNIEnv *env)
{
    return JavaBridge::registerClass(env, "com/mta/tehreer/internal/Raw", JNI_METHODS, sizeof(JNI_METHODS) / sizeof(JNI_METHODS[0]));
}
{'fraction_non_alphanumeric': 0.13312123241795043, 'fraction_numerical': 0.013395847287340924, 'mean_word_length': 4.285840707964602, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '913926', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2027, 'n_words': 584}