react-native-jieba 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/Jieba.podspec +17 -1
- package/README.md +70 -16
- package/android/CMakeLists.txt +3 -0
- package/android/build.gradle +32 -0
- package/android/generated/java/com/jieba/NativeJiebaSpec.java +4 -0
- package/android/generated/jni/JiebaSpec-generated.cpp +6 -0
- package/android/generated/jni/react/renderer/components/JiebaSpec/JiebaSpecJSI.h +8 -0
- package/android/src/main/java/com/jieba/JiebaAndroidHelperModule.kt +7 -22
- package/android/src/main/java/com/jieba/JiebaDict.kt +82 -0
- package/android/src/main/java/com/jieba/JiebaPackage.kt +4 -0
- package/cpp/JiebaDictAndroid.cpp +26 -0
- package/cpp/JiebaDictAndroid.h +14 -0
- package/cpp/JiebaEngine.cpp +18 -0
- package/cpp/JiebaEngine.h +128 -0
- package/cpp/JiebaImpl.cpp +31 -6
- package/cpp/JiebaImpl.h +4 -3
- package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec-generated.mm +7 -0
- package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec.h +1 -0
- package/ios/generated/ReactCodegen/JiebaSpecJSI.h +8 -0
- package/lib/module/NativeJieba.js.map +1 -1
- package/lib/module/NativeJieba.web.js +8 -2
- package/lib/module/NativeJieba.web.js.map +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/init.js +33 -6
- package/lib/module/init.js.map +1 -1
- package/lib/module/init.web.js +19 -7
- package/lib/module/init.web.js.map +1 -1
- package/lib/module/jieba.js +12 -0
- package/lib/module/jieba.js.map +1 -1
- package/lib/typescript/src/NativeJieba.d.ts +1 -0
- package/lib/typescript/src/NativeJieba.d.ts.map +1 -1
- package/lib/typescript/src/NativeJieba.web.d.ts +1 -0
- package/lib/typescript/src/NativeJieba.web.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/init.d.ts +25 -2
- package/lib/typescript/src/init.d.ts.map +1 -1
- package/lib/typescript/src/init.web.d.ts +17 -3
- package/lib/typescript/src/init.web.d.ts.map +1 -1
- package/lib/typescript/src/jieba.d.ts +11 -0
- package/lib/typescript/src/jieba.d.ts.map +1 -1
- package/package.json +8 -1
- package/src/NativeJieba.ts +1 -0
- package/src/NativeJieba.web.ts +11 -3
- package/src/index.tsx +2 -1
- package/src/init.ts +35 -7
- package/src/init.web.ts +30 -9
- package/src/jieba.ts +11 -0
- package/cpp/cppjieba/LICENSE +0 -20
- package/cpp/cppjieba/deps/limonp/LICENSE +0 -20
- package/cpp/cppjieba/deps/limonp/include/limonp/ArgvContext.hpp +0 -70
- package/cpp/cppjieba/deps/limonp/include/limonp/Closure.hpp +0 -206
- package/cpp/cppjieba/deps/limonp/include/limonp/Colors.hpp +0 -31
- package/cpp/cppjieba/deps/limonp/include/limonp/Condition.hpp +0 -38
- package/cpp/cppjieba/deps/limonp/include/limonp/Config.hpp +0 -103
- package/cpp/cppjieba/deps/limonp/include/limonp/ForcePublic.hpp +0 -7
- package/cpp/cppjieba/deps/limonp/include/limonp/LocalVector.hpp +0 -139
- package/cpp/cppjieba/deps/limonp/include/limonp/Logging.hpp +0 -92
- package/cpp/cppjieba/deps/limonp/include/limonp/NonCopyable.hpp +0 -21
- package/cpp/cppjieba/deps/limonp/include/limonp/StdExtension.hpp +0 -157
- package/cpp/cppjieba/deps/limonp/include/limonp/StringUtil.hpp +0 -386
- package/cpp/cppjieba/dict/hmm_model.utf8 +0 -34
- package/cpp/cppjieba/dict/idf.utf8 +0 -258826
- package/cpp/cppjieba/dict/jieba.dict.utf8 +0 -348982
- package/cpp/cppjieba/dict/stop_words.utf8 +0 -1534
- package/cpp/cppjieba/dict/user.dict.utf8 +0 -3
- package/cpp/cppjieba/include/cppjieba/DictTrie.hpp +0 -322
- package/cpp/cppjieba/include/cppjieba/FullSegment.hpp +0 -94
- package/cpp/cppjieba/include/cppjieba/HMMModel.hpp +0 -131
- package/cpp/cppjieba/include/cppjieba/HMMSegment.hpp +0 -211
- package/cpp/cppjieba/include/cppjieba/Jieba.hpp +0 -169
- package/cpp/cppjieba/include/cppjieba/KeywordExtractor.hpp +0 -152
- package/cpp/cppjieba/include/cppjieba/MPSegment.hpp +0 -137
- package/cpp/cppjieba/include/cppjieba/MixSegment.hpp +0 -109
- package/cpp/cppjieba/include/cppjieba/PosTagger.hpp +0 -77
- package/cpp/cppjieba/include/cppjieba/PreFilter.hpp +0 -54
- package/cpp/cppjieba/include/cppjieba/QuerySegment.hpp +0 -89
- package/cpp/cppjieba/include/cppjieba/SegmentBase.hpp +0 -46
- package/cpp/cppjieba/include/cppjieba/SegmentTagged.hpp +0 -23
- package/cpp/cppjieba/include/cppjieba/TextRankExtractor.hpp +0 -192
- package/cpp/cppjieba/include/cppjieba/Trie.hpp +0 -200
- package/cpp/cppjieba/include/cppjieba/Unicode.hpp +0 -231
- package/cpp/cppjieba/include/cppjieba/UnicodeFile.hpp +0 -68
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-jieba",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Native Chinese text segmentation for React Native, powered by cppjieba.",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"src",
|
|
17
17
|
"lib",
|
|
18
|
+
"README.md",
|
|
19
|
+
"CHANGELOG.md",
|
|
20
|
+
"LICENSE",
|
|
18
21
|
"android/build.gradle",
|
|
19
22
|
"android/CMakeLists.txt",
|
|
20
23
|
"android/generated",
|
|
@@ -23,6 +26,10 @@
|
|
|
23
26
|
"ios/generated",
|
|
24
27
|
"cpp/JiebaImpl.h",
|
|
25
28
|
"cpp/JiebaImpl.cpp",
|
|
29
|
+
"cpp/JiebaEngine.h",
|
|
30
|
+
"cpp/JiebaEngine.cpp",
|
|
31
|
+
"cpp/JiebaDictAndroid.h",
|
|
32
|
+
"cpp/JiebaDictAndroid.cpp",
|
|
26
33
|
"cpp/cppjieba/LICENSE",
|
|
27
34
|
"cpp/cppjieba/include/cppjieba",
|
|
28
35
|
"cpp/cppjieba/deps/limonp/LICENSE",
|
package/src/NativeJieba.ts
CHANGED
package/src/NativeJieba.web.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { Spec } from './NativeJieba';
|
|
|
3
3
|
// `jieba-wasm` exposes the same segmentation primitives as cppjieba, but a
|
|
4
4
|
// smaller surface: cut / cutAll / cutForSearch / tag / add_word. The remaining
|
|
5
5
|
// native methods are polyfilled on top of those where the semantics allow, and
|
|
6
|
-
// throw otherwise. The wasm module is loaded lazily by `
|
|
6
|
+
// throw otherwise. The wasm module is loaded lazily by `prepareJieba()` (see
|
|
7
7
|
// `init.web.ts`) and handed to this shim via `setJiebaWasm`.
|
|
8
8
|
|
|
9
9
|
export type JiebaWasmModule = {
|
|
@@ -23,17 +23,25 @@ export function setJiebaWasm(module: JiebaWasmModule): void {
|
|
|
23
23
|
wasm = module;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
export function isJiebaWasmReady(): boolean {
|
|
27
|
+
return wasm !== null;
|
|
28
|
+
}
|
|
29
|
+
|
|
26
30
|
function getWasm(): JiebaWasmModule {
|
|
27
31
|
if (!wasm) {
|
|
28
32
|
throw new Error(
|
|
29
|
-
'react-native-jieba: jieba-wasm is not
|
|
30
|
-
'
|
|
33
|
+
'react-native-jieba: jieba-wasm is not loaded yet. ' +
|
|
34
|
+
'On web you must call (and await) prepareJieba() before any ' +
|
|
35
|
+
'segmentation call.'
|
|
31
36
|
);
|
|
32
37
|
}
|
|
33
38
|
return wasm;
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
const Jieba: Spec = {
|
|
42
|
+
isReady() {
|
|
43
|
+
return isJiebaWasmReady();
|
|
44
|
+
},
|
|
37
45
|
setDictPath() {
|
|
38
46
|
// No-op on web: jieba-wasm bundles its own dictionary.
|
|
39
47
|
},
|
package/src/index.tsx
CHANGED
package/src/init.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Platform } from 'react-native';
|
|
|
2
2
|
import Jieba from './NativeJieba';
|
|
3
3
|
import JiebaAndroidHelper from './NativeJiebaAndroidHelper';
|
|
4
4
|
|
|
5
|
-
export type
|
|
5
|
+
export type PrepareJiebaOptions = {
|
|
6
6
|
/**
|
|
7
7
|
* Web only: URL (or fetch input) for the jieba-wasm `.wasm` binary. Most
|
|
8
8
|
* bundlers resolve it automatically, so this is only needed when the asset
|
|
@@ -11,11 +11,28 @@ export type InitJiebaOptions = {
|
|
|
11
11
|
wasmUrl?: string | URL | Request;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
let
|
|
14
|
+
let preparePromise: Promise<void> | null = null;
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Optional on native (iOS/Android): the dictionary is located/extracted
|
|
18
|
+
* automatically on the first segmentation call, so you can call `cut`, `tag`,
|
|
19
|
+
* etc. directly without ever calling `prepareJieba`.
|
|
20
|
+
*
|
|
21
|
+
* Calling it is still useful as a *warm-up*: on Android's very first run the
|
|
22
|
+
* dictionary assets are extracted from the APK, and doing that work here
|
|
23
|
+
* (asynchronously, up front) avoids a one-time blocking pause on the first
|
|
24
|
+
* `cut`/`tag` call.
|
|
25
|
+
*
|
|
26
|
+
* On web it is **required** — it loads and instantiates the `jieba-wasm`
|
|
27
|
+
* binary, which cannot be done synchronously.
|
|
28
|
+
*
|
|
29
|
+
* Idempotent: repeated calls return the same promise.
|
|
30
|
+
*/
|
|
31
|
+
export function prepareJieba(
|
|
32
|
+
_options: PrepareJiebaOptions = {}
|
|
33
|
+
): Promise<void> {
|
|
34
|
+
if (preparePromise) return preparePromise;
|
|
35
|
+
preparePromise = (async () => {
|
|
19
36
|
if (Platform.OS === 'android') {
|
|
20
37
|
if (!JiebaAndroidHelper) {
|
|
21
38
|
throw new Error(
|
|
@@ -27,8 +44,19 @@ export function initJieba(_options: InitJiebaOptions = {}): Promise<void> {
|
|
|
27
44
|
}
|
|
28
45
|
// iOS: OnLoad.mm already configured the dict path from the bundle.
|
|
29
46
|
})().catch((err) => {
|
|
30
|
-
|
|
47
|
+
preparePromise = null;
|
|
31
48
|
throw err;
|
|
32
49
|
});
|
|
33
|
-
return
|
|
50
|
+
return preparePromise;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether jieba is ready to segment synchronously.
|
|
55
|
+
*
|
|
56
|
+
* Backed by the native engine state (not by whether `prepareJieba` was called),
|
|
57
|
+
* so it stays correct even when the dictionary is resolved lazily on the first
|
|
58
|
+
* `cut`/`tag` call. On iOS this is `true` from app launch.
|
|
59
|
+
*/
|
|
60
|
+
export function isJiebaReady(): boolean {
|
|
61
|
+
return Jieba.isReady();
|
|
34
62
|
}
|
package/src/init.web.ts
CHANGED
|
@@ -1,13 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
setJiebaWasm,
|
|
3
|
+
isJiebaWasmReady,
|
|
4
|
+
type JiebaWasmModule,
|
|
5
|
+
} from './NativeJieba.web';
|
|
3
6
|
|
|
4
|
-
export type
|
|
7
|
+
export type PrepareJiebaOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* URL (or fetch input) for the jieba-wasm `.wasm` binary. Most bundlers
|
|
10
|
+
* resolve it automatically, so this is only needed when the asset is served
|
|
11
|
+
* from a custom location.
|
|
12
|
+
*/
|
|
13
|
+
wasmUrl?: string | URL | Request;
|
|
14
|
+
};
|
|
5
15
|
|
|
6
|
-
let
|
|
16
|
+
let preparePromise: Promise<void> | null = null;
|
|
7
17
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
18
|
+
/**
|
|
19
|
+
* **Required on web**: loads and instantiates the `jieba-wasm` binary (which
|
|
20
|
+
* cannot be done synchronously). Await it once before calling `cut`, `tag`, etc.
|
|
21
|
+
*
|
|
22
|
+
* Idempotent: repeated calls return the same promise.
|
|
23
|
+
*/
|
|
24
|
+
export function prepareJieba(options: PrepareJiebaOptions = {}): Promise<void> {
|
|
25
|
+
if (preparePromise) return preparePromise;
|
|
26
|
+
preparePromise = (async () => {
|
|
11
27
|
// `jieba-wasm` resolves to its `web` build under the `browser`/`import`
|
|
12
28
|
// conditions, whose default export is an async initializer that fetches
|
|
13
29
|
// and instantiates the wasm binary.
|
|
@@ -25,8 +41,13 @@ export function initJieba(options: InitJiebaOptions = {}): Promise<void> {
|
|
|
25
41
|
await wasm.default(options.wasmUrl);
|
|
26
42
|
setJiebaWasm(wasm);
|
|
27
43
|
})().catch((err) => {
|
|
28
|
-
|
|
44
|
+
preparePromise = null;
|
|
29
45
|
throw err;
|
|
30
46
|
});
|
|
31
|
-
return
|
|
47
|
+
return preparePromise;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Whether the wasm module has been loaded and jieba can segment. */
|
|
51
|
+
export function isJiebaReady(): boolean {
|
|
52
|
+
return isJiebaWasmReady();
|
|
32
53
|
}
|
package/src/jieba.ts
CHANGED
|
@@ -27,6 +27,17 @@ export function tag(sentence: string): Tagged[] {
|
|
|
27
27
|
return Jieba.tag(sentence);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* TF-IDF keyword extraction.
|
|
32
|
+
*
|
|
33
|
+
* Requires the IDF dictionary, which is loaded lazily on the first call (every other API works
|
|
34
|
+
* without it). Throws when that dictionary is unavailable:
|
|
35
|
+
* - on **web**, always — jieba-wasm ships no IDF data;
|
|
36
|
+
* - on **native**, only in builds that opted out of bundling it via `RN_JIEBA_EXCLUDE_IDF_DICT=1`
|
|
37
|
+
* (iOS) or `rnJiebaExcludeIdfDict=true` (Android), which drops ~5MB from the app.
|
|
38
|
+
*
|
|
39
|
+
* Guard it in cross-platform code, or in any app that may be built with the dictionary excluded.
|
|
40
|
+
*/
|
|
30
41
|
export function extract(sentence: string, topK: number = 5): Keyword[] {
|
|
31
42
|
return Jieba.extract(sentence, topK);
|
|
32
43
|
}
|
package/cpp/cppjieba/LICENSE
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2013
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
-
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
-
the Software without restriction, including without limitation the rights to
|
|
8
|
-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
-
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
-
subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2013
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
-
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
-
the Software without restriction, including without limitation the rights to
|
|
8
|
-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
-
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
-
subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/************************************
|
|
2
|
-
* file enc : ascii
|
|
3
|
-
* author : wuyanyi09@gmail.com
|
|
4
|
-
************************************/
|
|
5
|
-
|
|
6
|
-
#ifndef LIMONP_ARGV_FUNCTS_H
|
|
7
|
-
#define LIMONP_ARGV_FUNCTS_H
|
|
8
|
-
|
|
9
|
-
#include <set>
|
|
10
|
-
#include <sstream>
|
|
11
|
-
#include "StringUtil.hpp"
|
|
12
|
-
|
|
13
|
-
namespace limonp {
|
|
14
|
-
|
|
15
|
-
using namespace std;
|
|
16
|
-
|
|
17
|
-
class ArgvContext {
|
|
18
|
-
public :
|
|
19
|
-
ArgvContext(int argc, const char* const * argv) {
|
|
20
|
-
for(int i = 0; i < argc; i++) {
|
|
21
|
-
if(StartsWith(argv[i], "-")) {
|
|
22
|
-
if(i + 1 < argc && !StartsWith(argv[i + 1], "-")) {
|
|
23
|
-
mpss_[argv[i]] = argv[i+1];
|
|
24
|
-
i++;
|
|
25
|
-
} else {
|
|
26
|
-
sset_.insert(argv[i]);
|
|
27
|
-
}
|
|
28
|
-
} else {
|
|
29
|
-
args_.push_back(argv[i]);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
~ArgvContext() {
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
friend ostream& operator << (ostream& os, const ArgvContext& args);
|
|
37
|
-
string operator [](size_t i) const {
|
|
38
|
-
if(i < args_.size()) {
|
|
39
|
-
return args_[i];
|
|
40
|
-
}
|
|
41
|
-
return "";
|
|
42
|
-
}
|
|
43
|
-
string operator [](const string& key) const {
|
|
44
|
-
map<string, string>::const_iterator it = mpss_.find(key);
|
|
45
|
-
if(it != mpss_.end()) {
|
|
46
|
-
return it->second;
|
|
47
|
-
}
|
|
48
|
-
return "";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
bool HasKey(const string& key) const {
|
|
52
|
-
if(mpss_.find(key) != mpss_.end() || sset_.find(key) != sset_.end()) {
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private:
|
|
59
|
-
vector<string> args_;
|
|
60
|
-
map<string, string> mpss_;
|
|
61
|
-
set<string> sset_;
|
|
62
|
-
}; // class ArgvContext
|
|
63
|
-
|
|
64
|
-
inline ostream& operator << (ostream& os, const ArgvContext& args) {
|
|
65
|
-
return os<<args.args_<<args.mpss_<<args.sset_;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
} // namespace limonp
|
|
69
|
-
|
|
70
|
-
#endif
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
#ifndef LIMONP_CLOSURE_HPP
|
|
2
|
-
#define LIMONP_CLOSURE_HPP
|
|
3
|
-
|
|
4
|
-
namespace limonp {
|
|
5
|
-
|
|
6
|
-
class ClosureInterface {
|
|
7
|
-
public:
|
|
8
|
-
virtual ~ClosureInterface() {
|
|
9
|
-
}
|
|
10
|
-
virtual void Run() = 0;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
template <class Funct>
|
|
14
|
-
class Closure0: public ClosureInterface {
|
|
15
|
-
public:
|
|
16
|
-
Closure0(Funct fun) {
|
|
17
|
-
fun_ = fun;
|
|
18
|
-
}
|
|
19
|
-
virtual ~Closure0() {
|
|
20
|
-
}
|
|
21
|
-
virtual void Run() {
|
|
22
|
-
(*fun_)();
|
|
23
|
-
}
|
|
24
|
-
private:
|
|
25
|
-
Funct fun_;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
template <class Funct, class Arg1>
|
|
29
|
-
class Closure1: public ClosureInterface {
|
|
30
|
-
public:
|
|
31
|
-
Closure1(Funct fun, Arg1 arg1) {
|
|
32
|
-
fun_ = fun;
|
|
33
|
-
arg1_ = arg1;
|
|
34
|
-
}
|
|
35
|
-
virtual ~Closure1() {
|
|
36
|
-
}
|
|
37
|
-
virtual void Run() {
|
|
38
|
-
(*fun_)(arg1_);
|
|
39
|
-
}
|
|
40
|
-
private:
|
|
41
|
-
Funct fun_;
|
|
42
|
-
Arg1 arg1_;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
template <class Funct, class Arg1, class Arg2>
|
|
46
|
-
class Closure2: public ClosureInterface {
|
|
47
|
-
public:
|
|
48
|
-
Closure2(Funct fun, Arg1 arg1, Arg2 arg2) {
|
|
49
|
-
fun_ = fun;
|
|
50
|
-
arg1_ = arg1;
|
|
51
|
-
arg2_ = arg2;
|
|
52
|
-
}
|
|
53
|
-
virtual ~Closure2() {
|
|
54
|
-
}
|
|
55
|
-
virtual void Run() {
|
|
56
|
-
(*fun_)(arg1_, arg2_);
|
|
57
|
-
}
|
|
58
|
-
private:
|
|
59
|
-
Funct fun_;
|
|
60
|
-
Arg1 arg1_;
|
|
61
|
-
Arg2 arg2_;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
template <class Funct, class Arg1, class Arg2, class Arg3>
|
|
65
|
-
class Closure3: public ClosureInterface {
|
|
66
|
-
public:
|
|
67
|
-
Closure3(Funct fun, Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
|
68
|
-
fun_ = fun;
|
|
69
|
-
arg1_ = arg1;
|
|
70
|
-
arg2_ = arg2;
|
|
71
|
-
arg3_ = arg3;
|
|
72
|
-
}
|
|
73
|
-
virtual ~Closure3() {
|
|
74
|
-
}
|
|
75
|
-
virtual void Run() {
|
|
76
|
-
(*fun_)(arg1_, arg2_, arg3_);
|
|
77
|
-
}
|
|
78
|
-
private:
|
|
79
|
-
Funct fun_;
|
|
80
|
-
Arg1 arg1_;
|
|
81
|
-
Arg2 arg2_;
|
|
82
|
-
Arg3 arg3_;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
template <class Obj, class Funct>
|
|
86
|
-
class ObjClosure0: public ClosureInterface {
|
|
87
|
-
public:
|
|
88
|
-
ObjClosure0(Obj* p, Funct fun) {
|
|
89
|
-
p_ = p;
|
|
90
|
-
fun_ = fun;
|
|
91
|
-
}
|
|
92
|
-
virtual ~ObjClosure0() {
|
|
93
|
-
}
|
|
94
|
-
virtual void Run() {
|
|
95
|
-
(p_->*fun_)();
|
|
96
|
-
}
|
|
97
|
-
private:
|
|
98
|
-
Obj* p_;
|
|
99
|
-
Funct fun_;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
template <class Obj, class Funct, class Arg1>
|
|
103
|
-
class ObjClosure1: public ClosureInterface {
|
|
104
|
-
public:
|
|
105
|
-
ObjClosure1(Obj* p, Funct fun, Arg1 arg1) {
|
|
106
|
-
p_ = p;
|
|
107
|
-
fun_ = fun;
|
|
108
|
-
arg1_ = arg1;
|
|
109
|
-
}
|
|
110
|
-
virtual ~ObjClosure1() {
|
|
111
|
-
}
|
|
112
|
-
virtual void Run() {
|
|
113
|
-
(p_->*fun_)(arg1_);
|
|
114
|
-
}
|
|
115
|
-
private:
|
|
116
|
-
Obj* p_;
|
|
117
|
-
Funct fun_;
|
|
118
|
-
Arg1 arg1_;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
template <class Obj, class Funct, class Arg1, class Arg2>
|
|
122
|
-
class ObjClosure2: public ClosureInterface {
|
|
123
|
-
public:
|
|
124
|
-
ObjClosure2(Obj* p, Funct fun, Arg1 arg1, Arg2 arg2) {
|
|
125
|
-
p_ = p;
|
|
126
|
-
fun_ = fun;
|
|
127
|
-
arg1_ = arg1;
|
|
128
|
-
arg2_ = arg2;
|
|
129
|
-
}
|
|
130
|
-
virtual ~ObjClosure2() {
|
|
131
|
-
}
|
|
132
|
-
virtual void Run() {
|
|
133
|
-
(p_->*fun_)(arg1_, arg2_);
|
|
134
|
-
}
|
|
135
|
-
private:
|
|
136
|
-
Obj* p_;
|
|
137
|
-
Funct fun_;
|
|
138
|
-
Arg1 arg1_;
|
|
139
|
-
Arg2 arg2_;
|
|
140
|
-
};
|
|
141
|
-
template <class Obj, class Funct, class Arg1, class Arg2, class Arg3>
|
|
142
|
-
class ObjClosure3: public ClosureInterface {
|
|
143
|
-
public:
|
|
144
|
-
ObjClosure3(Obj* p, Funct fun, Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
|
145
|
-
p_ = p;
|
|
146
|
-
fun_ = fun;
|
|
147
|
-
arg1_ = arg1;
|
|
148
|
-
arg2_ = arg2;
|
|
149
|
-
arg3_ = arg3;
|
|
150
|
-
}
|
|
151
|
-
virtual ~ObjClosure3() {
|
|
152
|
-
}
|
|
153
|
-
virtual void Run() {
|
|
154
|
-
(p_->*fun_)(arg1_, arg2_, arg3_);
|
|
155
|
-
}
|
|
156
|
-
private:
|
|
157
|
-
Obj* p_;
|
|
158
|
-
Funct fun_;
|
|
159
|
-
Arg1 arg1_;
|
|
160
|
-
Arg2 arg2_;
|
|
161
|
-
Arg3 arg3_;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
template<class R>
|
|
165
|
-
ClosureInterface* NewClosure(R (*fun)()) {
|
|
166
|
-
return new Closure0<R (*)()>(fun);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
template<class R, class Arg1>
|
|
170
|
-
ClosureInterface* NewClosure(R (*fun)(Arg1), Arg1 arg1) {
|
|
171
|
-
return new Closure1<R (*)(Arg1), Arg1>(fun, arg1);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
template<class R, class Arg1, class Arg2>
|
|
175
|
-
ClosureInterface* NewClosure(R (*fun)(Arg1, Arg2), Arg1 arg1, Arg2 arg2) {
|
|
176
|
-
return new Closure2<R (*)(Arg1, Arg2), Arg1, Arg2>(fun, arg1, arg2);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
template<class R, class Arg1, class Arg2, class Arg3>
|
|
180
|
-
ClosureInterface* NewClosure(R (*fun)(Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
|
181
|
-
return new Closure3<R (*)(Arg1, Arg2, Arg3), Arg1, Arg2, Arg3>(fun, arg1, arg2, arg3);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
template<class R, class Obj>
|
|
185
|
-
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)()) {
|
|
186
|
-
return new ObjClosure0<Obj, R (Obj::* )()>(obj, fun);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
template<class R, class Obj, class Arg1>
|
|
190
|
-
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1), Arg1 arg1) {
|
|
191
|
-
return new ObjClosure1<Obj, R (Obj::* )(Arg1), Arg1>(obj, fun, arg1);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
template<class R, class Obj, class Arg1, class Arg2>
|
|
195
|
-
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1, Arg2), Arg1 arg1, Arg2 arg2) {
|
|
196
|
-
return new ObjClosure2<Obj, R (Obj::*)(Arg1, Arg2), Arg1, Arg2>(obj, fun, arg1, arg2);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
template<class R, class Obj, class Arg1, class Arg2, class Arg3>
|
|
200
|
-
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
|
201
|
-
return new ObjClosure3<Obj, R (Obj::*)(Arg1, Arg2, Arg3), Arg1, Arg2, Arg3>(obj, fun, arg1, arg2, arg3);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
} // namespace limonp
|
|
205
|
-
|
|
206
|
-
#endif // LIMONP_CLOSURE_HPP
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
#ifndef LIMONP_COLOR_PRINT_HPP
|
|
2
|
-
#define LIMONP_COLOR_PRINT_HPP
|
|
3
|
-
|
|
4
|
-
#include <string>
|
|
5
|
-
#include <stdarg.h>
|
|
6
|
-
|
|
7
|
-
namespace limonp {
|
|
8
|
-
|
|
9
|
-
using std::string;
|
|
10
|
-
|
|
11
|
-
enum Color {
|
|
12
|
-
BLACK = 30,
|
|
13
|
-
RED,
|
|
14
|
-
GREEN,
|
|
15
|
-
YELLOW,
|
|
16
|
-
BLUE,
|
|
17
|
-
PURPLE
|
|
18
|
-
}; // enum Color
|
|
19
|
-
|
|
20
|
-
static void ColorPrintln(enum Color color, const char * fmt, ...) {
|
|
21
|
-
va_list ap;
|
|
22
|
-
printf("\033[0;%dm", color);
|
|
23
|
-
va_start(ap, fmt);
|
|
24
|
-
vprintf(fmt, ap);
|
|
25
|
-
va_end(ap);
|
|
26
|
-
printf("\033[0m\n"); // if not \n , in some situation , the next lines will be set the same color unexpectedly
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
} // namespace limonp
|
|
30
|
-
|
|
31
|
-
#endif // LIMONP_COLOR_PRINT_HPP
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
#ifndef LIMONP_CONDITION_HPP
|
|
2
|
-
#define LIMONP_CONDITION_HPP
|
|
3
|
-
|
|
4
|
-
#include "MutexLock.hpp"
|
|
5
|
-
|
|
6
|
-
namespace limonp {
|
|
7
|
-
|
|
8
|
-
class Condition : NonCopyable {
|
|
9
|
-
public:
|
|
10
|
-
explicit Condition(MutexLock& mutex)
|
|
11
|
-
: mutex_(mutex) {
|
|
12
|
-
XCHECK(!pthread_cond_init(&pcond_, NULL));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
~Condition() {
|
|
16
|
-
XCHECK(!pthread_cond_destroy(&pcond_));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
void Wait() {
|
|
20
|
-
XCHECK(!pthread_cond_wait(&pcond_, mutex_.GetPthreadMutex()));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
void Notify() {
|
|
24
|
-
XCHECK(!pthread_cond_signal(&pcond_));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
void NotifyAll() {
|
|
28
|
-
XCHECK(!pthread_cond_broadcast(&pcond_));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
private:
|
|
32
|
-
MutexLock& mutex_;
|
|
33
|
-
pthread_cond_t pcond_;
|
|
34
|
-
}; // class Condition
|
|
35
|
-
|
|
36
|
-
} // namespace limonp
|
|
37
|
-
|
|
38
|
-
#endif // LIMONP_CONDITION_HPP
|