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