jieba_rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +51 -0
  8. data/Rakefile +11 -0
  9. data/ext/cppjieba/.gitignore +17 -0
  10. data/ext/cppjieba/.travis.yml +22 -0
  11. data/ext/cppjieba/CMakeLists.txt +27 -0
  12. data/ext/cppjieba/ChangeLog.md +81 -0
  13. data/ext/cppjieba/Dockerfile +11 -0
  14. data/ext/cppjieba/LICENSE +20 -0
  15. data/ext/cppjieba/README.md +359 -0
  16. data/ext/cppjieba/conf/CMakeLists.txt +1 -0
  17. data/ext/cppjieba/conf/server.conf +16 -0
  18. data/ext/cppjieba/dict/CMakeLists.txt +1 -0
  19. data/ext/cppjieba/dict/README.md +31 -0
  20. data/ext/cppjieba/dict/extra_dict/jieba.dict.small.utf8 +109750 -0
  21. data/ext/cppjieba/dict/gbk_dict/hmm_model.gbk +34 -0
  22. data/ext/cppjieba/dict/gbk_dict/jieba.dict.gbk +348982 -0
  23. data/ext/cppjieba/dict/hmm_model.utf8 +34 -0
  24. data/ext/cppjieba/dict/idf.utf8 +258826 -0
  25. data/ext/cppjieba/dict/jieba.dict.utf8 +348982 -0
  26. data/ext/cppjieba/dict/pos_dict/char_state_tab.utf8 +6653 -0
  27. data/ext/cppjieba/dict/pos_dict/prob_emit.utf8 +166 -0
  28. data/ext/cppjieba/dict/pos_dict/prob_start.utf8 +259 -0
  29. data/ext/cppjieba/dict/pos_dict/prob_trans.utf8 +5222 -0
  30. data/ext/cppjieba/dict/stop_words.utf8 +1534 -0
  31. data/ext/cppjieba/dict/user.dict.utf8 +3 -0
  32. data/ext/cppjieba/script/CMakeLists.txt +1 -0
  33. data/ext/cppjieba/script/cjserver.start +12 -0
  34. data/ext/cppjieba/script/cjserver.stop +13 -0
  35. data/ext/cppjieba/server/CMakeLists.txt +9 -0
  36. data/ext/cppjieba/server/Husky/HttpReqInfo.hpp +294 -0
  37. data/ext/cppjieba/server/Husky/IRequestHandler.hpp +18 -0
  38. data/ext/cppjieba/server/Husky/ThreadPoolServer.hpp +108 -0
  39. data/ext/cppjieba/server/Husky/WorkerThread.hpp +133 -0
  40. data/ext/cppjieba/server/server.cpp +91 -0
  41. data/ext/cppjieba/src/DictTrie.hpp +211 -0
  42. data/ext/cppjieba/src/FullSegment.hpp +153 -0
  43. data/ext/cppjieba/src/HMMSegment.hpp +394 -0
  44. data/ext/cppjieba/src/ISegment.hpp +17 -0
  45. data/ext/cppjieba/src/KeywordExtractor.hpp +173 -0
  46. data/ext/cppjieba/src/Limonp/ArgvContext.hpp +84 -0
  47. data/ext/cppjieba/src/Limonp/BlockingQueue.hpp +128 -0
  48. data/ext/cppjieba/src/Limonp/BoundedQueue.hpp +73 -0
  49. data/ext/cppjieba/src/Limonp/CastFloat.hpp +90 -0
  50. data/ext/cppjieba/src/Limonp/Condition.hpp +48 -0
  51. data/ext/cppjieba/src/Limonp/Config.hpp +118 -0
  52. data/ext/cppjieba/src/Limonp/HandyMacro.hpp +31 -0
  53. data/ext/cppjieba/src/Limonp/InitOnOff.hpp +21 -0
  54. data/ext/cppjieba/src/Limonp/LocalVector.hpp +171 -0
  55. data/ext/cppjieba/src/Limonp/Logger.hpp +74 -0
  56. data/ext/cppjieba/src/Limonp/Md5.hpp +432 -0
  57. data/ext/cppjieba/src/Limonp/MutexLock.hpp +57 -0
  58. data/ext/cppjieba/src/Limonp/MysqlClient.hpp +125 -0
  59. data/ext/cppjieba/src/Limonp/NonCopyable.hpp +22 -0
  60. data/ext/cppjieba/src/Limonp/StdExtension.hpp +139 -0
  61. data/ext/cppjieba/src/Limonp/StringUtil.hpp +349 -0
  62. data/ext/cppjieba/src/Limonp/Thread.hpp +50 -0
  63. data/ext/cppjieba/src/Limonp/ThreadPool.hpp +105 -0
  64. data/ext/cppjieba/src/MPSegment.hpp +148 -0
  65. data/ext/cppjieba/src/MixSegment.hpp +121 -0
  66. data/ext/cppjieba/src/PosTagger.hpp +109 -0
  67. data/ext/cppjieba/src/QuerySegment.hpp +123 -0
  68. data/ext/cppjieba/src/SegmentBase.hpp +78 -0
  69. data/ext/cppjieba/src/TransCode.hpp +63 -0
  70. data/ext/cppjieba/src/Trie.hpp +298 -0
  71. data/ext/cppjieba/test/CMakeLists.txt +7 -0
  72. data/ext/cppjieba/test/keyword_demo.cpp +16 -0
  73. data/ext/cppjieba/test/load_test.cpp +56 -0
  74. data/ext/cppjieba/test/segment_demo.cpp +59 -0
  75. data/ext/cppjieba/test/servertest/go_load_test.sh +2 -0
  76. data/ext/cppjieba/test/servertest/load_test.py +91 -0
  77. data/ext/cppjieba/test/servertest/run_curl.sh +11 -0
  78. data/ext/cppjieba/test/tagging_demo.cpp +12 -0
  79. data/ext/cppjieba/test/testdata/curl.res +1 -0
  80. data/ext/cppjieba/test/testdata/jieba.dict.0.1.utf8 +93 -0
  81. data/ext/cppjieba/test/testdata/jieba.dict.0.utf8 +93 -0
  82. data/ext/cppjieba/test/testdata/jieba.dict.1.utf8 +67 -0
  83. data/ext/cppjieba/test/testdata/jieba.dict.2.utf8 +64 -0
  84. data/ext/cppjieba/test/testdata/load_test.urls +2 -0
  85. data/ext/cppjieba/test/testdata/review.100 +100 -0
  86. data/ext/cppjieba/test/testdata/review.100.res +200 -0
  87. data/ext/cppjieba/test/testdata/server.conf +13 -0
  88. data/ext/cppjieba/test/testdata/testlines.gbk +9 -0
  89. data/ext/cppjieba/test/testdata/testlines.utf8 +8 -0
  90. data/ext/cppjieba/test/testdata/userdict.utf8 +6 -0
  91. data/ext/cppjieba/test/testdata/weicheng.utf8 +247 -0
  92. data/ext/cppjieba/test/unittest/CMakeLists.txt +28 -0
  93. data/ext/cppjieba/test/unittest/TKeywordExtractor.cpp +18 -0
  94. data/ext/cppjieba/test/unittest/TPosTagger.cpp +43 -0
  95. data/ext/cppjieba/test/unittest/TSegments.cpp +187 -0
  96. data/ext/cppjieba/test/unittest/TTrie.cpp +80 -0
  97. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-death-test.h +283 -0
  98. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-message.h +230 -0
  99. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-param-test.h +1421 -0
  100. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-param-test.h.pump +487 -0
  101. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-printers.h +796 -0
  102. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-spi.h +232 -0
  103. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-test-part.h +176 -0
  104. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-typed-test.h +259 -0
  105. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest.h +2155 -0
  106. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest_pred_impl.h +358 -0
  107. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest_prod.h +58 -0
  108. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-death-test-internal.h +308 -0
  109. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-filepath.h +210 -0
  110. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-internal.h +1226 -0
  111. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-linked_ptr.h +233 -0
  112. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-param-util-generated.h +4822 -0
  113. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-param-util-generated.h.pump +301 -0
  114. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-param-util.h +619 -0
  115. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-port.h +1788 -0
  116. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-string.h +350 -0
  117. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-tuple.h +968 -0
  118. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-tuple.h.pump +336 -0
  119. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-type-util.h +3330 -0
  120. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-type-util.h.pump +296 -0
  121. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.deps/.dirstamp +0 -0
  122. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.deps/gtest-all.Plo +681 -0
  123. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.deps/gtest_main.Plo +509 -0
  124. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.dirstamp +0 -0
  125. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-all.cc +48 -0
  126. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-death-test.cc +1234 -0
  127. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-filepath.cc +380 -0
  128. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-internal-inl.h +1038 -0
  129. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-port.cc +746 -0
  130. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-printers.cc +356 -0
  131. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-test-part.cc +110 -0
  132. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-typed-test.cc +110 -0
  133. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest.cc +4898 -0
  134. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest_main.cc +39 -0
  135. data/ext/cppjieba/test/unittest/gtest_main.cpp +39 -0
  136. data/ext/jieba/extconf.rb +26 -0
  137. data/ext/jieba/jieba.c +9 -0
  138. data/ext/jieba/jieba.h +9 -0
  139. data/ext/jieba/segment.cc +88 -0
  140. data/ext/jieba/segment.h +17 -0
  141. data/jieba_rb.gemspec +51 -0
  142. data/lib/jieba_rb/version.rb +3 -0
  143. data/lib/jieba_rb.rb +28 -0
  144. data/test/test_segment.rb +32 -0
  145. metadata +246 -0
