1 /* 2 * Copyright (C) 2013, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "suggest/policyimpl/dictionary/utils/sparse_table.h" 18 19 namespace latinime { 20 21 const int SparseTable::NOT_EXIST = -1; 22 const int SparseTable::INDEX_SIZE = 4; 23 24 bool SparseTable::contains(const int id) const { 25 const int readingPos = getPosInIndexTable(id); 26 if (id < 0 || mIndexTableBuffer->getTailPosition() <= readingPos) { 27 return false; 28 } 29 const int index = mIndexTableBuffer->readUint(INDEX_SIZE, readingPos); 30 return index != NOT_EXIST; 31 } 32 33 uint32_t SparseTable::get(const int id) const { 34 const int indexTableReadingPos = getPosInIndexTable(id); 35 const int index = mIndexTableBuffer->readUint(INDEX_SIZE, indexTableReadingPos); 36 const int contentTableReadingPos = getPosInContentTable(id, index); 37 if (contentTableReadingPos < 0 38 || contentTableReadingPos >= mContentTableBuffer->getTailPosition()) { 39 AKLOGE("contentTableReadingPos(%d) is invalid. id: %d, index: %d", 40 contentTableReadingPos, id, index); 41 return NOT_A_DICT_POS; 42 } 43 const int contentValue = mContentTableBuffer->readUint(mDataSize, contentTableReadingPos); 44 return contentValue == NOT_EXIST ? NOT_A_DICT_POS : contentValue; 45 } 46 47 bool SparseTable::set(const int id, const uint32_t value) { 48 const int posInIndexTable = getPosInIndexTable(id); 49 // Extends the index table if needed. 50 int tailPos = mIndexTableBuffer->getTailPosition(); 51 while (tailPos <= posInIndexTable) { 52 if (!mIndexTableBuffer->writeUintAndAdvancePosition(NOT_EXIST, INDEX_SIZE, &tailPos)) { 53 AKLOGE("cannot extend index table. tailPos: %d to: %d", tailPos, posInIndexTable); 54 return false; 55 } 56 } 57 if (contains(id)) { 58 // The entry is already in the content table. 59 const int index = mIndexTableBuffer->readUint(INDEX_SIZE, posInIndexTable); 60 if (!mContentTableBuffer->writeUint(value, mDataSize, getPosInContentTable(id, index))) { 61 AKLOGE("cannot update value %d. pos: %d, tailPos: %d, mDataSize: %d", value, 62 getPosInContentTable(id, index), mContentTableBuffer->getTailPosition(), 63 mDataSize); 64 return false; 65 } 66 return true; 67 } 68 // The entry is not in the content table. 69 // Create new entry in the content table. 70 const int index = getIndexFromContentTablePos(mContentTableBuffer->getTailPosition()); 71 if (!mIndexTableBuffer->writeUint(index, INDEX_SIZE, posInIndexTable)) { 72 AKLOGE("cannot write index %d. pos %d", index, posInIndexTable); 73 return false; 74 } 75 // Write a new block that containing the entry to be set. 76 int writingPos = getPosInContentTable(0 /* id */, index); 77 for (int i = 0; i < mBlockSize; ++i) { 78 if (!mContentTableBuffer->writeUintAndAdvancePosition(NOT_EXIST, mDataSize, 79 &writingPos)) { 80 AKLOGE("cannot write content table to extend. writingPos: %d, tailPos: %d, " 81 "mDataSize: %d", writingPos, mContentTableBuffer->getTailPosition(), mDataSize); 82 return false; 83 } 84 } 85 return mContentTableBuffer->writeUint(value, mDataSize, getPosInContentTable(id, index)); 86 } 87 88 int SparseTable::getIndexFromContentTablePos(const int contentTablePos) const { 89 return contentTablePos / mDataSize / mBlockSize; 90 } 91 92 int SparseTable::getPosInIndexTable(const int id) const { 93 return (id / mBlockSize) * INDEX_SIZE; 94 } 95 96 int SparseTable::getPosInContentTable(const int id, const int index) const { 97 const int offset = id % mBlockSize; 98 return (index * mBlockSize + offset) * mDataSize; 99 } 100 101 } // namespace latinime 102