Merge "Introduce ExtendableBuffer for dynamic update."
commit
5f438d555c
|
@ -71,14 +71,16 @@ LATIN_IME_CORE_SRC_FILES := \
|
|||
header/header_policy.cpp \
|
||||
header/header_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 \
|
||||
dynamic_patricia_trie_node_reader.cpp \
|
||||
dynamic_patricia_trie_policy.cpp \
|
||||
dynamic_patricia_trie_reading_utils.cpp \
|
||||
patricia_trie_policy.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 \
|
||||
$(addprefix suggest/policyimpl/typing/, \
|
||||
scoring_params.cpp \
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "suggest/policyimpl/dictionary/bigram/bigram_list_policy.h"
|
||||
#include "suggest/policyimpl/dictionary/header/header_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"
|
||||
|
||||
namespace latinime {
|
||||
|
@ -34,7 +35,7 @@ class DicNodeVector;
|
|||
class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
|
||||
public:
|
||||
DynamicPatriciaTriePolicy(const MmappedBuffer *const buffer)
|
||||
: mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer()),
|
||||
: mBuffer(buffer), mExtendableBuffer(), mHeaderPolicy(mBuffer->getBuffer()),
|
||||
mDictRoot(mBuffer->getBuffer() + mHeaderPolicy.getSize()),
|
||||
mBigramListPolicy(mDictRoot), mShortcutListPolicy(mDictRoot) {}
|
||||
|
||||
|
@ -87,8 +88,10 @@ class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
|
|||
static const int MAX_CHILD_COUNT_TO_AVOID_INFINITE_LOOP;
|
||||
|
||||
const MmappedBuffer *const mBuffer;
|
||||
const ExtendableBuffer mExtendableBuffer;
|
||||
const HeaderPolicy mHeaderPolicy;
|
||||
// TODO: Consolidate mDictRoot.
|
||||
// CAVEAT!: Be careful about array out of bound access with mDictRoot
|
||||
const uint8_t *const mDictRoot;
|
||||
const BigramListPolicy mBigramListPolicy;
|
||||
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