| OLD | NEW |
| (Empty) | |
| 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 "third_party/prediction/suggest/policyimpl/dictionary/utils/sparse_tabl
e.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 = |
| 36 mIndexTableBuffer->readUint(INDEX_SIZE, indexTableReadingPos); |
| 37 const int contentTableReadingPos = getPosInContentTable(id, index); |
| 38 if (contentTableReadingPos < 0 || |
| 39 contentTableReadingPos >= mContentTableBuffer->getTailPosition()) { |
| 40 AKLOGE("contentTableReadingPos(%d) is invalid. id: %d, index: %d", |
| 41 contentTableReadingPos, id, index); |
| 42 return NOT_A_DICT_POS; |
| 43 } |
| 44 const int contentValue = |
| 45 mContentTableBuffer->readUint(mDataSize, contentTableReadingPos); |
| 46 return contentValue == NOT_EXIST ? NOT_A_DICT_POS : contentValue; |
| 47 } |
| 48 |
| 49 bool SparseTable::set(const int id, const uint32_t value) { |
| 50 const int posInIndexTable = getPosInIndexTable(id); |
| 51 // Extends the index table if needed. |
| 52 int tailPos = mIndexTableBuffer->getTailPosition(); |
| 53 while (tailPos <= posInIndexTable) { |
| 54 if (!mIndexTableBuffer->writeUintAndAdvancePosition(NOT_EXIST, INDEX_SIZE, |
| 55 &tailPos)) { |
| 56 AKLOGE("cannot extend index table. tailPos: %d to: %d", tailPos, |
| 57 posInIndexTable); |
| 58 return false; |
| 59 } |
| 60 } |
| 61 if (contains(id)) { |
| 62 // The entry is already in the content table. |
| 63 const int index = mIndexTableBuffer->readUint(INDEX_SIZE, posInIndexTable); |
| 64 if (!mContentTableBuffer->writeUint(value, mDataSize, |
| 65 getPosInContentTable(id, index))) { |
| 66 AKLOGE("cannot update value %d. pos: %d, tailPos: %d, mDataSize: %d", |
| 67 value, getPosInContentTable(id, index), |
| 68 mContentTableBuffer->getTailPosition(), mDataSize); |
| 69 return false; |
| 70 } |
| 71 return true; |
| 72 } |
| 73 // The entry is not in the content table. |
| 74 // Create new entry in the content table. |
| 75 const int index = |
| 76 getIndexFromContentTablePos(mContentTableBuffer->getTailPosition()); |
| 77 if (!mIndexTableBuffer->writeUint(index, INDEX_SIZE, posInIndexTable)) { |
| 78 AKLOGE("cannot write index %d. pos %d", index, posInIndexTable); |
| 79 return false; |
| 80 } |
| 81 // Write a new block that containing the entry to be set. |
| 82 int writingPos = getPosInContentTable(0 /* id */, index); |
| 83 for (int i = 0; i < mBlockSize; ++i) { |
| 84 if (!mContentTableBuffer->writeUintAndAdvancePosition(NOT_EXIST, mDataSize, |
| 85 &writingPos)) { |
| 86 AKLOGE( |
| 87 "cannot write content table to extend. writingPos: %d, tailPos: %d, " |
| 88 "mDataSize: %d", |
| 89 writingPos, mContentTableBuffer->getTailPosition(), mDataSize); |
| 90 return false; |
| 91 } |
| 92 } |
| 93 return mContentTableBuffer->writeUint(value, mDataSize, |
| 94 getPosInContentTable(id, index)); |
| 95 } |
| 96 |
| 97 int SparseTable::getIndexFromContentTablePos(const int contentTablePos) const { |
| 98 return contentTablePos / mDataSize / mBlockSize; |
| 99 } |
| 100 |
| 101 int SparseTable::getPosInIndexTable(const int id) const { |
| 102 return (id / mBlockSize) * INDEX_SIZE; |
| 103 } |
| 104 |
| 105 int SparseTable::getPosInContentTable(const int id, const int index) const { |
| 106 const int offset = id % mBlockSize; |
| 107 return (index * mBlockSize + offset) * mDataSize; |
| 108 } |
| 109 |
| 110 } // namespace latinime |
| OLD | NEW |