@@ -0,0 +1,128 @@
1
+ /*
2
+ https://github.com/chenshuo/muduo/blob/master/muduo/base/BlockingQueue.h
3
+ */
4
+
5
+ #ifndef LIMONP_BLOCKINGQUEUE_HPP
6
+ #define LIMONP_BLOCKINGQUEUE_HPP
7
+
8
+ #include <queue>
9
+ #include "BoundedQueue.hpp"
10
+ #include "Condition.hpp"
11
+
12
+ namespace Limonp
13
+ {
14
+ template<class T>
15
+ class BlockingQueue: NonCopyable
16
+ {
17
+ public:
18
+ BlockingQueue()
19
+ : mutex_(), notEmpty_(mutex_), queue_()
20
+ {
21
+ }
22
+
23
+ void push(const T& x)
24
+ {
25
+ MutexLockGuard lock(mutex_);
26
+ queue_.push(x);
27
+ notEmpty_.notify(); // wait morphing saves us
28
+ }
29
+
30
+ T pop()
31
+ {
32
+ MutexLockGuard lock(mutex_);
33
+ // always use a while-loop, due to spurious wakeup
34
+ while (queue_.empty())
35
+ {
36
+ notEmpty_.wait();
37
+ }
38
+ assert(!queue_.empty());
39
+ T front(queue_.front());
40
+ queue_.pop();
41
+ return front;
42
+ }
43
+
44
+ size_t size() const
45
+ {
46
+ MutexLockGuard lock(mutex_);
47
+ return queue_.size();
48
+ }
49
+ bool empty() const
50
+ {
51
+ return size() == 0;
52
+ }
53
+
54
+ private:
55
+ mutable MutexLock mutex_;
56
+ Condition notEmpty_;
57
+ std::queue<T> queue_;
58
+ };
59
+
60
+ template<typename T>
61
+ class BoundedBlockingQueue : NonCopyable
62
+ {
63
+ public:
64
+ explicit BoundedBlockingQueue(size_t maxSize)
65
+ : mutex_(),
66
+ notEmpty_(mutex_),
67
+ notFull_(mutex_),
68
+ queue_(maxSize)
69
+ {}
70
+
71
+ void push(const T& x)
72
+ {
73
+ MutexLockGuard lock(mutex_);
74
+ while (queue_.full())
75
+ {
76
+ notFull_.wait();
77
+ }
78
+ assert(!queue_.full());
79
+ queue_.push(x);
80
+ notEmpty_.notify();
81
+ }
82
+
83
+ T pop()
84
+ {
85
+ MutexLockGuard lock(mutex_);
86
+ while (queue_.empty())
87
+ {
88
+ notEmpty_.wait();
89
+ }
90
+ assert(!queue_.empty());
91
+ T res = queue_.pop();
92
+ notFull_.notify();
93
+ return res;
94
+ }
95
+
96
+ bool empty() const
97
+ {
98
+ MutexLockGuard lock(mutex_);
99
+ return queue_.empty();
100
+ }
101
+
102
+ bool full() const
103
+ {
104
+ MutexLockGuard lock(mutex_);
105
+ return queue_.full();
106
+ }
107
+
108
+ size_t size() const
109
+ {
110
+ MutexLockGuard lock(mutex_);
111
+ return queue_.size();
112
+ }
113
+
114
+ size_t capacity() const
115
+ {
116
+ return queue_.capacity();
117
+ }
118
+
119
+ private:
120
+ mutable MutexLock mutex_;
121
+ Condition notEmpty_;
122
+ Condition notFull_;
123
+ BoundedQueue<T> queue_;
124
+ };
125
+
126
+ }
127
+
128
+ #endif
@@ -0,0 +1,73 @@
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
+ {
10
+ using namespace std;
11
+ template<class T>
12
+ class BoundedQueue
13
+ {
14
+ private:
15
+ size_t head_;
16
+ size_t tail_;
17
+ size_t size_;
18
+ const size_t capacity_;
19
+ vector<T> circular__buffer;
20
+ public:
21
+ explicit BoundedQueue(size_t capacity): capacity_(capacity), circular__buffer(capacity)
22
+ {
23
+ head_ = 0;
24
+ tail_ = 0;
25
+ size_ = 0;
26
+ assert(capacity_);
27
+ }
28
+ ~BoundedQueue(){}
29
+ public:
30
+ void clear()
31
+ {
32
+ head_ = 0;
33
+ tail_ = 0;
34
+ size_ = 0;
35
+ }
36
+ bool empty() const
37
+ {
38
+ return !size_;
39
+ }
40
+ bool full() const
41
+ {
42
+ return capacity_ == size_;
43
+ }
44
+ size_t size() const
45
+ {
46
+ return size_;
47
+ }
48
+ size_t capacity() const
49
+ {
50
+ return capacity_;
51
+ }
52
+
53
+ void push(const T& t)
54
+ {
55
+ assert(!full());
56
+ circular__buffer[tail_] = t;
57
+ tail_ = (tail_ + 1) % capacity_;
58
+ size_ ++;
59
+ }
60
+
61
+ T pop()
62
+ {
63
+ assert(!empty());
64
+ size_t oldPos = head_;
65
+ head_ = (head_ + 1) % capacity_;
66
+ size_ --;
67
+ return circular__buffer[oldPos];
68
+ }
69
+
70
+ };
71
+ }
72
+
73
+ #endif
@@ -0,0 +1,90 @@
1
+ #ifndef LIMONP_CAST_FUNCTS_H
2
+ #define LIMONP_CAST_FUNCTS_H
3
+
4
+ namespace Limonp
5
+ {
6
+ namespace CastFloat
7
+ {
8
+ //logical and or
9
+ static const int sign_32 = 0xC0000000;
10
+ static const int exponent_32 = 0x07800000;
11
+ static const int mantissa_32 = 0x007FE000;
12
+ static const int sign_exponent_32 = 0x40000000;
13
+ static const int loss_32 = 0x38000000;
14
+
15
+ static const short sign_16 = (short)0xC000;
16
+ static const short exponent_16 = (short)0x3C00;
17
+ static const short mantissa_16 = (short)0x03FF;
18
+ static const short sign_exponent_16 = (short)0x4000;
19
+ static const int exponent_fill_32 = 0x38000000;
20
+
21
+ //infinite
22
+ static const short infinite_16 = (short) 0x7FFF;
23
+ static const short infinitesmall_16 = (short) 0x0000;
24
+
25
+ inline float intBitsToFloat(unsigned int x)
26
+ {
27
+ union
28
+ {
29
+ float f;
30
+ int i;
31
+ }u;
32
+ u.i = x;
33
+ return u.f;
34
+ }
35
+
36
+ inline int floatToIntBits(float f)
37
+ {
38
+ union
39
+ {
40
+ float f;
41
+ int i ;
42
+ }u;
43
+ u.f = f;
44
+ return u.i;
45
+ }
46
+
47
+ inline short floatToShortBits(float f)
48
+ {
49
+ int fi = floatToIntBits(f);
50
+
51
+ // 提取关键信息
52
+ short sign = (short) ((unsigned int)(fi & sign_32) >> 16);
53
+ short exponent = (short) ((unsigned int)(fi & exponent_32) >> 13);
54
+ short mantissa = (short) ((unsigned int)(fi & mantissa_32) >> 13);
55
+ // 生成编码结果
56
+ short code = (short) (sign | exponent | mantissa);
57
+ // 无穷大量、无穷小量的处理
58
+ if ((fi & loss_32) > 0 && (fi & sign_exponent_32) > 0) {
59
+ // 当指数符号为1时(正次方),且左234位为1,返回无穷大量
60
+ return (short) (code | infinite_16);
61
+ }
62
+ if (((fi & loss_32) ^ loss_32) > 0 && (fi & sign_exponent_32) == 0) {
63
+ // 当指数符号位0时(负次方),且左234位为0(与111异或>0),返回无穷小量
64
+ return infinitesmall_16;
65
+ }
66
+
67
+ return code;
68
+ }
69
+
70
+ inline float shortBitsToFloat(short s)
71
+ {
72
+ /*
73
+ * 指数空余3位:若符号位为1,补0;若符号位为0,补1。 尾数位在后补0(13个)
74
+ */
75
+ int sign = ((int) (s & sign_16)) << 16;
76
+ int exponent = ((int) (s & exponent_16)) << 13;
77
+ // 指数符号位为0,234位补1
78
+ if ((s & sign_exponent_16) == 0 && s != 0) {
79
+ exponent |= exponent_fill_32;
80
+ }
81
+ int mantissa = ((int) (s & mantissa_16)) << 13;
82
+ // 生成解码结果
83
+ int code = sign | exponent | mantissa;
84
+ return intBitsToFloat(code);
85
+
86
+ }
87
+ }
88
+ }
89
+
90
+ #endif
@@ -0,0 +1,48 @@
1
+ /*
2
+ * https://github.com/chenshuo/muduo/blob/master/muduo/base/Condition.h
3
+ */
4
+
5
+ #ifndef LIMONP_CONDITION_HPP
6
+ #define LIMONP_CONDITION_HPP
7
+
8
+ #include "MutexLock.hpp"
9
+
10
+ namespace Limonp
11
+ {
12
+ class Condition : NonCopyable
13
+ {
14
+ public:
15
+ explicit Condition(MutexLock& mutex)
16
+ : mutex_(mutex)
17
+ {
18
+ LIMONP_CHECK(!pthread_cond_init(&pcond_, NULL));
19
+ }
20
+
21
+ ~Condition()
22
+ {
23
+ LIMONP_CHECK(!pthread_cond_destroy(&pcond_));
24
+ }
25
+
26
+ void wait()
27
+ {
28
+ LIMONP_CHECK(!pthread_cond_wait(&pcond_, mutex_.getPthreadMutex()));
29
+ }
30
+
31
+ void notify()
32
+ {
33
+ LIMONP_CHECK(!pthread_cond_signal(&pcond_));
34
+ }
35
+
36
+ void notifyAll()
37
+ {
38
+ LIMONP_CHECK(!pthread_cond_broadcast(&pcond_));
39
+ }
40
+
41
+ private:
42
+ MutexLock& mutex_;
43
+ pthread_cond_t pcond_;
44
+ };
45
+
46
+ }
47
+
48
+ #endif
@@ -0,0 +1,118 @@
1
+ /************************************
2
+ * file enc : utf8
3
+ * author : wuyanyi09@gmail.com
4
+ ************************************/
5
+ #ifndef LIMONP_CONFIG_H
6
+ #define LIMONP_CONFIG_H
7
+
8
+
9
+ #include <map>
10
+ #include <fstream>
11
+ #include <iostream>
12
+ #include <assert.h>
13
+ #include "StringUtil.hpp"
14
+
15
+ namespace Limonp
16
+ {
17
+ using namespace std;
18
+ class Config
19
+ {
20
+ public:
21
+ explicit Config(const string& filePath)
22
+ {
23
+ loadFile_(filePath);
24
+ }
25
+ public:
26
+ operator bool ()
27
+ {
28
+ return !map_.empty();
29
+ }
30
+ private:
31
+ void loadFile_(const string& filePath)
32
+ {
33
+ ifstream ifs(filePath.c_str());
34
+ assert(ifs);
35
+ string line;
36
+ vector<string> vecBuf;
37
+ size_t lineno = 0;
38
+ while(getline(ifs, line))
39
+ {
40
+ lineno ++;
41
+ trim(line);
42
+ if(line.empty() || startsWith(line, "#"))
43
+ {
44
+ continue;
45
+ }
46
+ vecBuf.clear();
47
+ if(!split(line, vecBuf, "=") || 2 != vecBuf.size())
48
+ {
49
+ fprintf(stderr, "line[%s] illegal.\n", line.c_str());
50
+ assert(false);
51
+ continue;
52
+ }
53
+ string& key = vecBuf[0];
54
+ string& value = vecBuf[1];
55
+ trim(key);
56
+ trim(value);
57
+ if(!map_.insert(make_pair(key, value)).second)
58
+ {
59
+ fprintf(stderr, "key[%s] already exits.\n", key.c_str());
60
+ assert(false);
61
+ continue;
62
+ }
63
+ }
64
+ ifs.close();
65
+ }
66
+ public:
67
+ bool get(const string& key, string& value) const
68
+ {
69
+ map<string, string>::const_iterator it = map_.find(key);
70
+ if(map_.end() != it)
71
+ {
72
+ value = it->second;
73
+ return true;
74
+ }
75
+ return false;
76
+ }
77
+ bool get(const string& key, int & value) const
78
+ {
79
+ string str;
80
+ if(!get(key, str)) {
81
+ return false;
82
+ }
83
+ value = atoi(str.c_str());
84
+ return true;
85
+ }
86
+ const char* operator [] (const char* key) const
87
+ {
88
+ if(NULL == key)
89
+ {
90
+ return NULL;
91
+ }
92
+ map<string, string>::const_iterator it = map_.find(key);
93
+ if(map_.end() != it)
94
+ {
95
+ return it->second.c_str();
96
+ }
97
+ return NULL;
98
+ }
99
+ public:
100
+ string getConfigInfo() const
101
+ {
102
+ string res;
103
+ res << *this;
104
+ return res;
105
+ }
106
+ private:
107
+ map<string, string> map_;
108
+ private:
109
+ friend ostream& operator << (ostream& os, const Config& config);
110
+ };
111
+
112
+ inline ostream& operator << (ostream& os, const Config& config)
113
+ {
114
+ return os << config.map_;
115
+ }
116
+ }
117
+
118
+ #endif
@@ -0,0 +1,31 @@
1
+ #ifndef LIMONP_HANDY_MACRO_HPP
2
+ #define LIMONP_HANDY_MACRO_HPP
3
+
4
+ #include <cstdio>
5
+ #include <cstdlib>
6
+
7
+ #define LIMONP_CHECK(exp) \
8
+ if(!(exp)){fprintf(stderr, "File:%s, Line:%d Exp:[" #exp "] is true, abort.\n", __FILE__, __LINE__); abort();}
9
+
10
+ #define print(x) cout<< #x": " << x <<endl
11
+ /*
12
+ #define XX_GET_SET(varType, varName, funName)\
13
+ private: varType varName;\
14
+ public: inline varType get##funName(void) const {return varName;}\
15
+ public: inline void set##funName(varType var) {varName = var;}
16
+
17
+ #define XX_GET(varType, varName, funName)\
18
+ private: varType varName;\
19
+ public: inline varType get##funName(void) const {return varName;}
20
+
21
+ #define XX_SET(varType, varName, funName)\
22
+ private: varType varName;\
23
+ public: inline void set##funName(varType var) {varName = var;}
24
+
25
+ #define XX_GET_SET_BY_REF(varType, varName, funName)\
26
+ private: varType varName;\
27
+ public: inline const varType& get##funName(void) const {return varName;}\
28
+ public: inline void set##funName(const varType& var){varName = var;}
29
+ */
30
+
31
+ #endif
@@ -0,0 +1,21 @@
1
+ #ifndef LIMONP_INITONOFF_H
2
+ #define LIMONP_INITONOFF_H
3
+
4
+ namespace Limonp
5
+ {
6
+ class InitOnOff
7
+ {
8
+ public:
9
+ InitOnOff():isInited_(false){};
10
+ ~InitOnOff(){};
11
+ protected:
12
+ bool isInited_;
13
+ bool getInitFlag_()const{return isInited_;};
14
+ bool setInitFlag_(bool flag){return isInited_ = flag;};
15
+ public:
16
+ operator bool() const {return getInitFlag_();};
17
+
18
+ };
19
+ }
20
+
21
+ #endif
@@ -0,0 +1,171 @@
1
+ #ifndef LIMONP_LOCAL_VECTOR_HPP
2
+ #define LIMONP_LOCAL_VECTOR_HPP
3
+
4
+ #include <iostream>
5
+ #include <stdlib.h>
6
+ #include <assert.h>
7
+ #include <string.h>
8
+
9
+ namespace Limonp
10
+ {
11
+ using namespace std;
12
+ /*
13
+ * LocalVector<T> : T must be primitive type (char , int, size_t), if T is struct or class, LocalVector<T> may be dangerous..
14
+ * LocalVector<T> is simple and not well-tested.
15
+ */
16
+ const size_t LOCAL_VECTOR_BUFFER_SIZE = 16;
17
+ template <class T>
18
+ class LocalVector
19
+ {
20
+ public:
21
+ typedef const T* const_iterator ;
22
+ typedef T value_type;
23
+ typedef size_t size_type;
24
+ private:
25
+ T buffer_[LOCAL_VECTOR_BUFFER_SIZE];
26
+ T * ptr_;
27
+ size_t size_;
28
+ size_t capacity_;
29
+ public:
30
+ LocalVector()
31
+ {
32
+ init_();
33
+ };
34
+ LocalVector(const LocalVector<T>& vec)
35
+ {
36
+ init_();
37
+ *this = vec;
38
+ }
39
+ LocalVector(const_iterator begin, const_iterator end) // TODO: make it faster
40
+ {
41
+ init_();
42
+ while(begin != end)
43
+ {
44
+ push_back(*begin++);
45
+ }
46
+ }
47
+ LocalVector(size_t size, const T& t) // TODO: make it faster
48
+ {
49
+ init_();
50
+ while(size--)
51
+ {
52
+ push_back(t);
53
+ }
54
+ }
55
+ ~LocalVector()
56
+ {
57
+ if(ptr_ != buffer_)
58
+ {
59
+ free(ptr_);
60
+ }
61
+ };
62
+ public:
63
+ LocalVector<T>& operator = (const LocalVector<T>& vec)
64
+ {
65
+ clear();
66
+ size_ = vec.size();
67
+ capacity_ = vec.capacity();
68
+ if(vec.buffer_ == vec.ptr_)
69
+ {
70
+ memcpy(buffer_, vec.buffer_, sizeof(T) * size_);
71
+ ptr_ = buffer_;
72
+ }
73
+ else
74
+ {
75
+ ptr_ = (T*) malloc(vec.capacity() * sizeof(T));
76
+ assert(ptr_);
77
+ memcpy(ptr_, vec.ptr_, vec.size() * sizeof(T));
78
+ }
79
+ return *this;
80
+ }
81
+ private:
82
+ void init_()
83
+ {
84
+ ptr_ = buffer_;
85
+ size_ = 0;
86
+ capacity_ = LOCAL_VECTOR_BUFFER_SIZE;
87
+ }
88
+ public:
89
+ T& operator [] (size_t i)
90
+ {
91
+ return ptr_[i];
92
+ }
93
+ const T& operator [] (size_t i) const
94
+ {
95
+ return ptr_[i];
96
+ }
97
+ void push_back(const T& t)
98
+ {
99
+ if(size_ == capacity_)
100
+ {
101
+ assert(capacity_);
102
+ reserve(capacity_ * 2);
103
+ }
104
+ ptr_[size_ ++ ] = t;
105
+ }
106
+ void reserve(size_t size)
107
+ {
108
+ if(size <= capacity_)
109
+ {
110
+ return;
111
+ }
112
+ T * next = (T*)malloc(sizeof(T) * size);
113
+ assert(next);
114
+ T * old = ptr_;
115
+ ptr_ = next;
116
+ memcpy(ptr_, old, sizeof(T) * capacity_);
117
+ capacity_ = size;
118
+ if(old != buffer_)
119
+ {
120
+ free(old);
121
+ }
122
+ }
123
+ bool empty() const
124
+ {
125
+ return 0 == size();
126
+ }
127
+ size_t size() const
128
+ {
129
+ return size_;
130
+ }
131
+ size_t capacity() const
132
+ {
133
+ return capacity_;
134
+ }
135
+ const_iterator begin() const
136
+ {
137
+ return ptr_;
138
+ }
139
+ const_iterator end() const
140
+ {
141
+ return ptr_ + size_;
142
+ }
143
+ void clear()
144
+ {
145
+ if(ptr_ != buffer_)
146
+ {
147
+ free(ptr_);
148
+ }
149
+ init_();
150
+ }
151
+ };
152
+
153
+ template <class T>
154
+ ostream & operator << (ostream& os, const LocalVector<T>& vec)
155
+ {
156
+ if(vec.empty())
157
+ {
158
+ return os << "[]";
159
+ }
160
+ os<<"[\""<<vec[0];
161
+ for(size_t i = 1; i < vec.size(); i++)
162
+ {
163
+ os<<"\", \""<<vec[i];
164
+ }
165
+ os<<"\"]";
166
+ return os;
167
+ }
168
+
169
+ }
170
+
171
+ #endif