1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <editeng/LatinLookupTree.hxx>
21 #include <editeng/LatinTreeNode.hxx>
23 LatinLookupTree::LatinLookupTree(OUString sLanguage
) :
24 LookupTree( sLanguage
)
26 for ( sal_Unicode i
= 0; i
< 52; ++i
)
32 LatinLookupTree::~LatinLookupTree()
37 void LatinLookupTree::returnToRoot()
39 if ( m_pCurrent
== m_pHead
)
42 // If there is no entry in this node or the tree that sprouts from it.
44 m_pCurrent
->m_pParent
&&
45 !m_pCurrent
->m_nChildren
&&
46 !m_pCurrent
->m_nKeyProbability
)
48 m_pCurrent
->m_pParent
->childHasChanged( m_pCurrent
, 0, true );
54 void LatinLookupTree::gotoNode(OUString sNode
)
58 // walk down the tree...
59 for ( int i
= 0; i
< sNode
.getLength(); i
++ )
61 m_pCurrent
= m_pCurrent
->advanceKey( sNode
[i
] );
65 void LatinLookupTree::advance(const sal_Unicode cKey
)
67 m_pCurrent
= m_pCurrent
->advanceKey( cKey
);
70 void LatinLookupTree::goBack()
72 if ( m_pCurrent
->m_pParent
) // if we're not at the root
74 const Node
* const pChild
= m_pCurrent
;
75 m_pCurrent
= m_pCurrent
->m_pParent
; // set focus to parent
77 // if this is an unused tree leaf
78 if ( !pChild
->m_nChildren
&& !pChild
->m_nKeyProbability
)
80 m_pCurrent
->removeChild( m_pCurrent
->getChildRef( pChild
->m_cKey
) );
85 void LatinLookupTree::insert(OUString sKey
, const int nProbability
)
87 if ( !sKey
.isEmpty() && nProbability
> 0 )
89 insertKey( sKey
, nProbability
);
93 void LatinLookupTree::insert(const int nProbability
)
95 if ( m_pCurrent
== this )
99 int proba
= m_pCurrent
->m_nKeyProbability
+= nProbability
;
102 m_pCurrent
->m_pParent
->childHasChanged( m_pCurrent
, proba
);
105 void LatinLookupTree::remove(OUString sKey
)
109 if ( !sKey
.isEmpty() )
115 OUString
LatinLookupTree::suggestAutoCompletion() const
120 Node
* pWalker
= m_pCurrent
;
122 int distance
= 0, nTargetProbability
= 0;
123 OUString sSuggestion
;
125 while ( pWalker
->m_pSuggest
&& ( distance
< 2 ||
126 // Make sure the suggestion is at least 2 chars long.
127 nTargetProbability
== pWalker
->m_nHighestProbaInSubtree
) )
130 nTargetProbability
= pWalker
->m_nHighestProbaInSubtree
;
132 // follow the tree along the suggested route
133 pWalker
= pWalker
->m_pSuggest
;
135 sSuggestion
+= OUString(pWalker
->m_cKey
);
141 void LatinLookupTree::clear()
146 bool LatinLookupTree::isSeparatedlyHandled(const sal_Unicode cKey
) const
149 ( cKey
>= sal_Unicode('a') && cKey
<= sal_Unicode('z') )
150 || ( cKey
>= sal_Unicode('A') && cKey
<= sal_Unicode('Z') );
153 Node
*& LatinLookupTree::getChildRef(const sal_Unicode cKey
, bool bCreatePlaceholder
)
157 // determine position in array if possible
158 if ( cKey
>= sal_Unicode('a') && cKey
<= sal_Unicode('z') )
160 pos
= cKey
- our_nLowerCaseA
;
162 else if ( cKey
>= sal_Unicode('A') && cKey
<= sal_Unicode('Z') )
164 pos
= cKey
- our_nUpperCaseA
+ 26;
169 return m_pLeaves
[pos
];
173 for ( std::list
<Node
*>::iterator i
= m_lChildren
.begin(); i
!= m_lChildren
.end(); ++i
)
175 if ( (*i
)->m_cKey
== cKey
)
180 if ( bCreatePlaceholder
)
182 // Create new entry in case there isn't one.
183 m_lChildren
.push_back( NULL
);
184 return *(--m_lChildren
.end());
188 return our_pNodeNullPointer
;
193 void LatinLookupTree::evaluateSeparateStorage(int& nSuggest
, Node
*& pSuggest
) const
195 for ( sal_Unicode i
= 0; i
< 52; ++i
)
199 if ( m_pLeaves
[i
]->m_nHighestProbaInSubtree
> nSuggest
)
201 nSuggest
= m_pLeaves
[i
]->m_nHighestProbaInSubtree
;
202 pSuggest
= m_pLeaves
[i
];
204 if ( m_pLeaves
[i
]->m_nKeyProbability
> nSuggest
)
206 nSuggest
= m_pLeaves
[i
]->m_nKeyProbability
;
207 pSuggest
= m_pLeaves
[i
];
213 void LatinLookupTree::freeMemory()
215 // remove nodes from array
216 for ( sal_Unicode i
= 0; i
< 52; ++i
)
220 m_pLeaves
[i
]->freeMemory();
226 while ( m_lChildren
.size() )
228 Node
* pTmp
= m_lChildren
.front();
229 m_lChildren
.pop_front();
234 Node
* LatinLookupTree::newNode(Node
* pParent
, const sal_Unicode cKey
, const int nProbability
)
236 return new LatinTreeNode( this, pParent
, cKey
, nProbability
);
239 const unsigned int LatinLookupTree::our_nLowerCaseA
= 97;
240 const unsigned int LatinLookupTree::our_nUpperCaseA
= 65;
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */