Keisuke Kuroyanagi 99808b7b0d Implement ver4 dictionary bigram writing methods.
Bug: 11073222
Change-Id: Ibb0e697fe14dac1edb1f3074fb59e2cdc91039d4
2013-11-12 21:10:01 +09:00

87 lines
3.3 KiB
C++

/*
* 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/sparse_table.h"
namespace latinime {
const int SparseTable::NOT_EXIST = -1;
const int SparseTable::INDEX_SIZE = 4;
bool SparseTable::contains(const int id) const {
const int readingPos = getPosInIndexTable(id);
if (id < 0 || mIndexTableBuffer->getTailPosition() <= readingPos) {
return false;
}
const int index = mIndexTableBuffer->readUint(INDEX_SIZE, readingPos);
return index != NOT_EXIST;
}
uint32_t SparseTable::get(const int id) const {
const int indexTableReadingPos = getPosInIndexTable(id);
const int index = mIndexTableBuffer->readUint(INDEX_SIZE, indexTableReadingPos);
const int contentTableReadingPos = getPosInContentTable(id, index);
return mContentTableBuffer->readUint(mDataSize, contentTableReadingPos);
}
bool SparseTable::set(const int id, const uint32_t value) {
const int posInIndexTable = getPosInIndexTable(id);
// Extends the index table if needed.
if (mIndexTableBuffer->getTailPosition() < posInIndexTable) {
int tailPos = mIndexTableBuffer->getTailPosition();
while(tailPos < posInIndexTable) {
if (!mIndexTableBuffer->writeUintAndAdvancePosition(NOT_EXIST, INDEX_SIZE, &tailPos)) {
return false;
}
}
}
if (contains(id)) {
// The entry is already in the content table.
const int index = mIndexTableBuffer->readUint(INDEX_SIZE, posInIndexTable);
return mContentTableBuffer->writeUint(value, mDataSize, getPosInContentTable(id, index));
}
// The entry is not in the content table.
// Create new entry in the content table.
const int index = getIndexFromContentTablePos(mContentTableBuffer->getTailPosition());
if (!mIndexTableBuffer->writeUint(index, INDEX_SIZE, posInIndexTable)) {
return false;
}
// Write a new block that containing the entry to be set.
int writingPos = getPosInContentTable(0 /* id */, index);
for (int i = 0; i < mBlockSize; ++i) {
if (!mContentTableBuffer->writeUintAndAdvancePosition(NOT_A_DICT_POS, mDataSize,
&writingPos)) {
return false;
}
}
return mContentTableBuffer->writeUint(value, mDataSize, getPosInContentTable(id, index));
}
int SparseTable::getIndexFromContentTablePos(const int contentTablePos) const {
return contentTablePos / mDataSize / mBlockSize;
}
int SparseTable::getPosInIndexTable(const int id) const {
return (id / mBlockSize) * INDEX_SIZE;
}
int SparseTable::getPosInContentTable(const int id, const int index) const {
const int offset = id % mBlockSize;
return (index * mDataSize + offset) * mBlockSize;
}
} // namespace latinime