summaryrefslogtreecommitdiffstats
path: root/src/storage
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2010-08-26 15:41:42 +0800
committerPeng Wu <alexepico@gmail.com>2010-08-26 15:41:42 +0800
commit2e9663da330866e655ba414bed0dfcd0de486864 (patch)
tree194b7b6540d61b322f3a5b8cccefd44ab9ae5159 /src/storage
parentb601fb3a3e0799d744f3eff61449d79ffbb6417d (diff)
downloadlibpinyin-2e9663da330866e655ba414bed0dfcd0de486864.tar.gz
libpinyin-2e9663da330866e655ba414bed0dfcd0de486864.tar.xz
libpinyin-2e9663da330866e655ba414bed0dfcd0de486864.zip
add search to phrase large table.
Diffstat (limited to 'src/storage')
-rw-r--r--src/storage/phrase_large_table.cpp40
-rw-r--r--src/storage/pinyin_phrase.h2
2 files changed, 40 insertions, 2 deletions
diff --git a/src/storage/phrase_large_table.cpp b/src/storage/phrase_large_table.cpp
index a033cee..38d6d5e 100644
--- a/src/storage/phrase_large_table.cpp
+++ b/src/storage/phrase_large_table.cpp
@@ -23,6 +23,32 @@
#include <string.h>
#include "phrase_large_table.h"
+template<size_t phrase_length>
+struct PhraseIndexItem{
+ phrase_token_t m_token;
+ utf16_t m_phrase[phrase_length];
+public:
+ PhraseIndexItem<phrase_length>(utf16_t phrase[], phrase_token_t token){
+ memmove(m_phrase, phrase, sizeof(utf16_t) * phrase_length);
+ m_token = token;
+ }
+};
+
+template<size_t phrase_length>
+static int phrase_compare(const PhraseIndexItem<phrase_length> &lhs,
+ const PhraseIndexItem<phrase_length> &rhs){
+ utf16_t * phrase_lhs = (utf16_t *) lhs.m_phrase;
+ utf16_t * phrase_rhs = (utf16_t *) rhs.m_phrase;
+
+ return memcmp(phrase_lhs, phrase_rhs, sizeof(utf16_t) * phrase_length);
+}
+
+template<size_t phrase_length>
+static bool phrase_less_than(const PhraseIndexItem<phrase_length> & lhs,
+ const PhraseIndexItem<phrase_length> & rhs){
+ return 0 > phrase_compare(lhs, rhs);
+}
+
PhraseBitmapIndexLevel::PhraseBitmapIndexLevel(){
memset(m_phrase_length_indexes, 0, sizeof(m_phrase_length_indexes));
}
@@ -132,5 +158,17 @@ int PhraseLengthIndexLevel::search(int phrase_length,
template<size_t phrase_length>
int PhraseArrayIndexLevel<phrase_length>::search(/* in */ utf16_t phrase[], /* out */ phrase_token_t & token){
-
+ PhraseIndexItem<phrase_length> * chunk_begin, * chunk_end;
+ chunk_begin = (PhraseIndexItem<phrase_length> *)m_chunk.begin();
+ chunk_end = (PhraseIndexItem<phrase_length> *)m_chunk.end();
+ PhraseIndexItem<phrase_length> search_elem(phrase, -1);
+
+ //do the search
+ std_lite::pair<PhraseIndexItem<phrase_length> *, PhraseIndexItem<phrase_length> *> range;
+ range = std_lite::equal_range(chunk_begin, chunk_end, search_elem, phrase_less_than<phrase_length>);
+
+ if ( range.first == range.second )
+ return SEARCH_NONE;
+ assert(range.second - range.first == 1);
+ return SEARCH_OK;
}
diff --git a/src/storage/pinyin_phrase.h b/src/storage/pinyin_phrase.h
index 9ce2c3f..1902f23 100644
--- a/src/storage/pinyin_phrase.h
+++ b/src/storage/pinyin_phrase.h
@@ -186,7 +186,7 @@ inline void compute_upper_value(const PinyinCustomSettings &custom,
}
}
-template<int phrase_length>
+template<size_t phrase_length>
struct PinyinIndexItem{
phrase_token_t m_token;
PinyinKey m_keys[phrase_length];