Home | History | Annotate | Download | only in v402
      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 /*
     18  * !!!!! DO NOT EDIT THIS FILE !!!!!
     19  *
     20  * This file was generated from
     21  *   dictionary/structure/v4/ver4_patricia_trie_writing_helper.h
     22  */
     23 
     24 #ifndef LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_WRITING_HELPER_H
     25 #define LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_WRITING_HELPER_H
     26 
     27 #include "defines.h"
     28 #include "dictionary/structure/pt_common/dynamic_pt_gc_event_listeners.h"
     29 #include "dictionary/structure/backward/v402/content/terminal_position_lookup_table.h"
     30 #include "dictionary/utils/entry_counters.h"
     31 
     32 namespace latinime {
     33 namespace backward {
     34 namespace v402 {
     35 
     36 } // namespace v402
     37 } // namespace backward
     38 class HeaderPolicy;
     39 namespace backward {
     40 namespace v402 {
     41 class Ver4DictBuffers;
     42 class Ver4PatriciaTrieNodeReader;
     43 class Ver4PatriciaTrieNodeWriter;
     44 
     45 class Ver4PatriciaTrieWritingHelper {
     46  public:
     47     Ver4PatriciaTrieWritingHelper(Ver4DictBuffers *const buffers)
     48             : mBuffers(buffers) {}
     49 
     50     bool writeToDictFile(const char *const dictDirPath, const EntryCounts &entryCounts) const;
     51 
     52     // This method cannot be const because the original dictionary buffer will be updated to detect
     53     // useless PtNodes during GC.
     54     bool writeToDictFileWithGC(const int rootPtNodeArrayPos, const char *const dictDirPath);
     55 
     56  private:
     57     DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4PatriciaTrieWritingHelper);
     58 
     59     class TraversePolicyToUpdateAllPtNodeFlagsAndTerminalIds
     60             : public DynamicPtReadingHelper::TraversingEventListener {
     61      public:
     62         TraversePolicyToUpdateAllPtNodeFlagsAndTerminalIds(
     63                 Ver4PatriciaTrieNodeWriter *const ptNodeWriter,
     64                 const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap)
     65                 : mPtNodeWriter(ptNodeWriter), mTerminalIdMap(terminalIdMap) {}
     66 
     67         bool onAscend() { return true; }
     68 
     69         bool onDescend(const int ptNodeArrayPos) { return true; }
     70 
     71         bool onReadingPtNodeArrayTail() { return true; }
     72 
     73         bool onVisitingPtNode(const PtNodeParams *const ptNodeParams);
     74 
     75      private:
     76         DISALLOW_IMPLICIT_CONSTRUCTORS(TraversePolicyToUpdateAllPtNodeFlagsAndTerminalIds);
     77 
     78         Ver4PatriciaTrieNodeWriter *const mPtNodeWriter;
     79         const TerminalPositionLookupTable::TerminalIdMap *const mTerminalIdMap;
     80     };
     81 
     82     // For truncateUnigrams() and truncateBigrams().
     83     class DictProbability {
     84      public:
     85         DictProbability(const int dictPos, const int probability, const int timestamp)
     86                 : mDictPos(dictPos), mProbability(probability), mTimestamp(timestamp) {}
     87 
     88         int getDictPos() const {
     89             return mDictPos;
     90         }
     91 
     92         int getProbability() const {
     93             return mProbability;
     94         }
     95 
     96         int getTimestamp() const {
     97             return mTimestamp;
     98         }
     99 
    100      private:
    101         DISALLOW_DEFAULT_CONSTRUCTOR(DictProbability);
    102 
    103         int mDictPos;
    104         int mProbability;
    105         int mTimestamp;
    106     };
    107 
    108     // For truncateUnigrams() and truncateBigrams().
    109     class DictProbabilityComparator {
    110      public:
    111         bool operator()(const DictProbability &left, const DictProbability &right) {
    112             if (left.getProbability() != right.getProbability()) {
    113                 return left.getProbability() > right.getProbability();
    114             }
    115             if (left.getTimestamp() != right.getTimestamp()) {
    116                 return left.getTimestamp() < right.getTimestamp();
    117             }
    118             return left.getDictPos() > right.getDictPos();
    119         }
    120 
    121      private:
    122         DISALLOW_ASSIGNMENT_OPERATOR(DictProbabilityComparator);
    123     };
    124 
    125     bool runGC(const int rootPtNodeArrayPos, const HeaderPolicy *const headerPolicy,
    126             Ver4DictBuffers *const buffersToWrite, int *const outUnigramCount,
    127             int *const outBigramCount);
    128 
    129     bool truncateUnigrams(const Ver4PatriciaTrieNodeReader *const ptNodeReader,
    130             Ver4PatriciaTrieNodeWriter *const ptNodeWriter, const int maxUnigramCount);
    131 
    132     bool truncateBigrams(const int maxBigramCount);
    133 
    134     Ver4DictBuffers *const mBuffers;
    135 };
    136 } // namespace v402
    137 } // namespace backward
    138 } // namespace latinime
    139 
    140 #endif /* LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_WRITING_HELPER_H */
    141