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 137 : 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 137 : m_nChildren( 0 )
32 : {
33 137 : }
34 :
35 137 : Node::~Node()
36 : {
37 137 : }
38 :
39 64 : void Node::removeChild(Node*& pChild)
40 : {
41 64 : const sal_Unicode cKey = pChild->m_cKey;
42 :
43 64 : if ( pChild )
44 : {
45 64 : delete pChild;
46 64 : pChild = NULL;
47 64 : --m_nChildren;
48 : }
49 :
50 64 : if ( !isSeparatedlyHandled( cKey ) )
51 : {
52 20 : std::list<Node*>::iterator i = m_lChildren.begin();
53 62 : while ( i != m_lChildren.end() )
54 : {
55 22 : if ( !(*i) )
56 : {
57 20 : i = m_lChildren.erase( i );
58 : }
59 : else
60 : {
61 2 : ++i;
62 : }
63 : }
64 : }
65 64 : }
66 :
67 263 : void Node::insertKey(OUString sKey, const int nProbability)
68 : {
69 263 : if ( !sKey.isEmpty() )
70 : {
71 243 : const sal_Unicode cKey = sKey[0];
72 243 : sKey = sKey.copy( 1 );
73 :
74 243 : Node*& pChild = getChildRef( cKey, true );
75 :
76 243 : if ( !pChild )
77 : {
78 69 : pChild = m_pHead->newNode( this, cKey );
79 69 : ++m_nChildren;
80 : }
81 :
82 243 : pChild->insertKey( sKey, nProbability );
83 : }
84 : else
85 : {
86 20 : m_nKeyProbability += nProbability;
87 20 : if ( m_pParent )
88 : {
89 : // inform parent about change
90 20 : int probability = m_nHighestProbaInSubtree > m_nKeyProbability ? m_nHighestProbaInSubtree : m_nKeyProbability;
91 20 : m_pParent->childHasChanged( this, probability);
92 : }
93 : }
94 263 : }
95 :
96 : // Removes a complete keyword starting from this node of the tree.
97 102 : void Node::removeKey(OUString sKey)
98 : {
99 102 : if ( !sKey.isEmpty() )
100 : {
101 95 : Node*& pChild = getChildRef( sKey[0] );
102 :
103 95 : if ( pChild )
104 : {
105 : // recursive call downwards
106 94 : 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 7 : m_nKeyProbability = 0;
115 : // ... and make sure our parent is updated.
116 7 : m_pParent->childHasChanged( this, m_nHighestProbaInSubtree, this != m_pHead->m_pCurrent );
117 : }
118 102 : }
119 :
120 80 : Node *Node::advanceKey(const sal_Unicode cKey)
121 : {
122 80 : Node*& pChild = getChildRef( cKey, true );
123 :
124 80 : if ( !pChild )
125 : {
126 57 : pChild = m_pHead->newNode( this, cKey );
127 : }
128 :
129 80 : return pChild;
130 : }
131 :
132 289 : void Node::childHasChanged(Node *pChild, const int nProbability, bool bAllowRemoval)
133 : {
134 289 : if ( nProbability > m_nHighestProbaInSubtree )
135 : {
136 173 : m_pSuggest = pChild;
137 173 : m_nHighestProbaInSubtree = nProbability;
138 :
139 173 : if ( m_pParent ) // recursive call upwards
140 : {
141 161 : int probabilityChange = nProbability > m_nKeyProbability ? nProbability : m_nKeyProbability;
142 161 : m_pParent->childHasChanged( this, probabilityChange );
143 : }
144 : }
145 116 : else if ( !nProbability || nProbability < m_nHighestProbaInSubtree )
146 : {
147 110 : bool bNewEvaluationRequired = m_pSuggest == pChild;
148 :
149 110 : if ( !nProbability && bAllowRemoval )
150 : {
151 : // Remove child. Caller needs to make sure we are allowed to.
152 62 : removeChild( getChildRef( pChild->m_cKey ) );
153 : }
154 :
155 110 : 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 104 : reevaluateSuggestion( bNodeProbabilityChanged );
162 :
163 : // If necessary, inform our parent about change via recursive call
164 104 : if ( bNodeProbabilityChanged && m_pParent )
165 : {
166 96 : bAllowRemoval = bAllowRemoval && this != m_pHead->m_pCurrent;
167 96 : int probabilityChange = m_nHighestProbaInSubtree > m_nKeyProbability ? m_nHighestProbaInSubtree : m_nKeyProbability;
168 96 : m_pParent->childHasChanged( this, probabilityChange, bAllowRemoval );
169 : }
170 : }
171 : }
172 289 : }
173 :
174 104 : void Node::reevaluateSuggestion(bool& bNodeProbabilityChanged)
175 : {
176 104 : if ( m_nChildren ) // find child with highest probability
177 : {
178 77 : int nSuggest = 0;
179 77 : Node* pSuggest = NULL;
180 :
181 : // find child with highest probability in array
182 77 : evaluateSeparateStorage( nSuggest, pSuggest );
183 :
184 : // do the same thing within list
185 79 : for ( std::list<Node*>::iterator i = m_lChildren.begin(); i != m_lChildren.end(); ++i )
186 : {
187 2 : if ( (*i)->m_nHighestProbaInSubtree > nSuggest )
188 : {
189 2 : nSuggest = (*i)->m_nHighestProbaInSubtree;
190 2 : pSuggest = (*i);
191 : }
192 2 : 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 77 : bNodeProbabilityChanged = m_nHighestProbaInSubtree != nSuggest;
201 :
202 77 : m_pSuggest = pSuggest;
203 77 : m_nHighestProbaInSubtree = nSuggest;
204 : }
205 : else // there are no children
206 : {
207 27 : m_pSuggest = NULL;
208 27 : m_nHighestProbaInSubtree = 0;
209 :
210 27 : bNodeProbabilityChanged = true;
211 : }
212 104 : }
213 :
214 : Node* Node::our_pNodeNullPointer = NULL;
215 :
216 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|