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_OUTPUT_H 18 #define LATINIME_DIC_NODE_STATE_OUTPUT_H 19 20 #include <cstring> // for memcpy() 21 #include <stdint.h> 22 23 #include "defines.h" 24 25 namespace latinime { 26 27 class DicNodeStateOutput { 28 public: 29 DicNodeStateOutput() : mOutputtedCodePointCount(0) { 30 init(); 31 } 32 33 virtual ~DicNodeStateOutput() {} 34 35 void init() { 36 mOutputtedCodePointCount = 0; 37 mCodePointsBuf[0] = 0; 38 } 39 40 void init(const DicNodeStateOutput *const stateOutput) { 41 memcpy(mCodePointsBuf, stateOutput->mCodePointsBuf, 42 stateOutput->mOutputtedCodePointCount * sizeof(mCodePointsBuf[0])); 43 mOutputtedCodePointCount = stateOutput->mOutputtedCodePointCount; 44 if (mOutputtedCodePointCount < MAX_WORD_LENGTH) { 45 mCodePointsBuf[mOutputtedCodePointCount] = 0; 46 } 47 } 48 49 void addMergedNodeCodePoints(const uint16_t mergedNodeCodePointCount, 50 const int *const mergedNodeCodePoints) { 51 if (mergedNodeCodePoints) { 52 const int additionalCodePointCount = min(static_cast<int>(mergedNodeCodePointCount), 53 MAX_WORD_LENGTH - mOutputtedCodePointCount); 54 memcpy(&mCodePointsBuf[mOutputtedCodePointCount], mergedNodeCodePoints, 55 additionalCodePointCount * sizeof(mCodePointsBuf[0])); 56 mOutputtedCodePointCount = static_cast<uint16_t>( 57 mOutputtedCodePointCount + mergedNodeCodePointCount); 58 if (mOutputtedCodePointCount < MAX_WORD_LENGTH) { 59 mCodePointsBuf[mOutputtedCodePointCount] = 0; 60 } 61 } 62 } 63 64 // TODO: Remove 65 int getCodePointAt(const int index) const { 66 return mCodePointsBuf[index]; 67 } 68 69 // TODO: Move to private 70 int mCodePointsBuf[MAX_WORD_LENGTH]; 71 72 private: 73 // Caution!!! 74 // Use a default copy constructor and an assign operator because shallow copies are ok 75 // for this class 76 uint16_t mOutputtedCodePointCount; 77 }; 78 } // namespace latinime 79 #endif // LATINIME_DIC_NODE_STATE_OUTPUT_H 80