diff options
author | Peng Wu <alexepico@gmail.com> | 2011-04-06 14:47:57 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2011-04-06 14:47:57 +0800 |
commit | 74721fab7449e1fb9e655812a067cd0dd3d7c26d (patch) | |
tree | 229b64f326a382a289c147e02ae6c2f84c2f2e3d /src | |
parent | dc42988b0d0378e3f12eea06c0a0de52d6875b4c (diff) | |
download | libpinyin-74721fab7449e1fb9e655812a067cd0dd3d7c26d.tar.gz libpinyin-74721fab7449e1fb9e655812a067cd0dd3d7c26d.tar.xz libpinyin-74721fab7449e1fb9e655812a067cd0dd3d7c26d.zip |
add get/set array item methods
Diffstat (limited to 'src')
-rw-r--r-- | src/storage/flexible_ngram.h | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/src/storage/flexible_ngram.h b/src/storage/flexible_ngram.h index d8fea2d..e393a91 100644 --- a/src/storage/flexible_ngram.h +++ b/src/storage/flexible_ngram.h @@ -110,10 +110,66 @@ public: /* get array item */ bool get_array_item(/* in */ phrase_token_t token, - /* out */ ArrayItem & item); + /* 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)); + return true; + } + } + return false; + } + /* set array item */ bool set_array_item(/* in */ phrase_token_t token, - /* in */ const ArrayItem & item); + /* 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 ){ + cur_item.m_item = item; + return true; + } + } + m_chunk.insert_content(m_chunk.size(), &insert_item, + sizeof(ArrayItemWithToken)); + return true; + } /* get array header */ bool get_array_header(/* out */ ArrayHeader & header){ |