diff options
| author | Peng Wu <alexepico@gmail.com> | 2016-03-08 15:55:09 +0800 |
|---|---|---|
| committer | Peng Wu <alexepico@gmail.com> | 2016-03-08 15:55:09 +0800 |
| commit | 026f53bd34ba93050178b36fc4bdde2a7528b5cc (patch) | |
| tree | a9606905aa7d5e3e1744aa6be31fd86396865681 /src | |
| parent | 5d16830d7e1ce3df548bca0017a4deaccdc8cfb0 (diff) | |
| download | libpinyin-026f53bd34ba93050178b36fc4bdde2a7528b5cc.tar.gz libpinyin-026f53bd34ba93050178b36fc4bdde2a7528b5cc.tar.xz libpinyin-026f53bd34ba93050178b36fc4bdde2a7528b5cc.zip | |
write search method
Diffstat (limited to 'src')
| -rw-r--r-- | src/storage/chewing_large_table2.h | 10 | ||||
| -rw-r--r-- | src/storage/chewing_large_table2_bdb.cpp | 84 | ||||
| -rw-r--r-- | src/storage/chewing_large_table2_bdb.h | 7 |
3 files changed, 93 insertions, 8 deletions
diff --git a/src/storage/chewing_large_table2.h b/src/storage/chewing_large_table2.h index 68821ed..f612718 100644 --- a/src/storage/chewing_large_table2.h +++ b/src/storage/chewing_large_table2.h @@ -59,9 +59,9 @@ public: /* convert method. */ /* compress consecutive tokens */ int convert(const ChewingKey keys[], - IndexItem * begin, IndexItem * end, - PhraseIndexRanges ranges) const { - IndexItem * iter = NULL; + const IndexItem * begin, const IndexItem * end, + PhraseIndexRanges ranges) { + const IndexItem * iter = NULL; PhraseIndexRange cursor; GArray * head, * cursor_head = NULL; @@ -104,9 +104,7 @@ public: /* search method */ int search(/* in */ const ChewingKey keys[], - /* out */ PhraseIndexRanges ranges) const { - int result = SEARCH_NONE; - + /* out */ PhraseIndexRanges ranges) { compute_chewing_index(keys, m_cache_item.m_keys, phrase_length); const IndexItem * begin = (IndexItem *) m_chunk.begin(); diff --git a/src/storage/chewing_large_table2_bdb.cpp b/src/storage/chewing_large_table2_bdb.cpp index fb15eb2..877af40 100644 --- a/src/storage/chewing_large_table2_bdb.cpp +++ b/src/storage/chewing_large_table2_bdb.cpp @@ -215,4 +215,88 @@ bool ChewingLargeTable2::store_db(const char * new_filename) { return true; } +template<size_t phrase_length> +int ChewingLargeTable2::search_internal(/* in */ const ChewingKey keys[], + /* out */ PhraseIndexRanges ranges) const { + int result = SEARCH_NONE; + + if (NULL == m_db) + return result; + + ChewingTableEntry<phrase_length> * entry = + (ChewingTableEntry<phrase_length> *) + g_ptr_array_index(m_entries, phrase_length); + assert(NULL != entry); + + DBT db_key; + memset(&db_key, 0, sizeof(DBT)); + db_key.data = (void *) keys; + db_key.size = phrase_length * sizeof(ChewingKey); + + DBT db_data; + memset(&db_data, 0, sizeof(DBT)); + int ret = m_db->get(m_db, NULL, &db_key, &db_data, 0); + if (ret != 0) + return result; + + /* continue searching. */ + result |= SEARCH_CONTINUED; + + entry->m_chunk.set_chunk(db_data.data, db_data.size, NULL); + + result = entry->search(keys, ranges) | result; + + return result; +} + + +int ChewingLargeTable2::search_internal(int phrase_length, + /* in */ const ChewingKey keys[], + /* out */ PhraseIndexRanges ranges) const { +#define CASE(len) case len: \ + { \ + return search_internal<len>(keys, ranges); \ + } + + switch(phrase_length) { + CASE(1); + CASE(2); + CASE(3); + CASE(4); + CASE(5); + CASE(6); + CASE(7); + CASE(8); + CASE(9); + CASE(10); + CASE(11); + CASE(12); + CASE(13); + CASE(14); + CASE(15); + CASE(16); + default: + assert(false); + } + +#undef CASE + + return SEARCH_NONE; +} + +/* search method */ +int ChewingLargeTable2::search(int phrase_length, + /* in */ const ChewingKey keys[], + /* out */ PhraseIndexRanges ranges) { + if (contains_incomplete_pinyin(keys, phrase_length)) { + compute_incomplete_chewing_index(keys, m_cache_index, phrase_length); + return search_internal(phrase_length, m_cache_index, ranges); + } else { + compute_chewing_index(keys, m_cache_index, phrase_length); + return search_internal(phrase_length, m_cache_index, ranges); + } + + return SEARCH_NONE; +} + }; diff --git a/src/storage/chewing_large_table2_bdb.h b/src/storage/chewing_large_table2_bdb.h index a05b933..494d65a 100644 --- a/src/storage/chewing_large_table2_bdb.h +++ b/src/storage/chewing_large_table2_bdb.h @@ -32,7 +32,7 @@ template<size_t phrase_length> class ChewingTableEntry; class ChewingLargeTable2{ -private: +protected: /* member variables. */ DB * m_db; @@ -41,6 +41,9 @@ protected: all elements are always available. */ GPtrArray * m_entries; + /* The cache index of ChewingKey. */ + ChewingKey m_cache_index[MAX_PHRASE_LENGTH]; + void init_entries(); void reset(); @@ -73,7 +76,7 @@ public: /* search method */ int search(int phrase_length, /* in */ const ChewingKey keys[], - /* out */ PhraseIndexRanges ranges) const; + /* out */ PhraseIndexRanges ranges); /* add/remove index method */ int add_index(int phrase_length, /* in */ const ChewingKey keys[], |
