cppjieba_rb 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.gitmodules +3 -0
- data/.travis.yml +26 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +81 -0
- data/Rakefile +20 -0
- data/cppjieba_rb.gemspec +50 -0
- data/ext/cppjieba/.gitignore +17 -0
- data/ext/cppjieba/.travis.yml +22 -0
- data/ext/cppjieba/CMakeLists.txt +28 -0
- data/ext/cppjieba/ChangeLog.md +236 -0
- data/ext/cppjieba/README.md +285 -0
- data/ext/cppjieba/README_EN.md +111 -0
- data/ext/cppjieba/appveyor.yml +32 -0
- data/ext/cppjieba/deps/CMakeLists.txt +1 -0
- data/ext/cppjieba/deps/gtest/CMakeLists.txt +5 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-death-test.h +283 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-message.h +230 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h +1421 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h.pump +487 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-printers.h +796 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-spi.h +232 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-test-part.h +176 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-typed-test.h +259 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest.h +2155 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest_pred_impl.h +358 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest_prod.h +58 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-death-test-internal.h +308 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-filepath.h +210 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-internal.h +1226 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-linked_ptr.h +233 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h +4822 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h.pump +301 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util.h +619 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-port.h +1788 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-string.h +350 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h +968 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h.pump +336 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h +3330 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h.pump +296 -0
- data/ext/cppjieba/deps/gtest/src/.deps/.dirstamp +0 -0
- data/ext/cppjieba/deps/gtest/src/.deps/gtest-all.Plo +681 -0
- data/ext/cppjieba/deps/gtest/src/.deps/gtest_main.Plo +509 -0
- data/ext/cppjieba/deps/gtest/src/.dirstamp +0 -0
- data/ext/cppjieba/deps/gtest/src/gtest-all.cc +48 -0
- data/ext/cppjieba/deps/gtest/src/gtest-death-test.cc +1234 -0
- data/ext/cppjieba/deps/gtest/src/gtest-filepath.cc +380 -0
- data/ext/cppjieba/deps/gtest/src/gtest-internal-inl.h +1038 -0
- data/ext/cppjieba/deps/gtest/src/gtest-port.cc +746 -0
- data/ext/cppjieba/deps/gtest/src/gtest-printers.cc +356 -0
- data/ext/cppjieba/deps/gtest/src/gtest-test-part.cc +110 -0
- data/ext/cppjieba/deps/gtest/src/gtest-typed-test.cc +110 -0
- data/ext/cppjieba/deps/gtest/src/gtest.cc +4898 -0
- data/ext/cppjieba/deps/gtest/src/gtest_main.cc +39 -0
- data/ext/cppjieba/deps/limonp/ArgvContext.hpp +70 -0
- data/ext/cppjieba/deps/limonp/BlockingQueue.hpp +49 -0
- data/ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp +67 -0
- data/ext/cppjieba/deps/limonp/BoundedQueue.hpp +65 -0
- data/ext/cppjieba/deps/limonp/Closure.hpp +206 -0
- data/ext/cppjieba/deps/limonp/Colors.hpp +31 -0
- data/ext/cppjieba/deps/limonp/Condition.hpp +38 -0
- data/ext/cppjieba/deps/limonp/Config.hpp +103 -0
- data/ext/cppjieba/deps/limonp/FileLock.hpp +74 -0
- data/ext/cppjieba/deps/limonp/ForcePublic.hpp +7 -0
- data/ext/cppjieba/deps/limonp/LocalVector.hpp +139 -0
- data/ext/cppjieba/deps/limonp/Logging.hpp +76 -0
- data/ext/cppjieba/deps/limonp/Md5.hpp +411 -0
- data/ext/cppjieba/deps/limonp/MutexLock.hpp +51 -0
- data/ext/cppjieba/deps/limonp/NonCopyable.hpp +21 -0
- data/ext/cppjieba/deps/limonp/StdExtension.hpp +159 -0
- data/ext/cppjieba/deps/limonp/StringUtil.hpp +365 -0
- data/ext/cppjieba/deps/limonp/Thread.hpp +44 -0
- data/ext/cppjieba/deps/limonp/ThreadPool.hpp +86 -0
- data/ext/cppjieba/dict/README.md +31 -0
- data/ext/cppjieba/dict/hmm_model.utf8 +34 -0
- data/ext/cppjieba/dict/idf.utf8 +258826 -0
- data/ext/cppjieba/dict/jieba.dict.utf8 +348982 -0
- data/ext/cppjieba/dict/pos_dict/char_state_tab.utf8 +6653 -0
- data/ext/cppjieba/dict/pos_dict/prob_emit.utf8 +166 -0
- data/ext/cppjieba/dict/pos_dict/prob_start.utf8 +259 -0
- data/ext/cppjieba/dict/pos_dict/prob_trans.utf8 +5222 -0
- data/ext/cppjieba/dict/stop_words.utf8 +1534 -0
- data/ext/cppjieba/dict/user.dict.utf8 +4 -0
- data/ext/cppjieba/include/cppjieba/DictTrie.hpp +227 -0
- data/ext/cppjieba/include/cppjieba/FullSegment.hpp +93 -0
- data/ext/cppjieba/include/cppjieba/HMMModel.hpp +129 -0
- data/ext/cppjieba/include/cppjieba/HMMSegment.hpp +190 -0
- data/ext/cppjieba/include/cppjieba/Jieba.hpp +108 -0
- data/ext/cppjieba/include/cppjieba/KeywordExtractor.hpp +153 -0
- data/ext/cppjieba/include/cppjieba/MPSegment.hpp +137 -0
- data/ext/cppjieba/include/cppjieba/MixSegment.hpp +109 -0
- data/ext/cppjieba/include/cppjieba/PosTagger.hpp +77 -0
- data/ext/cppjieba/include/cppjieba/PreFilter.hpp +54 -0
- data/ext/cppjieba/include/cppjieba/QuerySegment.hpp +90 -0
- data/ext/cppjieba/include/cppjieba/SegmentBase.hpp +46 -0
- data/ext/cppjieba/include/cppjieba/SegmentTagged.hpp +23 -0
- data/ext/cppjieba/include/cppjieba/TextRankExtractor.hpp +190 -0
- data/ext/cppjieba/include/cppjieba/Trie.hpp +174 -0
- data/ext/cppjieba/include/cppjieba/Unicode.hpp +215 -0
- data/ext/cppjieba/test/CMakeLists.txt +5 -0
- data/ext/cppjieba/test/demo.cpp +80 -0
- data/ext/cppjieba/test/load_test.cpp +54 -0
- data/ext/cppjieba/test/testdata/curl.res +1 -0
- data/ext/cppjieba/test/testdata/extra_dict/jieba.dict.small.utf8 +109750 -0
- data/ext/cppjieba/test/testdata/gbk_dict/hmm_model.gbk +34 -0
- data/ext/cppjieba/test/testdata/gbk_dict/jieba.dict.gbk +348982 -0
- data/ext/cppjieba/test/testdata/jieba.dict.0.1.utf8 +93 -0
- data/ext/cppjieba/test/testdata/jieba.dict.0.utf8 +93 -0
- data/ext/cppjieba/test/testdata/jieba.dict.1.utf8 +67 -0
- data/ext/cppjieba/test/testdata/jieba.dict.2.utf8 +64 -0
- data/ext/cppjieba/test/testdata/load_test.urls +2 -0
- data/ext/cppjieba/test/testdata/review.100 +100 -0
- data/ext/cppjieba/test/testdata/review.100.res +200 -0
- data/ext/cppjieba/test/testdata/server.conf +19 -0
- data/ext/cppjieba/test/testdata/testlines.gbk +9 -0
- data/ext/cppjieba/test/testdata/testlines.utf8 +8 -0
- data/ext/cppjieba/test/testdata/userdict.2.utf8 +1 -0
- data/ext/cppjieba/test/testdata/userdict.english +2 -0
- data/ext/cppjieba/test/testdata/userdict.utf8 +8 -0
- data/ext/cppjieba/test/testdata/weicheng.utf8 +247 -0
- data/ext/cppjieba/test/unittest/CMakeLists.txt +24 -0
- data/ext/cppjieba/test/unittest/gtest_main.cpp +39 -0
- data/ext/cppjieba/test/unittest/jieba_test.cpp +133 -0
- data/ext/cppjieba/test/unittest/keyword_extractor_test.cpp +79 -0
- data/ext/cppjieba/test/unittest/pos_tagger_test.cpp +41 -0
- data/ext/cppjieba/test/unittest/pre_filter_test.cpp +43 -0
- data/ext/cppjieba/test/unittest/segments_test.cpp +256 -0
- data/ext/cppjieba/test/unittest/textrank_test.cpp +86 -0
- data/ext/cppjieba/test/unittest/trie_test.cpp +177 -0
- data/ext/cppjieba/test/unittest/unicode_test.cpp +43 -0
- data/ext/cppjieba_rb/cppjieba_rb.c +10 -0
- data/ext/cppjieba_rb/extconf.rb +26 -0
- data/ext/cppjieba_rb/internal.cc +148 -0
- data/lib/cppjieba_rb/segment.rb +20 -0
- data/lib/cppjieba_rb/version.rb +3 -0
- data/lib/cppjieba_rb.rb +34 -0
- data/test/test_keyword.rb +17 -0
- data/test/test_segment.rb +24 -0
- data/test/test_tagging.rb +19 -0
- metadata +244 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
// Copyright 2006, Google Inc.
|
2
|
+
// All rights reserved.
|
3
|
+
//
|
4
|
+
// Redistribution and use in source and binary forms, with or without
|
5
|
+
// modification, are permitted provided that the following conditions are
|
6
|
+
// met:
|
7
|
+
//
|
8
|
+
// * Redistributions of source code must retain the above copyright
|
9
|
+
// notice, this list of conditions and the following disclaimer.
|
10
|
+
// * Redistributions in binary form must reproduce the above
|
11
|
+
// copyright notice, this list of conditions and the following disclaimer
|
12
|
+
// in the documentation and/or other materials provided with the
|
13
|
+
// distribution.
|
14
|
+
// * Neither the name of Google Inc. nor the names of its
|
15
|
+
// contributors may be used to endorse or promote products derived from
|
16
|
+
// this software without specific prior written permission.
|
17
|
+
//
|
18
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
|
30
|
+
#include <iostream>
|
31
|
+
|
32
|
+
#include "gtest/gtest.h"
|
33
|
+
|
34
|
+
GTEST_API_ int main(int argc, char **argv) {
|
35
|
+
std::cout << "Running main() from gtest_main.cc\n";
|
36
|
+
|
37
|
+
testing::InitGoogleTest(&argc, argv);
|
38
|
+
return RUN_ALL_TESTS();
|
39
|
+
}
|
@@ -0,0 +1,70 @@
|
|
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
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#ifndef LIMONP_BLOCKINGQUEUE_HPP
|
2
|
+
#define LIMONP_BLOCKINGQUEUE_HPP
|
3
|
+
|
4
|
+
#include <queue>
|
5
|
+
#include "Condition.hpp"
|
6
|
+
|
7
|
+
namespace limonp {
|
8
|
+
template<class T>
|
9
|
+
class BlockingQueue: NonCopyable {
|
10
|
+
public:
|
11
|
+
BlockingQueue()
|
12
|
+
: mutex_(), notEmpty_(mutex_), queue_() {
|
13
|
+
}
|
14
|
+
|
15
|
+
void Push(const T& x) {
|
16
|
+
MutexLockGuard lock(mutex_);
|
17
|
+
queue_.push(x);
|
18
|
+
notEmpty_.Notify(); // Wait morphing saves us
|
19
|
+
}
|
20
|
+
|
21
|
+
T Pop() {
|
22
|
+
MutexLockGuard lock(mutex_);
|
23
|
+
// always use a while-loop, due to spurious wakeup
|
24
|
+
while (queue_.empty()) {
|
25
|
+
notEmpty_.Wait();
|
26
|
+
}
|
27
|
+
assert(!queue_.empty());
|
28
|
+
T front(queue_.front());
|
29
|
+
queue_.pop();
|
30
|
+
return front;
|
31
|
+
}
|
32
|
+
|
33
|
+
size_t Size() const {
|
34
|
+
MutexLockGuard lock(mutex_);
|
35
|
+
return queue_.size();
|
36
|
+
}
|
37
|
+
bool Empty() const {
|
38
|
+
return Size() == 0;
|
39
|
+
}
|
40
|
+
|
41
|
+
private:
|
42
|
+
mutable MutexLock mutex_;
|
43
|
+
Condition notEmpty_;
|
44
|
+
std::queue<T> queue_;
|
45
|
+
}; // class BlockingQueue
|
46
|
+
|
47
|
+
} // namespace limonp
|
48
|
+
|
49
|
+
#endif // LIMONP_BLOCKINGQUEUE_HPP
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#ifndef LIMONP_BOUNDED_BLOCKING_QUEUE_HPP
|
2
|
+
#define LIMONP_BOUNDED_BLOCKING_QUEUE_HPP
|
3
|
+
|
4
|
+
#include "BoundedQueue.hpp"
|
5
|
+
|
6
|
+
namespace limonp {
|
7
|
+
|
8
|
+
template<typename T>
|
9
|
+
class BoundedBlockingQueue : NonCopyable {
|
10
|
+
public:
|
11
|
+
explicit BoundedBlockingQueue(size_t maxSize)
|
12
|
+
: mutex_(),
|
13
|
+
notEmpty_(mutex_),
|
14
|
+
notFull_(mutex_),
|
15
|
+
queue_(maxSize) {
|
16
|
+
}
|
17
|
+
|
18
|
+
void Push(const T& x) {
|
19
|
+
MutexLockGuard lock(mutex_);
|
20
|
+
while (queue_.Full()) {
|
21
|
+
notFull_.Wait();
|
22
|
+
}
|
23
|
+
assert(!queue_.Full());
|
24
|
+
queue_.Push(x);
|
25
|
+
notEmpty_.Notify();
|
26
|
+
}
|
27
|
+
|
28
|
+
T Pop() {
|
29
|
+
MutexLockGuard lock(mutex_);
|
30
|
+
while (queue_.Empty()) {
|
31
|
+
notEmpty_.Wait();
|
32
|
+
}
|
33
|
+
assert(!queue_.Empty());
|
34
|
+
T res = queue_.Pop();
|
35
|
+
notFull_.Notify();
|
36
|
+
return res;
|
37
|
+
}
|
38
|
+
|
39
|
+
bool Empty() const {
|
40
|
+
MutexLockGuard lock(mutex_);
|
41
|
+
return queue_.Empty();
|
42
|
+
}
|
43
|
+
|
44
|
+
bool Full() const {
|
45
|
+
MutexLockGuard lock(mutex_);
|
46
|
+
return queue_.Full();
|
47
|
+
}
|
48
|
+
|
49
|
+
size_t size() const {
|
50
|
+
MutexLockGuard lock(mutex_);
|
51
|
+
return queue_.size();
|
52
|
+
}
|
53
|
+
|
54
|
+
size_t capacity() const {
|
55
|
+
return queue_.capacity();
|
56
|
+
}
|
57
|
+
|
58
|
+
private:
|
59
|
+
mutable MutexLock mutex_;
|
60
|
+
Condition notEmpty_;
|
61
|
+
Condition notFull_;
|
62
|
+
BoundedQueue<T> queue_;
|
63
|
+
}; // class BoundedBlockingQueue
|
64
|
+
|
65
|
+
} // namespace limonp
|
66
|
+
|
67
|
+
#endif // LIMONP_BOUNDED_BLOCKING_QUEUE_HPP
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#ifndef LIMONP_BOUNDED_QUEUE_HPP
|
2
|
+
#define LIMONP_BOUNDED_QUEUE_HPP
|
3
|
+
|
4
|
+
#include <vector>
|
5
|
+
#include <fstream>
|
6
|
+
#include <cassert>
|
7
|
+
|
8
|
+
namespace limonp {
|
9
|
+
using namespace std;
|
10
|
+
template<class T>
|
11
|
+
class BoundedQueue {
|
12
|
+
public:
|
13
|
+
explicit BoundedQueue(size_t capacity): capacity_(capacity), circular_buffer_(capacity) {
|
14
|
+
head_ = 0;
|
15
|
+
tail_ = 0;
|
16
|
+
size_ = 0;
|
17
|
+
assert(capacity_);
|
18
|
+
}
|
19
|
+
~BoundedQueue() {
|
20
|
+
}
|
21
|
+
|
22
|
+
void Clear() {
|
23
|
+
head_ = 0;
|
24
|
+
tail_ = 0;
|
25
|
+
size_ = 0;
|
26
|
+
}
|
27
|
+
bool Empty() const {
|
28
|
+
return !size_;
|
29
|
+
}
|
30
|
+
bool Full() const {
|
31
|
+
return capacity_ == size_;
|
32
|
+
}
|
33
|
+
size_t Size() const {
|
34
|
+
return size_;
|
35
|
+
}
|
36
|
+
size_t Capacity() const {
|
37
|
+
return capacity_;
|
38
|
+
}
|
39
|
+
|
40
|
+
void Push(const T& t) {
|
41
|
+
assert(!Full());
|
42
|
+
circular_buffer_[tail_] = t;
|
43
|
+
tail_ = (tail_ + 1) % capacity_;
|
44
|
+
size_ ++;
|
45
|
+
}
|
46
|
+
|
47
|
+
T Pop() {
|
48
|
+
assert(!Empty());
|
49
|
+
size_t oldPos = head_;
|
50
|
+
head_ = (head_ + 1) % capacity_;
|
51
|
+
size_ --;
|
52
|
+
return circular_buffer_[oldPos];
|
53
|
+
}
|
54
|
+
|
55
|
+
private:
|
56
|
+
size_t head_;
|
57
|
+
size_t tail_;
|
58
|
+
size_t size_;
|
59
|
+
const size_t capacity_;
|
60
|
+
vector<T> circular_buffer_;
|
61
|
+
|
62
|
+
}; // class BoundedQueue
|
63
|
+
} // namespace limonp
|
64
|
+
|
65
|
+
#endif
|
@@ -0,0 +1,206 @@
|
|
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
|
@@ -0,0 +1,31 @@
|
|
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
|
@@ -0,0 +1,38 @@
|
|
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
|
@@ -0,0 +1,103 @@
|
|
1
|
+
/************************************
|
2
|
+
* file enc : utf8
|
3
|
+
* author : wuyanyi09@gmail.com
|
4
|
+
************************************/
|
5
|
+
#ifndef LIMONP_CONFIG_H
|
6
|
+
#define LIMONP_CONFIG_H
|
7
|
+
|
8
|
+
#include <map>
|
9
|
+
#include <fstream>
|
10
|
+
#include <iostream>
|
11
|
+
#include <assert.h>
|
12
|
+
#include "StringUtil.hpp"
|
13
|
+
|
14
|
+
namespace limonp {
|
15
|
+
|
16
|
+
using namespace std;
|
17
|
+
|
18
|
+
class Config {
|
19
|
+
public:
|
20
|
+
explicit Config(const string& filePath) {
|
21
|
+
LoadFile(filePath);
|
22
|
+
}
|
23
|
+
|
24
|
+
operator bool () {
|
25
|
+
return !map_.empty();
|
26
|
+
}
|
27
|
+
|
28
|
+
string Get(const string& key, const string& defaultvalue) const {
|
29
|
+
map<string, string>::const_iterator it = map_.find(key);
|
30
|
+
if(map_.end() != it) {
|
31
|
+
return it->second;
|
32
|
+
}
|
33
|
+
return defaultvalue;
|
34
|
+
}
|
35
|
+
int Get(const string& key, int defaultvalue) const {
|
36
|
+
string str = Get(key, "");
|
37
|
+
if("" == str) {
|
38
|
+
return defaultvalue;
|
39
|
+
}
|
40
|
+
return atoi(str.c_str());
|
41
|
+
}
|
42
|
+
const char* operator [] (const char* key) const {
|
43
|
+
if(NULL == key) {
|
44
|
+
return NULL;
|
45
|
+
}
|
46
|
+
map<string, string>::const_iterator it = map_.find(key);
|
47
|
+
if(map_.end() != it) {
|
48
|
+
return it->second.c_str();
|
49
|
+
}
|
50
|
+
return NULL;
|
51
|
+
}
|
52
|
+
|
53
|
+
string GetConfigInfo() const {
|
54
|
+
string res;
|
55
|
+
res << *this;
|
56
|
+
return res;
|
57
|
+
}
|
58
|
+
|
59
|
+
private:
|
60
|
+
void LoadFile(const string& filePath) {
|
61
|
+
ifstream ifs(filePath.c_str());
|
62
|
+
assert(ifs);
|
63
|
+
string line;
|
64
|
+
vector<string> vecBuf;
|
65
|
+
size_t lineno = 0;
|
66
|
+
while(getline(ifs, line)) {
|
67
|
+
lineno ++;
|
68
|
+
Trim(line);
|
69
|
+
if(line.empty() || StartsWith(line, "#")) {
|
70
|
+
continue;
|
71
|
+
}
|
72
|
+
vecBuf.clear();
|
73
|
+
Split(line, vecBuf, "=");
|
74
|
+
if(2 != vecBuf.size()) {
|
75
|
+
fprintf(stderr, "line[%s] illegal.\n", line.c_str());
|
76
|
+
assert(false);
|
77
|
+
continue;
|
78
|
+
}
|
79
|
+
string& key = vecBuf[0];
|
80
|
+
string& value = vecBuf[1];
|
81
|
+
Trim(key);
|
82
|
+
Trim(value);
|
83
|
+
if(!map_.insert(make_pair(key, value)).second) {
|
84
|
+
fprintf(stderr, "key[%s] already exits.\n", key.c_str());
|
85
|
+
assert(false);
|
86
|
+
continue;
|
87
|
+
}
|
88
|
+
}
|
89
|
+
ifs.close();
|
90
|
+
}
|
91
|
+
|
92
|
+
friend ostream& operator << (ostream& os, const Config& config);
|
93
|
+
|
94
|
+
map<string, string> map_;
|
95
|
+
}; // class Config
|
96
|
+
|
97
|
+
inline ostream& operator << (ostream& os, const Config& config) {
|
98
|
+
return os << config.map_;
|
99
|
+
}
|
100
|
+
|
101
|
+
} // namespace limonp
|
102
|
+
|
103
|
+
#endif // LIMONP_CONFIG_H
|