Home | History | Annotate | Download | only in pt_common
      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 #ifndef LATINIME_DYNAMIC_PT_GC_EVENT_LISTENERS_H
     18 #define LATINIME_DYNAMIC_PT_GC_EVENT_LISTENERS_H
     19 
     20 #include <vector>
     21 
     22 #include "defines.h"
     23 #include "suggest/policyimpl/dictionary/structure/pt_common/dynamic_pt_reading_helper.h"
     24 #include "suggest/policyimpl/dictionary/structure/pt_common/pt_node_writer.h"
     25 #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
     26 
     27 namespace latinime {
     28 
     29 class PtNodeParams;
     30 
     31 class DynamicPtGcEventListeners {
     32  public:
     33     // Updates all PtNodes that can be reached from the root. Checks if each PtNode is useless or
     34     // not and marks useless PtNodes as deleted. Such deleted PtNodes will be discarded in the GC.
     35     // TODO: Concatenate non-terminal PtNodes.
     36     class TraversePolicyToUpdateUnigramProbabilityAndMarkUselessPtNodesAsDeleted
     37         : public DynamicPtReadingHelper::TraversingEventListener {
     38      public:
     39         TraversePolicyToUpdateUnigramProbabilityAndMarkUselessPtNodesAsDeleted(
     40                 PtNodeWriter *const ptNodeWriter)
     41                 : mPtNodeWriter(ptNodeWriter), mValueStack(), mChildrenValue(0),
     42                   mValidUnigramCount(0) {}
     43 
     44         ~TraversePolicyToUpdateUnigramProbabilityAndMarkUselessPtNodesAsDeleted() {};
     45 
     46         bool onAscend() {
     47             if (mValueStack.empty()) {
     48                 return false;
     49             }
     50             mChildrenValue = mValueStack.back();
     51             mValueStack.pop_back();
     52             return true;
     53         }
     54 
     55         bool onDescend(const int ptNodeArrayPos) {
     56             mValueStack.push_back(0);
     57             mChildrenValue = 0;
     58             return true;
     59         }
     60 
     61         bool onReadingPtNodeArrayTail() { return true; }
     62 
     63         bool onVisitingPtNode(const PtNodeParams *const ptNodeParams);
     64 
     65         int getValidUnigramCount() const {
     66             return mValidUnigramCount;
     67         }
     68 
     69      private:
     70         DISALLOW_IMPLICIT_CONSTRUCTORS(
     71                 TraversePolicyToUpdateUnigramProbabilityAndMarkUselessPtNodesAsDeleted);
     72 
     73         PtNodeWriter *const mPtNodeWriter;
     74         std::vector<int> mValueStack;
     75         int mChildrenValue;
     76         int mValidUnigramCount;
     77     };
     78 
     79     // Updates all bigram entries that are held by valid PtNodes. This removes useless bigram
     80     // entries.
     81     class TraversePolicyToUpdateBigramProbability
     82             : public DynamicPtReadingHelper::TraversingEventListener {
     83      public:
     84         TraversePolicyToUpdateBigramProbability(PtNodeWriter *const ptNodeWriter)
     85                 : mPtNodeWriter(ptNodeWriter), mValidBigramEntryCount(0) {}
     86 
     87         bool onAscend() { return true; }
     88 
     89         bool onDescend(const int ptNodeArrayPos) { return true; }
     90 
     91         bool onReadingPtNodeArrayTail() { return true; }
     92 
     93         bool onVisitingPtNode(const PtNodeParams *const ptNodeParams);
     94 
     95         int getValidBigramEntryCount() const {
     96             return mValidBigramEntryCount;
     97         }
     98 
     99      private:
    100         DISALLOW_IMPLICIT_CONSTRUCTORS(TraversePolicyToUpdateBigramProbability);
    101 
    102         PtNodeWriter *const mPtNodeWriter;
    103         int mValidBigramEntryCount;
    104     };
    105 
    106     class TraversePolicyToPlaceAndWriteValidPtNodesToBuffer
    107             : public DynamicPtReadingHelper::TraversingEventListener {
    108      public:
    109         TraversePolicyToPlaceAndWriteValidPtNodesToBuffer(
    110                 PtNodeWriter *const ptNodeWriter, BufferWithExtendableBuffer *const bufferToWrite,
    111                 PtNodeWriter::DictPositionRelocationMap *const dictPositionRelocationMap)
    112                 : mPtNodeWriter(ptNodeWriter), mBufferToWrite(bufferToWrite),
    113                   mDictPositionRelocationMap(dictPositionRelocationMap), mValidPtNodeCount(0),
    114                   mPtNodeArraySizeFieldPos(NOT_A_DICT_POS) {};
    115 
    116         bool onAscend() { return true; }
    117 
    118         bool onDescend(const int ptNodeArrayPos);
    119 
    120         bool onReadingPtNodeArrayTail();
    121 
    122         bool onVisitingPtNode(const PtNodeParams *const ptNodeParams);
    123 
    124      private:
    125         DISALLOW_IMPLICIT_CONSTRUCTORS(TraversePolicyToPlaceAndWriteValidPtNodesToBuffer);
    126 
    127         PtNodeWriter *const mPtNodeWriter;
    128         BufferWithExtendableBuffer *const mBufferToWrite;
    129         PtNodeWriter::DictPositionRelocationMap *const mDictPositionRelocationMap;
    130         int mValidPtNodeCount;
    131         int mPtNodeArraySizeFieldPos;
    132     };
    133 
    134     class TraversePolicyToUpdateAllPositionFields
    135             : public DynamicPtReadingHelper::TraversingEventListener {
    136      public:
    137         TraversePolicyToUpdateAllPositionFields(PtNodeWriter *const ptNodeWriter,
    138                 const PtNodeWriter::DictPositionRelocationMap *const dictPositionRelocationMap)
    139                 : mPtNodeWriter(ptNodeWriter),
    140                   mDictPositionRelocationMap(dictPositionRelocationMap), mUnigramCount(0),
    141                   mBigramCount(0) {};
    142 
    143         bool onAscend() { return true; }
    144 
    145         bool onDescend(const int ptNodeArrayPos) { return true; }
    146 
    147         bool onReadingPtNodeArrayTail() { return true; }
    148 
    149         bool onVisitingPtNode(const PtNodeParams *const ptNodeParams);
    150 
    151         int getUnigramCount() const {
    152             return mUnigramCount;
    153         }
    154 
    155         int getBigramCount() const {
    156             return mBigramCount;
    157         }
    158 
    159      private:
    160         DISALLOW_IMPLICIT_CONSTRUCTORS(TraversePolicyToUpdateAllPositionFields);
    161 
    162         PtNodeWriter *const mPtNodeWriter;
    163         const PtNodeWriter::DictPositionRelocationMap *const mDictPositionRelocationMap;
    164         int mUnigramCount;
    165         int mBigramCount;
    166     };
    167 
    168  private:
    169     DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPtGcEventListeners);
    170 };
    171 } // namespace latinime
    172 #endif /* LATINIME_DYNAMIC_PT_GC_EVENT_LISTENERS_H */
    173