Home | History | Annotate | Download | only in dicnode
      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 #include <list>
     18 
     19 #include "defines.h"
     20 #include "suggest/core/dicnode/dic_node_priority_queue.h"
     21 #include "suggest/core/dicnode/dic_node_utils.h"
     22 #include "suggest/core/dicnode/dic_nodes_cache.h"
     23 
     24 namespace latinime {
     25 
     26 // The biggest value among MAX_CACHE_DIC_NODE_SIZE, MAX_CACHE_DIC_NODE_SIZE_FOR_SINGLE_POINT, ...
     27 const int DicNodesCache::LARGE_PRIORITY_QUEUE_CAPACITY = 310;
     28 // Capacity for reducing memory footprint.
     29 const int DicNodesCache::SMALL_PRIORITY_QUEUE_CAPACITY = 100;
     30 
     31 /**
     32  * Truncates all of the dicNodes so that they start at the given commit point.
     33  * Only called for multi-word typing input.
     34  */
     35 DicNode *DicNodesCache::setCommitPoint(int commitPoint) {
     36     std::list<DicNode> dicNodesList;
     37     while (mCachedDicNodesForContinuousSuggestion->getSize() > 0) {
     38         DicNode dicNode;
     39         mCachedDicNodesForContinuousSuggestion->copyPop(&dicNode);
     40         dicNodesList.push_front(dicNode);
     41     }
     42 
     43     // Get the starting words of the top scoring dicNode (last dicNode popped from priority queue)
     44     // up to the commit point. These words have already been committed to the text view.
     45     DicNode *topDicNode = &dicNodesList.front();
     46     DicNode topDicNodeCopy;
     47     DicNodeUtils::initByCopy(topDicNode, &topDicNodeCopy);
     48 
     49     // Keep only those dicNodes that match the same starting words.
     50     std::list<DicNode>::iterator iter;
     51     for (iter = dicNodesList.begin(); iter != dicNodesList.end(); iter++) {
     52         DicNode *dicNode = &*iter;
     53         if (dicNode->truncateNode(&topDicNodeCopy, commitPoint)) {
     54             mCachedDicNodesForContinuousSuggestion->copyPush(dicNode);
     55         } else {
     56             // Top dicNode should be reprocessed.
     57             ASSERT(dicNode != topDicNode);
     58             DicNode::managedDelete(dicNode);
     59         }
     60     }
     61     mInputIndex -= commitPoint;
     62     return topDicNode;
     63 }
     64 }  // namespace latinime
     65