Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2012 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 #ifndef LATINIME_DIC_NODE_STATE_PREVWORD_H
     18 #define LATINIME_DIC_NODE_STATE_PREVWORD_H
     19 
     20 #include <cstring> // for memset()
     21 #include <stdint.h>
     22 
     23 #include "defines.h"
     24 #include "suggest/core/dicnode/dic_node_utils.h"
     25 #include "suggest/core/layout/proximity_info_state.h"
     26 
     27 namespace latinime {
     28 
     29 class DicNodeStatePrevWord {
     30  public:
     31     AK_FORCE_INLINE DicNodeStatePrevWord()
     32             : mPrevWordCount(0), mPrevWordLength(0), mPrevWordStart(0), mPrevWordProbability(0),
     33               mPrevWordNodePos(NOT_A_DICT_POS), mSecondWordFirstInputIndex(NOT_AN_INDEX) {
     34         memset(mPrevWord, 0, sizeof(mPrevWord));
     35     }
     36 
     37     virtual ~DicNodeStatePrevWord() {}
     38 
     39     void init() {
     40         mPrevWordLength = 0;
     41         mPrevWordCount = 0;
     42         mPrevWordStart = 0;
     43         mPrevWordProbability = -1;
     44         mPrevWordNodePos = NOT_A_DICT_POS;
     45         mSecondWordFirstInputIndex = NOT_AN_INDEX;
     46     }
     47 
     48     void init(const int prevWordNodePos) {
     49         mPrevWordLength = 0;
     50         mPrevWordCount = 0;
     51         mPrevWordStart = 0;
     52         mPrevWordProbability = -1;
     53         mPrevWordNodePos = prevWordNodePos;
     54         mSecondWordFirstInputIndex = NOT_AN_INDEX;
     55     }
     56 
     57     // Init by copy
     58     AK_FORCE_INLINE void init(const DicNodeStatePrevWord *const prevWord) {
     59         mPrevWordLength = prevWord->mPrevWordLength;
     60         mPrevWordCount = prevWord->mPrevWordCount;
     61         mPrevWordStart = prevWord->mPrevWordStart;
     62         mPrevWordProbability = prevWord->mPrevWordProbability;
     63         mPrevWordNodePos = prevWord->mPrevWordNodePos;
     64         mSecondWordFirstInputIndex = prevWord->mSecondWordFirstInputIndex;
     65         memcpy(mPrevWord, prevWord->mPrevWord, prevWord->mPrevWordLength * sizeof(mPrevWord[0]));
     66     }
     67 
     68     void init(const int16_t prevWordCount, const int16_t prevWordProbability,
     69             const int prevWordNodePos, const int *const src0, const int16_t length0,
     70             const int *const src1, const int16_t length1,
     71             const int prevWordSecondWordFirstInputIndex, const int lastInputIndex) {
     72         mPrevWordCount = min(prevWordCount, static_cast<int16_t>(MAX_RESULTS));
     73         mPrevWordProbability = prevWordProbability;
     74         mPrevWordNodePos = prevWordNodePos;
     75         int twoWordsLen =
     76                 DicNodeUtils::appendTwoWords(src0, length0, src1, length1, mPrevWord);
     77         if (twoWordsLen >= MAX_WORD_LENGTH) {
     78             twoWordsLen = MAX_WORD_LENGTH - 1;
     79         }
     80         mPrevWord[twoWordsLen] = KEYCODE_SPACE;
     81         mPrevWordStart = length0;
     82         mPrevWordLength = static_cast<int16_t>(twoWordsLen + 1);
     83         mSecondWordFirstInputIndex = prevWordSecondWordFirstInputIndex;
     84     }
     85 
     86     void truncate(const int offset) {
     87         // TODO: memmove
     88         if (mPrevWordLength < offset) {
     89             memset(mPrevWord, 0, sizeof(mPrevWord));
     90             mPrevWordLength = 0;
     91             return;
     92         }
     93         const int newPrevWordLength = mPrevWordLength - offset;
     94         memmove(mPrevWord, &mPrevWord[offset], newPrevWordLength * sizeof(mPrevWord[0]));
     95         mPrevWordLength = newPrevWordLength;
     96     }
     97 
     98     void setSecondWordFirstInputIndex(const int inputIndex) {
     99         mSecondWordFirstInputIndex = inputIndex;
    100     }
    101 
    102     int getSecondWordFirstInputIndex() const {
    103         return mSecondWordFirstInputIndex;
    104     }
    105 
    106     // TODO: remove
    107     int16_t getPrevWordLength() const {
    108         return mPrevWordLength;
    109     }
    110 
    111     int16_t getPrevWordCount() const {
    112         return mPrevWordCount;
    113     }
    114 
    115     int16_t getPrevWordStart() const {
    116         return mPrevWordStart;
    117     }
    118 
    119     int getPrevWordNodePos() const {
    120         return mPrevWordNodePos;
    121     }
    122 
    123     int getPrevWordCodePointAt(const int id) const {
    124         return mPrevWord[id];
    125     }
    126 
    127     bool startsWith(const DicNodeStatePrevWord *const prefix, const int prefixLen) const {
    128         if (prefixLen > mPrevWordLength) {
    129             return false;
    130         }
    131         for (int i = 0; i < prefixLen; ++i) {
    132             if (mPrevWord[i] != prefix->mPrevWord[i]) {
    133                 return false;
    134             }
    135         }
    136         return true;
    137     }
    138 
    139     // TODO: Move to private
    140     int mPrevWord[MAX_WORD_LENGTH];
    141 
    142  private:
    143     // Caution!!!
    144     // Use a default copy constructor and an assign operator because shallow copies are ok
    145     // for this class
    146     int16_t mPrevWordCount;
    147     int16_t mPrevWordLength;
    148     int16_t mPrevWordStart;
    149     int16_t mPrevWordProbability;
    150     int mPrevWordNodePos;
    151     int mSecondWordFirstInputIndex;
    152 };
    153 } // namespace latinime
    154 #endif // LATINIME_DIC_NODE_STATE_PREVWORD_H
    155