From 2c29075b74f2c6d11f7348fc7cfa2f2d3d7b923e Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Wed, 14 Sep 2011 13:51:59 +0800 Subject: refactor pinyin phrase --- src/storage/pinyin_large_table.cpp | 18 +++++++++++------- src/storage/pinyin_phrase.h | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/storage/pinyin_large_table.cpp b/src/storage/pinyin_large_table.cpp index 29febce..204518d 100644 --- a/src/storage/pinyin_large_table.cpp +++ b/src/storage/pinyin_large_table.cpp @@ -327,18 +327,21 @@ int PinyinLengthIndexLevel::search( int phrase_length, template int PinyinArrayIndexLevel::search(/* in */ PinyinCustomSettings * custom, /* in */ PinyinKey keys[], /* out */ PhraseIndexRanges ranges){ - PhraseExactLessThan m_lessthan; PinyinIndexItem * chunk_begin, * chunk_end; chunk_begin = (PinyinIndexItem *)m_chunk.begin(); chunk_end = (PinyinIndexItem *)m_chunk.end(); + //do the search PinyinKey left_keys[phrase_length], right_keys[phrase_length]; compute_lower_value(*custom, keys, left_keys, phrase_length); compute_upper_value(*custom, keys, right_keys, phrase_length); + PinyinIndexItem left(left_keys, -1), right(right_keys, -1); - PinyinIndexItem * begin = std_lite::lower_bound(chunk_begin, chunk_end, left, m_lessthan); - PinyinIndexItem * end = std_lite::upper_bound(chunk_begin, chunk_end, right, m_lessthan); + PinyinIndexItem * begin = std_lite::lower_bound + (chunk_begin, chunk_end, left, phrase_exact_less_than); + PinyinIndexItem * end = std_lite::upper_bound + (chunk_begin, chunk_end, right, phrase_exact_less_than); return convert(custom, keys, begin, end, ranges); } @@ -474,7 +477,6 @@ int PinyinLengthIndexLevel::remove_index( int phrase_length, /* in */ PinyinKey template int PinyinArrayIndexLevel::add_index(/* in */ PinyinKey keys[], /* in */ phrase_token_t token){ - PhraseExactLessThan m_lessthan; PinyinIndexItem * buf_begin, * buf_end; PinyinIndexItem new_elem(keys, token); @@ -482,7 +484,8 @@ int PinyinArrayIndexLevel::add_index(/* in */ PinyinKey keys[], / buf_end = (PinyinIndexItem *) m_chunk.end(); std_lite::pair *, PinyinIndexItem *> range; - range = std_lite::equal_range(buf_begin, buf_end, new_elem, m_lessthan); + range = std_lite::equal_range + (buf_begin, buf_end, new_elem, phrase_exact_less_than); PinyinIndexItem * cur_elem; for ( cur_elem = range.first; @@ -502,7 +505,6 @@ int PinyinArrayIndexLevel::add_index(/* in */ PinyinKey keys[], / template int PinyinArrayIndexLevel::remove_index(/* in */ PinyinKey keys[], /* in */ phrase_token_t token){ - PhraseExactLessThan m_lessthan; PinyinIndexItem * buf_begin, * buf_end; PinyinIndexItem remove_elem(keys, token); @@ -510,7 +512,9 @@ int PinyinArrayIndexLevel::remove_index(/* in */ PinyinKey keys[] buf_end = (PinyinIndexItem *) m_chunk.end(); std_lite::pair *, PinyinIndexItem *> range; - range = std_lite::equal_range(buf_begin, buf_end, remove_elem, m_lessthan); + range = std_lite::equal_range + (buf_begin, buf_end, remove_elem, + phrase_exact_less_than); PinyinIndexItem * cur_elem; for ( cur_elem = range.first; diff --git a/src/storage/pinyin_phrase.h b/src/storage/pinyin_phrase.h index 61c0c6e..b9a8812 100644 --- a/src/storage/pinyin_phrase.h +++ b/src/storage/pinyin_phrase.h @@ -187,7 +187,27 @@ public: } }; + //for find the element in the phrase array +template +inline int phrase_exact_compare(const PinyinIndexItem &lhs, + const PinyinIndexItem &rhs) +{ + PinyinKey * key_lhs = (PinyinKey *) lhs.m_keys; + PinyinKey * key_rhs = (PinyinKey *) rhs.m_keys; + return pinyin_exact_compare(key_lhs, key_rhs, phrase_length); +} + +template +inline bool phrase_exact_less_than(const PinyinIndexItem &lhs, + const PinyinIndexItem &rhs) +{ + return 0 > phrase_exact_compare(lhs, rhs); +} + + +#if 0 + template class PhraseExactCompare : public std_lite::binary_function @@ -219,6 +239,8 @@ class PhraseExactLessThan } }; +#endif + }; #endif -- cgit