Introduce ExtendableBuffer for dynamic update.
Bug: 6669677 Change-Id: I6343c348334c0dace8b12a71eb992d6f040e8c95main
parent
198b52f6db
commit
941811cbd6
|
@ -71,14 +71,16 @@ LATIN_IME_CORE_SRC_FILES := \
|
||||||
header/header_policy.cpp \
|
header/header_policy.cpp \
|
||||||
header/header_reading_utils.cpp \
|
header/header_reading_utils.cpp \
|
||||||
shortcut/shortcut_list_reading_utils.cpp \
|
shortcut/shortcut_list_reading_utils.cpp \
|
||||||
utils/byte_array_utils.cpp \
|
|
||||||
utils/format_utils.cpp \
|
|
||||||
dictionary_structure_with_buffer_policy_factory.cpp \
|
dictionary_structure_with_buffer_policy_factory.cpp \
|
||||||
dynamic_patricia_trie_node_reader.cpp \
|
dynamic_patricia_trie_node_reader.cpp \
|
||||||
dynamic_patricia_trie_policy.cpp \
|
dynamic_patricia_trie_policy.cpp \
|
||||||
dynamic_patricia_trie_reading_utils.cpp \
|
dynamic_patricia_trie_reading_utils.cpp \
|
||||||
patricia_trie_policy.cpp \
|
patricia_trie_policy.cpp \
|
||||||
patricia_trie_reading_utils.cpp) \
|
patricia_trie_reading_utils.cpp) \
|
||||||
|
$(addprefix suggest/policyimpl/dictionary/utils/, \
|
||||||
|
byte_array_utils.cpp \
|
||||||
|
extendable_buffer.cpp \
|
||||||
|
format_utils.cpp) \
|
||||||
suggest/policyimpl/gesture/gesture_suggest_policy_factory.cpp \
|
suggest/policyimpl/gesture/gesture_suggest_policy_factory.cpp \
|
||||||
$(addprefix suggest/policyimpl/typing/, \
|
$(addprefix suggest/policyimpl/typing/, \
|
||||||
scoring_params.cpp \
|
scoring_params.cpp \
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include "suggest/policyimpl/dictionary/bigram/bigram_list_policy.h"
|
#include "suggest/policyimpl/dictionary/bigram/bigram_list_policy.h"
|
||||||
#include "suggest/policyimpl/dictionary/header/header_policy.h"
|
#include "suggest/policyimpl/dictionary/header/header_policy.h"
|
||||||
#include "suggest/policyimpl/dictionary/shortcut/shortcut_list_policy.h"
|
#include "suggest/policyimpl/dictionary/shortcut/shortcut_list_policy.h"
|
||||||
|
#include "suggest/policyimpl/dictionary/utils/extendable_buffer.h"
|
||||||
#include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h"
|
#include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h"
|
||||||
|
|
||||||
namespace latinime {
|
namespace latinime {
|
||||||
|
@ -34,7 +35,7 @@ class DicNodeVector;
|
||||||
class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
|
class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
|
||||||
public:
|
public:
|
||||||
DynamicPatriciaTriePolicy(const MmappedBuffer *const buffer)
|
DynamicPatriciaTriePolicy(const MmappedBuffer *const buffer)
|
||||||
: mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer()),
|
: mBuffer(buffer), mExtendableBuffer(), mHeaderPolicy(mBuffer->getBuffer()),
|
||||||
mDictRoot(mBuffer->getBuffer() + mHeaderPolicy.getSize()),
|
mDictRoot(mBuffer->getBuffer() + mHeaderPolicy.getSize()),
|
||||||
mBigramListPolicy(mDictRoot), mShortcutListPolicy(mDictRoot) {}
|
mBigramListPolicy(mDictRoot), mShortcutListPolicy(mDictRoot) {}
|
||||||
|
|
||||||
|
@ -87,8 +88,10 @@ class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
|
||||||
static const int MAX_CHILD_COUNT_TO_AVOID_INFINITE_LOOP;
|
static const int MAX_CHILD_COUNT_TO_AVOID_INFINITE_LOOP;
|
||||||
|
|
||||||
const MmappedBuffer *const mBuffer;
|
const MmappedBuffer *const mBuffer;
|
||||||
|
const ExtendableBuffer mExtendableBuffer;
|
||||||
const HeaderPolicy mHeaderPolicy;
|
const HeaderPolicy mHeaderPolicy;
|
||||||
// TODO: Consolidate mDictRoot.
|
// TODO: Consolidate mDictRoot.
|
||||||
|
// CAVEAT!: Be careful about array out of bound access with mDictRoot
|
||||||
const uint8_t *const mDictRoot;
|
const uint8_t *const mDictRoot;
|
||||||
const BigramListPolicy mBigramListPolicy;
|
const BigramListPolicy mBigramListPolicy;
|
||||||
const ShortcutListPolicy mShortcutListPolicy;
|
const ShortcutListPolicy mShortcutListPolicy;
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2013 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "suggest/policyimpl/dictionary/utils/extendable_buffer.h"
|
||||||
|
|
||||||
|
namespace latinime {
|
||||||
|
|
||||||
|
const size_t ExtendableBuffer::INITIAL_BUFFER_SIZE = 16 * 1024;
|
||||||
|
const size_t ExtendableBuffer::MAX_BUFFER_SIZE = 1024 * 1024;
|
||||||
|
const size_t ExtendableBuffer::EXTEND_BUFFER_SIZE_STEP = 16 * 1024;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2013, The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LATINIME_EXTENDABLE_BUFFER_H
|
||||||
|
#define LATINIME_EXTENDABLE_BUFFER_H
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "defines.h"
|
||||||
|
|
||||||
|
namespace latinime {
|
||||||
|
|
||||||
|
// This is used as a buffer that can be extended for updatable dictionaries.
|
||||||
|
class ExtendableBuffer {
|
||||||
|
public:
|
||||||
|
ExtendableBuffer() : mBuffer(INITIAL_BUFFER_SIZE), mUsedSize(0) {}
|
||||||
|
|
||||||
|
AK_FORCE_INLINE uint8_t *getBuffer() {
|
||||||
|
return &mBuffer[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return if the buffer is successfully extended or not.
|
||||||
|
AK_FORCE_INLINE bool extendBuffer() {
|
||||||
|
if (mBuffer.size() + EXTEND_BUFFER_SIZE_STEP > MAX_BUFFER_SIZE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
mBuffer.resize(mBuffer.size() + EXTEND_BUFFER_SIZE_STEP);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
AK_FORCE_INLINE int getAllocatedSize() const {
|
||||||
|
return mBuffer.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
AK_FORCE_INLINE int getUsedSize() const {
|
||||||
|
return mUsedSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
AK_FORCE_INLINE void clear() {
|
||||||
|
mUsedSize = 0;
|
||||||
|
mBuffer.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(ExtendableBuffer);
|
||||||
|
|
||||||
|
static const size_t INITIAL_BUFFER_SIZE;
|
||||||
|
static const size_t MAX_BUFFER_SIZE;
|
||||||
|
static const size_t EXTEND_BUFFER_SIZE_STEP;
|
||||||
|
|
||||||
|
std::vector<uint8_t> mBuffer;
|
||||||
|
int mUsedSize;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif /* LATINIME_MMAPED_BUFFER_H */
|
Loading…
Reference in New Issue