LCOV - code coverage report
Current view: top level - libreoffice/editeng/source/lookuptree - Node.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 80 82 97.6 %
Date: 2012-12-17 Functions: 8 9 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <editeng/TreeHead.hxx>
      21             : #include <editeng/Node.hxx>
      22             : 
      23         294 : Node::Node(TreeHead* const pHead, Node* const pParent,
      24             :             const sal_Unicode cKey, const int nProbability ) :
      25             :     m_cKey( cKey ),
      26             :     m_nKeyProbability( nProbability ),
      27             :     m_nHighestProbaInSubtree( 0 ),
      28             :     m_pParent( pParent ),
      29             :     m_pSuggest( NULL ),
      30             :     m_pHead( pHead ),
      31         294 :     m_nChildren( 0 )
      32             : {
      33         294 : }
      34             : 
      35         294 : Node::~Node()
      36             : {
      37         294 : }
      38             : 
      39         128 : void Node::removeChild(Node*& pChild)
      40             : {
      41         128 :     const sal_Unicode cKey = pChild->m_cKey;
      42             : 
      43         128 :     if ( pChild )
      44             :     {
      45         128 :         delete pChild;
      46         128 :         pChild = NULL;
      47         128 :         --m_nChildren;
      48             :     }
      49             : 
      50         128 :     if ( !isSeparatedlyHandled( cKey ) )
      51             :     {
      52          40 :         std::list<Node*>::iterator i = m_lChildren.begin();
      53         124 :         while ( i != m_lChildren.end() )
      54             :         {
      55          44 :             if ( !(*i) )
      56             :             {
      57          40 :                 i = m_lChildren.erase( i );
      58             :             }
      59             :             else
      60             :             {
      61           4 :                 ++i;
      62             :             }
      63             :         }
      64             :     }
      65         128 : }
      66             : 
      67         575 : void Node::insertKey(OUString sKey, const int nProbability)
      68             : {
      69         575 :     if ( !sKey.isEmpty() )
      70             :     {
      71         532 :         const sal_Unicode cKey = sKey[0];
      72         532 :         sKey = sKey.copy( 1 );
      73             : 
      74         532 :         Node*& pChild = getChildRef( cKey, true );
      75             : 
      76         532 :         if ( !pChild )
      77             :         {
      78         154 :             pChild = m_pHead->newNode( this, cKey );
      79         154 :             ++m_nChildren;
      80             :         }
      81             : 
      82         532 :         pChild->insertKey( sKey, nProbability );
      83             :     }
      84             :     else
      85             :     {
      86          43 :         m_nKeyProbability += nProbability;
      87          43 :         if ( m_pParent )
      88             :         {
      89             :             // inform parent about change
      90          43 :             int probability = m_nHighestProbaInSubtree > m_nKeyProbability ? m_nHighestProbaInSubtree : m_nKeyProbability;
      91          43 :             m_pParent->childHasChanged( this, probability);
      92             :         }
      93             :     }
      94         575 : }
      95             : 
      96             : // Removes a complete keyword starting from this node of the tree.
      97         204 : void Node::removeKey(OUString sKey)
      98             : {
      99         204 :     if ( !sKey.isEmpty() )
     100             :     {
     101         190 :         Node*& pChild = getChildRef( sKey[0] );
     102             : 
     103         190 :         if ( pChild )
     104             :         {
     105             :             // recursive call downwards
     106         188 :             pChild->removeKey( sKey.copy( 1 ) );
     107             :         }
     108             :         // Else: Keyword to be removed couldn't be found within the tree.
     109             :         // No further changes are going to be made.
     110             :     }
     111             :     else    // If we are the node to be removed...
     112             :     {
     113             :         // ... remove our entry from tree...
     114          14 :         m_nKeyProbability = 0;
     115             :         // ... and make sure our parent is updated.
     116          14 :         m_pParent->childHasChanged( this, m_nHighestProbaInSubtree, this != m_pHead->m_pCurrent );
     117             :     }
     118         204 : }
     119             : 
     120         160 : Node *Node::advanceKey(const sal_Unicode cKey)
     121             : {
     122         160 :     Node*& pChild = getChildRef( cKey, true );
     123             : 
     124         160 :     if ( !pChild )
     125             :     {
     126         114 :         pChild = m_pHead->newNode( this, cKey );
     127             :     }
     128             : 
     129         160 :     return pChild;
     130             : }
     131             : 
     132         610 : void Node::childHasChanged(Node *pChild, const int nProbability, bool bAllowRemoval)
     133             : {
     134         610 :     if ( nProbability > m_nHighestProbaInSubtree )
     135             :     {
     136         377 :         m_pSuggest = pChild;
     137         377 :         m_nHighestProbaInSubtree = nProbability;
     138             : 
     139         377 :         if ( m_pParent ) // recursive call upwards
     140             :         {
     141         351 :             int probabilityChange = nProbability > m_nKeyProbability ? nProbability : m_nKeyProbability;
     142         351 :             m_pParent->childHasChanged( this, probabilityChange );
     143             :         }
     144             :     }
     145         233 :     else if ( !nProbability || nProbability < m_nHighestProbaInSubtree )
     146             :     {
     147         220 :         bool bNewEvaluationRequired = m_pSuggest == pChild;
     148             : 
     149         220 :         if ( !nProbability && bAllowRemoval )
     150             :         {
     151             :             // Remove child. Caller needs to make sure we are allowed to.
     152         124 :             removeChild( getChildRef( pChild->m_cKey ) );
     153             :         }
     154             : 
     155         220 :         if ( bNewEvaluationRequired )
     156             :         {
     157             :             // This is used to store whether we need to inform our parent about
     158             :             // the changes within this node.
     159             :             bool bNodeProbabilityChanged;
     160             : 
     161         208 :             reevaluateSuggestion( bNodeProbabilityChanged );
     162             : 
     163             :             // If necessary, inform our parent about change via recursive call
     164         208 :             if ( bNodeProbabilityChanged && m_pParent )
     165             :             {
     166         192 :                 bAllowRemoval = bAllowRemoval && this != m_pHead->m_pCurrent;
     167         192 :                 int probabilityChange = m_nHighestProbaInSubtree > m_nKeyProbability ? m_nHighestProbaInSubtree : m_nKeyProbability;
     168         192 :                 m_pParent->childHasChanged( this, probabilityChange, bAllowRemoval );
     169             :             }
     170             :         }
     171             :     }
     172         610 : }
     173             : 
     174         208 : void Node::reevaluateSuggestion(bool& bNodeProbabilityChanged)
     175             : {
     176         208 :     if ( m_nChildren ) // find child with highest probability
     177             :     {
     178         154 :         int nSuggest = 0;
     179         154 :         Node* pSuggest = NULL;
     180             : 
     181             :         // find child with highest probability in array
     182         154 :         evaluateSeparateStorage( nSuggest, pSuggest );
     183             : 
     184             :         // do the same thing within list
     185         158 :         for ( std::list<Node*>::iterator i = m_lChildren.begin(); i != m_lChildren.end(); ++i )
     186             :         {
     187           4 :             if ( (*i)->m_nHighestProbaInSubtree > nSuggest )
     188             :             {
     189           4 :                 nSuggest = (*i)->m_nHighestProbaInSubtree;
     190           4 :                 pSuggest = (*i);
     191             :             }
     192           4 :             if ( (*i)->m_nKeyProbability > nSuggest )
     193             :             {
     194           0 :                 nSuggest = (*i)->m_nKeyProbability;
     195           0 :                 pSuggest = (*i);
     196             :             }
     197             :         }
     198             : 
     199             :         // determine whether we need to inform our parent
     200         154 :         bNodeProbabilityChanged = m_nHighestProbaInSubtree != nSuggest;
     201             : 
     202         154 :         m_pSuggest = pSuggest;
     203         154 :         m_nHighestProbaInSubtree = nSuggest;
     204             :     }
     205             :     else // there are no children
     206             :     {
     207          54 :         m_pSuggest = NULL;
     208          54 :         m_nHighestProbaInSubtree = 0;
     209             : 
     210          54 :         bNodeProbabilityChanged = true;
     211             :     }
     212         208 : }
     213             : 
     214             : Node* Node::our_pNodeNullPointer = NULL;
     215             : 
     216             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10