Skip to content

Commit

Permalink
我只会在 心里偷偷地哭 你不会看到我泪水流出 明白自己究竟有多苦 输了自己 也输了全部
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Apr 24, 2017
1 parent 56da1cf commit 16302dd
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 155 deletions.
2 changes: 1 addition & 1 deletion jni/cpp-test/clean.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@echo off

if exist *.exe (
if get *.exe (
del /s /f /q *.exe
)

Expand Down
48 changes: 19 additions & 29 deletions jni/global/trie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ using algo4j_trie::Trie;

#define __goto_end_force(word) \
for (auto _ = 0; _ < len; ++_) { \
if (now->getNext(word[_]) == nullptr) \
if (nullptr == now->getNext(word[_])) \
now->setNext(word[_], new Node()); \
now = now->getNext(word[_]); \
}

#define __goto_end_weak(word) \
#define __goto_end_weak(word, value) \
for (auto _ = 0; _ < len; ++_) { \
if (now->getNext(word[_]) == nullptr) \
return false; \
if (nullptr == now->getNext(word[_])) \
return value; \
now = now->getNext(word[_]); \
}

#define __index(sym) ((sym) - 32)

algo4j_trie::Node::Node() : hasElement(false), next(new ptr_to<Node>[TRIE_NODE_SIZE]{}) { }
algo4j_trie::Node::Node() : obj(nullptr), next(new ptr_to<Node>[TRIE_NODE_SIZE]{}) { }

algo4j_trie::Node::~Node() {
for (auto _ = 0; _ < TRIE_NODE_SIZE; ++_) {
Expand All @@ -39,41 +39,31 @@ algo4j_trie::Trie::~Trie() {
delete head;
}

auto algo4j_trie::Trie::insert(const jbyte *word, const jsize len) -> void {
Node *now = head;
auto algo4j_trie::Trie::put(const jbyte *word, const jsize len, const jobject obj) -> void {
auto now = head;
__goto_end_force(word);
now->hasElement = true;
now->obj = obj;
}

auto algo4j_trie::Trie::exist(const jbyte *word, const jsize len) -> bool {
Node *now = head;
__goto_end_weak(word);
return now->hasElement;
auto algo4j_trie::Trie::get(const jbyte *word, const jsize len) -> jobject {
auto now = head;
__goto_end_weak(word, nullptr);
return now->obj;
}

auto algo4j_trie::Trie::existPrefix(const jbyte *word, const jsize len) -> bool {
Node *now = head;
__goto_end_weak(word);
return true;
}

auto Trie::getHead() const -> Node * {
return head;
}

auto Trie::remove(const jbyte *word, const jsize len) -> void {
Node *now = head;
auto Trie::remove(const jbyte *word, const jsize len) -> jobject {
auto now = head;
__goto_end_force(word);
now->hasElement = false;
auto ret = now->obj;
now->obj = nullptr;
return ret;
}

auto algo4j_trie::Node::setNext(jbyte sym, Node *newNode) -> void {
// printf("%d\n", __index(sym));
auto algo4j_trie::Node::setNext(jbyte sym, ptr_to<Node> newNode) -> void {
next[__index(sym)] = newNode;
}

auto algo4j_trie::Node::getNext(jbyte sym) -> Node * {
// printf("%d\n", __index(sym));
auto algo4j_trie::Node::getNext(jbyte sym) -> ptr_to<Node> {
return next[__index(sym)];
}

Expand Down
16 changes: 6 additions & 10 deletions jni/global/trie.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ namespace algo4j_trie {
private:
ptr_to<Node> *next;
public:
bool hasElement;
jobject obj;

explicit Node();

~Node();

auto setNext(jbyte, Node *) -> void;
auto setNext(jbyte, ptr_to<Node>) -> void;

auto getNext(jbyte) -> Node *;
auto getNext(jbyte) -> ptr_to<Node>;
};

class Trie {
Expand All @@ -37,15 +37,11 @@ namespace algo4j_trie {

~Trie();

auto insert(const jbyte *, const jsize) -> void;
auto put(const jbyte *, const jsize, const jobject) -> void;

auto remove(const jbyte *, const jsize) -> void;
auto remove(const jbyte *, const jsize) -> jobject;

auto exist(const jbyte *, const jsize) -> bool;

auto existPrefix(const jbyte *, const jsize) -> bool;

auto getHead() const -> Node *;
auto get(const jbyte *, const jsize) -> jobject;
};
}

Expand Down
38 changes: 11 additions & 27 deletions jni/tree/Trie.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

#include "../global/trie.h"
#include "../global/basics.hpp"
#include "Trie.h"

#include <stdio.h>

#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"

Expand All @@ -24,15 +21,17 @@ JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_deleteTrie(
delete (Trie *) ptr;
}

JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_insert(
JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_set(
JNIEnv *env,
jobject,
jlong ptr,
jbyteArray _word) -> void {
jbyteArray _word,
jobject _obj) -> void {
__JNI__FUNCTION__INIT__
auto trie = (Trie *) ptr;
__get(Byte, word);
trie->insert(word, __len(word));
auto obj = env->NewGlobalRef(_obj);
trie->put(word, __len(word), obj);
__abort(Byte, word);
__JNI__FUNCTION__CLEAN__
}
Expand All @@ -45,39 +44,24 @@ JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_remove(
__JNI__FUNCTION__INIT__
auto trie = (Trie *) ptr;
__get(Byte, word);
trie->remove(word, __len(word));
__abort(Byte, word);
__JNI__FUNCTION__CLEAN__
}

JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_contains(
JNIEnv *env,
jobject,
jlong ptr,
jbyteArray _word) -> jboolean {
__JNI__FUNCTION__INIT__
auto trie = (Trie *) ptr;
__get(Byte, word);
auto ret = trie->exist(word, __len(word));
auto ref = trie->remove(word, __len(word));
env->DeleteGlobalRef(ref);
__abort(Byte, word);
__JNI__FUNCTION__CLEAN__
return ret ? JNI_TRUE : JNI_FALSE;
}

JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_containsPrefix(
JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_get(
JNIEnv *env,
jobject,
jlong ptr,
jbyteArray _word) -> jboolean {
jbyteArray _word) -> jobject {
__JNI__FUNCTION__INIT__
auto trie = (Trie *) ptr;
__get(Byte, word);
auto ret = trie->existPrefix(word, __len(word));
auto ret = trie->get(word, __len(word));
__abort(Byte, word);
__JNI__FUNCTION__CLEAN__
return ret ? JNI_TRUE : JNI_FALSE;
return ret;
}



#pragma clang diagnostic pop
31 changes: 12 additions & 19 deletions jni/tree/Trie.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "jni.h"
/* Header for class org_algo4j_tree_Trie */

#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
#ifndef _Included_org_algo4j_tree_Trie
#define _Included_org_algo4j_tree_Trie

Expand All @@ -27,19 +29,20 @@ JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_createTrie(
JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_deleteTrie(
JNIEnv *,
jclass,
jlong
jlong
) -> void;

/**
* Class: org_algo4j_tree_Trie
* Method: insert
* Signature: (J[B)V
* Signature: (J[BLjava/lang/Object;)V
*/
JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_insert(
JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_set(
JNIEnv *,
jobject,
jlong,
jbyteArray
jbyteArray,
jobject
) -> void;

/**
Expand All @@ -57,30 +60,20 @@ JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_remove(
/**
* Class: org_algo4j_tree_Trie
* Method: contains
* Signature: (J[B)Z
* Signature: (J[B)Ljava/lang/Object;
*/
JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_contains(
JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_get(
JNIEnv *,
jobject,
jlong,
jbyteArray
) -> jboolean;

/**
* Class: org_algo4j_tree_Trie
* Method: containsPrefix
* Signature: (J[B)Z
*/
JNIEXPORT auto JNICALL Java_org_algo4j_tree_Trie_containsPrefix(
JNIEnv *,
jobject,
jlong,
jbyteArray
) -> jboolean;
) -> jobject;

#ifdef __cplusplus
}
#endif /// __cplusplus

#endif /// _Included_org_algo4j_tree_Trie


#pragma clang diagnostic pop
Loading

0 comments on commit 16302dd

Please sign in to comment.