1 /*
2 * Copyright (C) 2014, 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 "dictionary/structure/v2/ver2_pt_node_array_reader.h"
18
19 #include "dictionary/structure/pt_common/patricia_trie_reading_utils.h"
20
21 namespace latinime {
22
readPtNodeArrayInfoAndReturnIfValid(const int ptNodeArrayPos,int * const outPtNodeCount,int * const outFirstPtNodePos) const23 bool Ver2PtNodeArrayReader::readPtNodeArrayInfoAndReturnIfValid(const int ptNodeArrayPos,
24 int *const outPtNodeCount, int *const outFirstPtNodePos) const {
25 if (ptNodeArrayPos < 0 || ptNodeArrayPos >= static_cast<int>(mBuffer.size())) {
26 // Reading invalid position because of a bug or a broken dictionary.
27 AKLOGE("Reading PtNode array info from invalid dictionary position: %d, dict size: %zd",
28 ptNodeArrayPos, mBuffer.size());
29 ASSERT(false);
30 return false;
31 }
32 int readingPos = ptNodeArrayPos;
33 const int ptNodeCountInArray = PatriciaTrieReadingUtils::getPtNodeArraySizeAndAdvancePosition(
34 mBuffer.data(), &readingPos);
35 *outPtNodeCount = ptNodeCountInArray;
36 *outFirstPtNodePos = readingPos;
37 return true;
38 }
39
readForwardLinkAndReturnIfValid(const int forwordLinkPos,int * const outNextPtNodeArrayPos) const40 bool Ver2PtNodeArrayReader::readForwardLinkAndReturnIfValid(const int forwordLinkPos,
41 int *const outNextPtNodeArrayPos) const {
42 if (forwordLinkPos < 0 || forwordLinkPos >= static_cast<int>(mBuffer.size())) {
43 // Reading invalid position because of bug or broken dictionary.
44 AKLOGE("Reading forward link from invalid dictionary position: %d, dict size: %zd",
45 forwordLinkPos, mBuffer.size());
46 ASSERT(false);
47 return false;
48 }
49 // Ver2 dicts don't have forward links.
50 *outNextPtNodeArrayPos = NOT_A_DICT_POS;
51 return true;
52 }
53
54 } // namespace latinime
55