diff options
author | Peng Wu <alexepico@gmail.com> | 2011-04-18 15:42:21 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2011-04-18 15:42:21 +0800 |
commit | 469bcc917fdc827b6904c329a3c7c6e8c43aabaf (patch) | |
tree | be3dc01d38e5af35e26b6280d8d36dd997898aa2 /src | |
parent | 643bbc2a749f9a37a915c9daedaf544478711e42 (diff) | |
download | libpinyin-469bcc917fdc827b6904c329a3c7c6e8c43aabaf.tar.gz libpinyin-469bcc917fdc827b6904c329a3c7c6e8c43aabaf.tar.xz libpinyin-469bcc917fdc827b6904c329a3c7c6e8c43aabaf.zip |
add insert/remove array item to flexible bi-gram
Diffstat (limited to 'src')
-rw-r--r-- | src/storage/flexible_ngram.h | 80 | ||||
-rw-r--r-- | src/storage/ngram.cpp | 2 |
2 files changed, 68 insertions, 14 deletions
diff --git a/src/storage/flexible_ngram.h b/src/storage/flexible_ngram.h index 3cce47d..d82bd59 100644 --- a/src/storage/flexible_ngram.h +++ b/src/storage/flexible_ngram.h @@ -108,6 +108,70 @@ public: return true; } + /* insert array item */ + bool insert_array_item(/* in */ phrase_token_t token, + /* in */ const ArrayItem & item){ + ArrayItemWithToken * begin = (ArrayItemWithToken *) + ((const char *)(m_chunk.begin()) + sizeof(ArrayHeader)); + ArrayItemWithToken * end = (ArrayItemWithToken *) + m_chunk.end(); + + ArrayItemWithToken compare_item; + compare_item.m_token = token; + ArrayItemWithToken * cur_item = std_lite::lower_bound + (begin, end, compare_item, token_less_than); + + ArrayItemWithToken insert_item; + insert_item.m_token = token; + insert_item.m_item = item; + + for ( ; cur_item != end; ++cur_item ){ + if ( cur_item->m_token > token ){ + size_t offset = sizeof(ArrayHeader) + + sizeof(ArrayItemWithToken) * (cur_item - begin); + m_chunk.insert_content(offset, &insert_item, + sizeof(ArrayItemWithToken)); + return true; + } + if ( cur_item->m_token == token ){ + return false; + } + } + m_chunk.insert_content(m_chunk.size(), &insert_item, + sizeof(ArrayItemWithToken)); + return true; + } + + bool remove_array_item(/* in */ phrase_token_t token, + /* out */ ArrayItem & item) + { + /* clear retval */ + memset(&item, 0, sizeof(ArrayItem)); + + const ArrayItemWithToken * begin = (const ArrayItemWithToken *) + ((const char *)(m_chunk.begin()) + sizeof(ArrayHeader)); + const ArrayItemWithToken * end = (const ArrayItemWithToken *) + m_chunk.end(); + + ArrayItemWithToken compare_item; + compare_item.m_token = token; + const ArrayItemWithToken * cur_item = std_lite::lower_bound + (begin, end, compare_item, token_less_than); + + for ( ; cur_item != end; ++cur_item){ + if ( cur_item->m_token > token ) + return false; + if ( cur_item->m_token == token ){ + memcpy(&item, &(cur_item->m_item), sizeof(ArrayItem)); + size_t offset = sizeof(ArrayHeader) + + sizeof(ArrayItemWithToken) * (cur_item - begin); + m_chunk.remove_content(offset, sizeof(ArrayItemWithToken)); + return true; + } + } + return false; + } + /* get array item */ bool get_array_item(/* in */ phrase_token_t token, /* out */ ArrayItem & item) @@ -149,26 +213,16 @@ public: ArrayItemWithToken * cur_item = std_lite::lower_bound (begin, end, compare_item, token_less_than); - ArrayItemWithToken insert_item; - insert_item.m_token = token; - insert_item.m_item = item; - for ( ; cur_item != end; ++cur_item ){ if ( cur_item->m_token > token ){ - size_t offset = sizeof(ArrayHeader) + - sizeof(ArrayItemWithToken) * (cur_item - begin); - m_chunk.insert_content(offset, &insert_item, - sizeof(ArrayItemWithToken)); - return true; + return false; } if ( cur_item->m_token == token ){ - cur_item->m_item = item; + memcpy(&(cur_item->m_item), &item, sizeof(ArrayItem)); return true; } } - m_chunk.insert_content(m_chunk.size(), &insert_item, - sizeof(ArrayItemWithToken)); - return true; + return false; } /* get array header */ diff --git a/src/storage/ngram.cpp b/src/storage/ngram.cpp index 664ecb6..e836ee5 100644 --- a/src/storage/ngram.cpp +++ b/src/storage/ngram.cpp @@ -73,7 +73,7 @@ bool SingleGram::prune(){ return true; } -bool token_less_than(const SingleGramItem & lhs,const SingleGramItem & rhs){ +static bool token_less_than(const SingleGramItem & lhs,const SingleGramItem & rhs){ return lhs.m_token < rhs.m_token; } |