summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2016-02-29 15:49:29 +0800
committerPeng Wu <alexepico@gmail.com>2016-02-29 15:49:29 +0800
commitb3fb0481719dfdbbf2cf7a692a831a87d2416419 (patch)
tree76a69dc3e76dc3f3fa18c930cd70c2b388bcf8d9 /src
parent571322984a1b87bf6341492d7289a77d13a5a9d6 (diff)
downloadlibpinyin-b3fb0481719dfdbbf2cf7a692a831a87d2416419.tar.gz
libpinyin-b3fb0481719dfdbbf2cf7a692a831a87d2416419.tar.xz
libpinyin-b3fb0481719dfdbbf2cf7a692a831a87d2416419.zip
write search method
Diffstat (limited to 'src')
-rw-r--r--src/storage/chewing_large_table2.cpp2
-rw-r--r--src/storage/chewing_large_table2.h70
-rw-r--r--src/storage/pinyin_phrase3.h21
3 files changed, 88 insertions, 5 deletions
diff --git a/src/storage/chewing_large_table2.cpp b/src/storage/chewing_large_table2.cpp
index 66bb5e9..2376a5c 100644
--- a/src/storage/chewing_large_table2.cpp
+++ b/src/storage/chewing_large_table2.cpp
@@ -20,8 +20,6 @@
*/
#include "chewing_large_table2.h"
-#include "pinyin_phrase2.h"
-#include "pinyin_phrase3.h"
#include "pinyin_parser2.h"
diff --git a/src/storage/chewing_large_table2.h b/src/storage/chewing_large_table2.h
index 217ab9b..b6c9c35 100644
--- a/src/storage/chewing_large_table2.h
+++ b/src/storage/chewing_large_table2.h
@@ -25,6 +25,8 @@
#include "novel_types.h"
#include "memory_chunk.h"
#include "chewing_key.h"
+#include "pinyin_phrase2.h"
+#include "pinyin_phrase3.h"
#ifdef HAVE_BERKELEY_DB
#include "chewing_large_table2_bdb.h"
@@ -41,19 +43,81 @@ template<size_t phrase_length>
class ChewingTableEntry{
friend class ChewingLargeTable2;
protected:
+ typedef PinyinIndexItem2<phrase_length> IndexItem;
+
+protected:
MemoryChunk m_chunk;
- /* cache for storing the chewing keys index. */
- ChewingKey m_cache_index[phrase_length];
+ /* The cache item of IndexItem. */
+ IndexItem m_cache_item;
private:
/* Disallow used outside. */
ChewingTableEntry() {}
public:
+ /* convert method. */
+ /* compress consecutive tokens */
+ int convert(const ChewingKey keys[],
+ IndexItem * begin, IndexItem * end,
+ PhraseIndexRanges ranges) const {
+ IndexItem * iter = NULL;
+ PhraseIndexRange cursor;
+ GArray * head, * cursor_head = NULL;
+
+ int result = SEARCH_NONE;
+ /* TODO: check the below code */
+ cursor.m_range_begin = null_token; cursor.m_range_end = null_token;
+ for (iter = begin; iter != end; ++iter) {
+ if (0 != pinyin_compare_with_tones
+ (keys, iter->m_keys, phrase_length))
+ continue;
+
+ phrase_token_t token = iter->m_token;
+ head = ranges[PHRASE_INDEX_LIBRARY_INDEX(token)];
+ if (NULL == head)
+ continue;
+
+ result |= SEARCH_OK;
+
+ if (null_token == cursor.m_range_begin) {
+ cursor.m_range_begin = token;
+ cursor.m_range_end = token + 1;
+ cursor_head = head;
+ } else if (cursor.m_range_end == token &&
+ PHRASE_INDEX_LIBRARY_INDEX(cursor.m_range_begin) ==
+ PHRASE_INDEX_LIBRARY_INDEX(token)) {
+ ++cursor.m_range_end;
+ } else {
+ g_array_append_val(cursor_head, cursor);
+ cursor.m_range_begin = token; cursor.m_range_end = token + 1;
+ cursor_head = head;
+ }
+ }
+
+ if (null_token == cursor.m_range_begin)
+ return result;
+
+ g_array_append_val(cursor_head, cursor);
+ return result;
+ }
+
/* search method */
int search(/* in */ const ChewingKey keys[],
- /* out */ PhraseIndexRanges ranges) const;
+ /* out */ PhraseIndexRanges ranges) const {
+ int result = SEARCH_NONE;
+
+ compute_chewing_index(keys, m_cache_item.m_keys, phrase_length);
+
+ const IndexItem * begin = (IndexItem *) m_chunk.begin();
+ const IndexItem * end = (IndexItem *) m_chunk.end();
+
+ std_lite::pair<const IndexItem *, const IndexItem *> range=
+ std_lite::equal_range(begin, end, m_cache_item,
+ phrase_less_than_with_tones<phrase_length>);
+
+ return convert(keys, range.m_first, range.m_second, ranges);
+ }
/* add/remove index method */
int add_index(/* in */ const ChewingKey keys[],
diff --git a/src/storage/pinyin_phrase3.h b/src/storage/pinyin_phrase3.h
index 9e72c14..800d409 100644
--- a/src/storage/pinyin_phrase3.h
+++ b/src/storage/pinyin_phrase3.h
@@ -179,7 +179,13 @@ template<size_t phrase_length>
struct PinyinIndexItem2{
phrase_token_t m_token;
ChewingKey m_keys[phrase_length];
+
public:
+ PinyinIndexItem2<phrase_length> () {
+ memset(m_keys, 0, sizeof(ChewingKey) * phrase_length);
+ m_token = null_token;
+ }
+
PinyinIndexItem2<phrase_length> (const ChewingKey * keys,
phrase_token_t token) {
memmove(m_keys, keys, sizeof(ChewingKey) * phrase_length);
@@ -205,6 +211,21 @@ inline bool phrase_exact_less_than2(const PinyinIndexItem2<phrase_length> &lhs,
return 0 > phrase_exact_compare2<phrase_length>(lhs, rhs);
}
+template<size_t phrase_length>
+inline int phrase_compare_with_tones(const PinyinIndexItem2<phrase_length> &lhs,
+ const PinyinIndexItem2<phrase_length> &rhs)
+{
+ ChewingKey * keys_lhs = (ChewingKey *) lhs.m_keys;
+ ChewingKey * keys_rhs = (ChewingKey *) rhs.m_keys;
+ return pinyin_compare_with_tones(keys_lhs, keys_rhs, phrase_length);
+}
+
+template<size_t phrase_length>
+inline int phrase_less_than_with_tones(const PinyinIndexItem2<phrase_length> &lhs,
+ const PinyinIndexItem2<phrase_length> &rhs)
+{
+ return 0 > phrase_compare_with_tones<phrase_length>(lhs, rhs);
+}
};