diff options
author | Tomaž Vajngerl <quikee@gmail.com> | 2014-02-02 15:16:36 +0100 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2014-02-06 09:26:46 +0100 |
commit | 0502a09431602baa9a8280b87b77df9ad04e94bc (patch) | |
tree | 4a5680fbef918c8b75763190bf13fbbb03f9fea0 /editeng | |
parent | 569e5f023ea3dc86988365ac23ceef70b94b177d (diff) |
Remove LookupTree as it is replaced with Trie.
Change-Id: I7611c5307e4d4e925dc3e54c6b3f2d1a47bd9080
Diffstat (limited to 'editeng')
-rw-r--r-- | editeng/Library_editeng.mk | 3 | ||||
-rw-r--r-- | editeng/qa/lookuptree/lookuptree_test.cxx | 185 | ||||
-rw-r--r-- | editeng/source/lookuptree/LatinLookupTree.cxx | 242 | ||||
-rw-r--r-- | editeng/source/lookuptree/LatinTreeNode.cxx | 112 | ||||
-rw-r--r-- | editeng/source/lookuptree/Node.cxx | 216 |
5 files changed, 0 insertions, 758 deletions
diff --git a/editeng/Library_editeng.mk b/editeng/Library_editeng.mk index 21e2fd7a16a3..644bf98fee99 100644 --- a/editeng/Library_editeng.mk +++ b/editeng/Library_editeng.mk @@ -115,9 +115,6 @@ $(eval $(call gb_Library_add_exception_objects,editeng,\ editeng/source/uno/unoviwou \ editeng/source/xml/xmltxtexp \ editeng/source/xml/xmltxtimp \ - editeng/source/lookuptree/LatinLookupTree \ - editeng/source/lookuptree/LatinTreeNode \ - editeng/source/lookuptree/Node \ editeng/source/lookuptree/Trie \ )) diff --git a/editeng/qa/lookuptree/lookuptree_test.cxx b/editeng/qa/lookuptree/lookuptree_test.cxx index 0bea3a5bdf00..e1a35f41d0ef 100644 --- a/editeng/qa/lookuptree/lookuptree_test.cxx +++ b/editeng/qa/lookuptree/lookuptree_test.cxx @@ -21,10 +21,6 @@ #include <cppunit/TestFixture.h> #include <cppunit/extensions/HelperMacros.h> #include <cppunit/plugin/TestPlugIn.h> - -#include <editeng/LookupTree.hxx> -#include <editeng/LatinLookupTree.hxx> - #include <editeng/Trie.hxx> namespace { @@ -32,12 +28,10 @@ namespace { class LookupTreeTest : public CppUnit::TestFixture { public: - void testLookupTree(); void testTrie(); void testTrieGetAllEntries(); CPPUNIT_TEST_SUITE(LookupTreeTest); - CPPUNIT_TEST(testLookupTree); CPPUNIT_TEST(testTrie); CPPUNIT_TEST(testTrieGetAllEntries); CPPUNIT_TEST_SUITE_END(); @@ -45,185 +39,6 @@ public: CPPUNIT_TEST_SUITE_REGISTRATION(LookupTreeTest); -void LookupTreeTest::testLookupTree() -{ - LookupTree* a = new LatinLookupTree( "a" ); - - a->insert( OUString("vorschlagnummer1"), 2 ); - a->insert( OUString("vorschlagnummer12") ); - a->insert( OUString("vorschlagnummer2") ); - - CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer1"), a->suggestAutoCompletion() ); - - a->insert( OUString("vorschlagnummer12") ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer12"), a->suggestAutoCompletion() ); - - a->insert( OUString("vorschlagnummer2") ); - a->insert( OUString("vorschlagnummer2") ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer2"), a->suggestAutoCompletion() ); - - a->insert( OUString("vorschlag"), 15 ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a->suggestAutoCompletion() ); - - a->insert( OUString("vorschlagnummer2"), 16 ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer2"), a->suggestAutoCompletion() ); - - a->remove( OUString("vorschlagnummer2") ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a->suggestAutoCompletion() ); - - a->insert( OUString("vorschlag20"), 20 ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlag20"), a->suggestAutoCompletion() ); - - a->remove( OUString("vorschlag20") ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a->suggestAutoCompletion() ); - - a->insert( OUString("vorschlagn"), 14 ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a->suggestAutoCompletion() ); - - a->remove( OUString("vorschlag") ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlagn"), a->suggestAutoCompletion() ); - - a->remove( OUString("vorschlagn") ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer12"), a->suggestAutoCompletion() ); - - a->insert( OUString("aber"), 1 ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer12"), a->suggestAutoCompletion() ); - - a->advance( 'a' ); - CPPUNIT_ASSERT_EQUAL( OUString("ber"), a->suggestAutoCompletion() ); - - a->goBack(); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer12"), a->suggestAutoCompletion() ); - - a->insert( OUString("vorschlag"), 15 ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a->suggestAutoCompletion() ); - - a->insert( OUString("vorschlag13"), 13 ); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a->suggestAutoCompletion() ); - - a->gotoNode( "vorsch" ); - CPPUNIT_ASSERT_EQUAL( OUString("lag"), a->suggestAutoCompletion() ); - - a->advance( 'l' ); - CPPUNIT_ASSERT_EQUAL( OUString("ag"), a->suggestAutoCompletion() ); - - a->advance( 'a' ); - CPPUNIT_ASSERT_EQUAL( OUString("g13"), a->suggestAutoCompletion() ); - - a->advance( 'g' ); - CPPUNIT_ASSERT_EQUAL( OUString("13"), a->suggestAutoCompletion() ); - - a->advance( '1' ); - CPPUNIT_ASSERT_EQUAL( OUString("3"), a->suggestAutoCompletion() ); - - a->advance( '3' ); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - a->goBack(); - a->advance( 'z' ); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - /*a->gotoNode( "vorschlag13" ); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - a->advance( 'g' ); - a->advance( '1' ); - a->advance( '3' ); - a->remove( "vorschlag13" ); - CPPUNIT_ASSERT_EQUAL( OUString(""), a->suggestAutoCompletion() );*/ - - a->insert( "VeraHatMichL1eb.", 1000000 ); - a->returnToRoot(); - CPPUNIT_ASSERT_EQUAL( OUString("VeraHatMichL1eb."), a->suggestAutoCompletion() ); - - a->remove( "VeraHatMichL1eb." ); - a->gotoNode( "VeraHatMich" ); - CPPUNIT_ASSERT_EQUAL( OUString(""), a->suggestAutoCompletion() ); - - a->returnToRoot(); - CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a->suggestAutoCompletion() ); - - a->gotoNode( "VeraLiebtMich" ); - a->insert( 600 ); - a->returnToRoot(); - CPPUNIT_ASSERT_EQUAL( OUString("VeraLiebtMich"), a->suggestAutoCompletion() ); - - a->insert( "VeraHatMichL1eb.", 1000000 ); - a->returnToRoot(); - CPPUNIT_ASSERT_EQUAL( OUString("VeraHatMichL1eb."), a->suggestAutoCompletion() ); - - a->remove( "VeraHatMichL1eb." ); - a->gotoNode( "VeraHatMich" ); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - a->advance( 'L' ); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - a->insert( "VeraHatMichL1eb.", 1000000 ); - a->returnToRoot(); - a->remove( "VeraHatMichL1eb." ); - a->gotoNode( "VeraHatMich" ); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - a->goBack(); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - a->insert( "VeraHatMichL1eb.", 1000000 ); - a->returnToRoot(); - a->remove( "VeraHatMichL1eb." ); - a->gotoNode( "VeraHatMich" ); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - a->goBack(); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - a->insert( "neu", 2000 ); - a->returnToRoot(); - CPPUNIT_ASSERT_EQUAL( OUString("neu"), a->suggestAutoCompletion() ); - - a->gotoNode( "ne" ); - CPPUNIT_ASSERT_EQUAL( OUString("u"), a->suggestAutoCompletion() ); - - a->advance( 'u' ); - a->advance( 'e' ); - a->advance( 'r' ); - a->insert(); - CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() ); - - a->returnToRoot(); - CPPUNIT_ASSERT_EQUAL( OUString("neu"), a->suggestAutoCompletion() ); - - a->advance( 'n' ); - CPPUNIT_ASSERT_EQUAL( OUString("eu"), a->suggestAutoCompletion() ); - - a->advance( 'e' ); - CPPUNIT_ASSERT_EQUAL( OUString("uer"), a->suggestAutoCompletion() ); - - // Test unicode - OUString aQueryString = OStringToOUString( "H\xC3\xA4llo", RTL_TEXTENCODING_UTF8 ); - a->insert( aQueryString ); - a->returnToRoot(); - a->advance( 'H' ); - - OUString aAutocompletedString = a->suggestAutoCompletion(); - OUString aExpectedString = OStringToOUString( "\xC3\xA4llo", RTL_TEXTENCODING_UTF8 ); - - CPPUNIT_ASSERT_EQUAL( aExpectedString, aAutocompletedString ); - - OString aUtf8String( "\xe3\x81\x82\xe3\x81\x97\xe3\x81\x9f" ); - aQueryString = OStringToOUString( aUtf8String, RTL_TEXTENCODING_UTF8 ); - a->insert( aQueryString ); - - OUString aGotoString = OStringToOUString( "\xe3\x81\x82", RTL_TEXTENCODING_UTF8 ); - a->gotoNode( aGotoString ); - - aAutocompletedString = a->suggestAutoCompletion(); - aExpectedString = OStringToOUString( "\xe3\x81\x97\xe3\x81\x9f", RTL_TEXTENCODING_UTF8 ); - CPPUNIT_ASSERT_EQUAL( aExpectedString, aAutocompletedString ); - - delete a; -} - void LookupTreeTest::testTrie() { editeng::Trie trie; diff --git a/editeng/source/lookuptree/LatinLookupTree.cxx b/editeng/source/lookuptree/LatinLookupTree.cxx deleted file mode 100644 index cdc0a0b376a6..000000000000 --- a/editeng/source/lookuptree/LatinLookupTree.cxx +++ /dev/null @@ -1,242 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#include <editeng/LatinLookupTree.hxx> -#include <editeng/LatinTreeNode.hxx> - -LatinLookupTree::LatinLookupTree(OUString sLanguage) : - LookupTree( sLanguage ) -{ - for ( sal_Unicode i = 0; i < 52; ++i ) - { - m_pLeaves[i] = NULL; - } -} - -LatinLookupTree::~LatinLookupTree() -{ - freeMemory(); -} - -void LatinLookupTree::returnToRoot() -{ - if ( m_pCurrent == m_pHead ) - return; - - // If there is no entry in this node or the tree that sprouts from it. - if ( m_pCurrent && - m_pCurrent->m_pParent && - !m_pCurrent->m_nChildren && - !m_pCurrent->m_nKeyProbability ) - { - m_pCurrent->m_pParent->childHasChanged( m_pCurrent, 0, true ); - } - - m_pCurrent = m_pHead; -} - -void LatinLookupTree::gotoNode(OUString sNode) -{ - returnToRoot(); - - // walk down the tree... - for ( int i = 0; i < sNode.getLength(); i++ ) - { - m_pCurrent = m_pCurrent->advanceKey( sNode[i] ); - } -} - -void LatinLookupTree::advance(const sal_Unicode cKey) -{ - m_pCurrent = m_pCurrent->advanceKey( cKey ); -} - -void LatinLookupTree::goBack() -{ - if ( m_pCurrent->m_pParent ) // if we're not at the root - { - const Node* const pChild = m_pCurrent; - m_pCurrent = m_pCurrent->m_pParent; // set focus to parent - - // if this is an unused tree leaf - if ( !pChild->m_nChildren && !pChild->m_nKeyProbability ) - { - m_pCurrent->removeChild( m_pCurrent->getChildRef( pChild->m_cKey ) ); - } - } -} - -void LatinLookupTree::insert(OUString sKey, const int nProbability) -{ - if ( !sKey.isEmpty() && nProbability > 0 ) - { - insertKey( sKey, nProbability ); - } -} - -void LatinLookupTree::insert(const int nProbability) -{ - if ( m_pCurrent == this ) - return; - - // change probability - int proba = m_pCurrent->m_nKeyProbability += nProbability; - - // inform his parent - m_pCurrent->m_pParent->childHasChanged( m_pCurrent, proba ); -} - -void LatinLookupTree::remove(OUString sKey) -{ - returnToRoot(); - - if ( !sKey.isEmpty() ) - { - removeKey( sKey ); - } -} - -OUString LatinLookupTree::suggestAutoCompletion() const -{ - if ( !m_pCurrent ) - return OUString(); - - Node* pWalker = m_pCurrent; - - int distance = 0, nTargetProbability = 0; - OUString sSuggestion; - - while ( pWalker->m_pSuggest && ( distance < 2 || - // Make sure the suggestion is at least 2 chars long. - nTargetProbability == pWalker->m_nHighestProbaInSubtree ) ) - { - if ( distance < 2 ) - nTargetProbability = pWalker->m_nHighestProbaInSubtree; - - // follow the tree along the suggested route - pWalker = pWalker->m_pSuggest; - ++distance; - sSuggestion += OUString(pWalker->m_cKey); - } - - return sSuggestion; -} - -void LatinLookupTree::clear() -{ - freeMemory(); -} - -bool LatinLookupTree::isSeparatedlyHandled(const sal_Unicode cKey) const -{ - return - ( cKey >= 'a' && cKey <= 'z' ) - || ( cKey >= 'A' && cKey <= 'Z' ); -} - -Node*& LatinLookupTree::getChildRef(const sal_Unicode cKey, bool bCreatePlaceholder) -{ - int pos = -1; - - // determine position in array if possible - if ( cKey >= 'a' && cKey <= 'z' ) - { - pos = cKey - our_nLowerCaseA; - } - else if ( cKey >= 'A' && cKey <= 'Z' ) - { - pos = cKey - our_nUpperCaseA + 26; - } - - if ( pos != -1 ) - { - return m_pLeaves[pos]; - } - else - { - for ( std::list<Node*>::iterator i = m_lChildren.begin(); i != m_lChildren.end(); ++i ) - { - if ( (*i)->m_cKey == cKey ) - { - return *i; - } - } - if ( bCreatePlaceholder ) - { - // Create new entry in case there isn't one. - m_lChildren.push_back( NULL ); - return *(--m_lChildren.end()); - } - else - { - return our_pNodeNullPointer; - } - } -} - -void LatinLookupTree::evaluateSeparateStorage(int& nSuggest, Node*& pSuggest) const -{ - for ( sal_Unicode i = 0; i < 52; ++i ) - { - if ( m_pLeaves[i] ) - { - if ( m_pLeaves[i]->m_nHighestProbaInSubtree > nSuggest ) - { - nSuggest = m_pLeaves[i]->m_nHighestProbaInSubtree; - pSuggest = m_pLeaves[i]; - } - if ( m_pLeaves[i]->m_nKeyProbability > nSuggest ) - { - nSuggest = m_pLeaves[i]->m_nKeyProbability; - pSuggest = m_pLeaves[i]; - } - } - } -} - -void LatinLookupTree::freeMemory() -{ - // remove nodes from array - for ( sal_Unicode i = 0; i < 52; ++i ) - { - if ( m_pLeaves[i] ) - { - m_pLeaves[i]->freeMemory(); - delete m_pLeaves[i]; - m_pLeaves[i] = NULL; - } - } - // clear list - while ( m_lChildren.size() ) - { - Node* pTmp = m_lChildren.front(); - m_lChildren.pop_front(); - delete pTmp; - } -} - -Node* LatinLookupTree::newNode(Node* pParent, const sal_Unicode cKey, const int nProbability) -{ - return new LatinTreeNode( this, pParent, cKey, nProbability ); -} - -const unsigned int LatinLookupTree::our_nLowerCaseA = 97; -const unsigned int LatinLookupTree::our_nUpperCaseA = 65; - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/editeng/source/lookuptree/LatinTreeNode.cxx b/editeng/source/lookuptree/LatinTreeNode.cxx deleted file mode 100644 index a7f10aa5dd5a..000000000000 --- a/editeng/source/lookuptree/LatinTreeNode.cxx +++ /dev/null @@ -1,112 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#include <editeng/LatinTreeNode.hxx> -#include <editeng/LatinLookupTree.hxx> - -LatinTreeNode::LatinTreeNode(TreeHead* pHead, Node* pParent, const sal_Unicode cKey, const int nProbability) : - Node( pHead, pParent, cKey, nProbability ) -{ - for ( sal_Unicode i = 0; i < 26; ++i ) - { - m_pLeaves[i] = NULL; - } -} - -LatinTreeNode::~LatinTreeNode() -{ - freeMemory(); -} - -bool LatinTreeNode::isSeparatedlyHandled(const sal_Unicode cKey) const -{ - return ( cKey >= 'a' && cKey <= 'z' ); -} - -Node*& LatinTreeNode::getChildRef(const sal_Unicode cKey, bool bCreatePlaceholder) -{ - // determine position in array if possible - if ( cKey >= 'a' && cKey <= 'z' ) - { - return m_pLeaves[cKey - LatinLookupTree::our_nLowerCaseA]; - } - else - { - for ( std::list<Node*>::iterator i = m_lChildren.begin(); i != m_lChildren.end(); ++i ) - { - if ( (*i)->m_cKey == cKey ) - { - return *i; - } - } - if ( bCreatePlaceholder ) - { - // Create new entry in case there isn't one. - m_lChildren.push_back( NULL ); - return *(--m_lChildren.end()); - } - else - { - return our_pNodeNullPointer; - } - } -} - -void LatinTreeNode::evaluateSeparateStorage(int& nSuggest, Node*& pSuggest) const -{ - for ( sal_Unicode i = 0; i < 26; ++i ) - { - if ( m_pLeaves[i] ) - { - if ( m_pLeaves[i]->m_nHighestProbaInSubtree > nSuggest ) - { - nSuggest = m_pLeaves[i]->m_nHighestProbaInSubtree; - pSuggest = m_pLeaves[i]; - } - if ( m_pLeaves[i]->m_nKeyProbability > nSuggest ) - { - nSuggest = m_pLeaves[i]->m_nKeyProbability; - pSuggest = m_pLeaves[i]; - } - } - } -} - -void LatinTreeNode::freeMemory() -{ - // remove nodes from array - for ( sal_Unicode i = 0; i < 26; ++i ) - { - if ( m_pLeaves[i] ) - { - m_pLeaves[i]->freeMemory(); - delete m_pLeaves[i]; - m_pLeaves[i] = NULL; - } - } - // clear list - while ( m_lChildren.size() ) - { - Node* pTmp = m_lChildren.front(); - m_lChildren.pop_front(); - delete pTmp; - } -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/editeng/source/lookuptree/Node.cxx b/editeng/source/lookuptree/Node.cxx deleted file mode 100644 index 2492a88e5c08..000000000000 --- a/editeng/source/lookuptree/Node.cxx +++ /dev/null @@ -1,216 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#include <editeng/TreeHead.hxx> -#include <editeng/Node.hxx> - -Node::Node(TreeHead* const pHead, Node* const pParent, - const sal_Unicode cKey, const int nProbability ) : - m_cKey( cKey ), - m_nKeyProbability( nProbability ), - m_nHighestProbaInSubtree( 0 ), - m_pParent( pParent ), - m_pSuggest( NULL ), - m_pHead( pHead ), - m_nChildren( 0 ) -{ -} - -Node::~Node() -{ -} - -void Node::removeChild(Node*& pChild) -{ - const sal_Unicode cKey = pChild->m_cKey; - - if ( pChild ) - { - delete pChild; - pChild = NULL; - --m_nChildren; - } - - if ( !isSeparatedlyHandled( cKey ) ) - { - std::list<Node*>::iterator i = m_lChildren.begin(); - while ( i != m_lChildren.end() ) - { - if ( !(*i) ) - { - i = m_lChildren.erase( i ); - } - else - { - ++i; - } - } - } -} - -void Node::insertKey(OUString sKey, const int nProbability) -{ - if ( !sKey.isEmpty() ) - { - const sal_Unicode cKey = sKey[0]; - sKey = sKey.copy( 1 ); - - Node*& pChild = getChildRef( cKey, true ); - - if ( !pChild ) - { - pChild = m_pHead->newNode( this, cKey ); - ++m_nChildren; - } - - pChild->insertKey( sKey, nProbability ); - } - else - { - m_nKeyProbability += nProbability; - if ( m_pParent ) - { - // inform parent about change - int probability = m_nHighestProbaInSubtree > m_nKeyProbability ? m_nHighestProbaInSubtree : m_nKeyProbability; - m_pParent->childHasChanged( this, probability); - } - } -} - -// Removes a complete keyword starting from this node of the tree. -void Node::removeKey(OUString sKey) -{ - if ( !sKey.isEmpty() ) - { - Node*& pChild = getChildRef( sKey[0] ); - - if ( pChild ) - { - // recursive call downwards - pChild->removeKey( sKey.copy( 1 ) ); - } - // Else: Keyword to be removed couldn't be found within the tree. - // No further changes are going to be made. - } - else // If we are the node to be removed... - { - // ... remove our entry from tree... - m_nKeyProbability = 0; - // ... and make sure our parent is updated. - m_pParent->childHasChanged( this, m_nHighestProbaInSubtree, this != m_pHead->m_pCurrent ); - } -} - -Node *Node::advanceKey(const sal_Unicode cKey) -{ - Node*& pChild = getChildRef( cKey, true ); - - if ( !pChild ) - { - pChild = m_pHead->newNode( this, cKey ); - } - - return pChild; -} - -void Node::childHasChanged(Node *pChild, const int nProbability, bool bAllowRemoval) -{ - if ( nProbability > m_nHighestProbaInSubtree ) - { - m_pSuggest = pChild; - m_nHighestProbaInSubtree = nProbability; - - if ( m_pParent ) // recursive call upwards - { - int probabilityChange = nProbability > m_nKeyProbability ? nProbability : m_nKeyProbability; - m_pParent->childHasChanged( this, probabilityChange ); - } - } - else if ( !nProbability || nProbability < m_nHighestProbaInSubtree ) - { - bool bNewEvaluationRequired = m_pSuggest == pChild; - - if ( !nProbability && bAllowRemoval ) - { - // Remove child. Caller needs to make sure we are allowed to. - removeChild( getChildRef( pChild->m_cKey ) ); - } - - if ( bNewEvaluationRequired ) - { - // This is used to store whether we need to inform our parent about - // the changes within this node. - bool bNodeProbabilityChanged; - - reevaluateSuggestion( bNodeProbabilityChanged ); - - // If necessary, inform our parent about change via recursive call - if ( bNodeProbabilityChanged && m_pParent ) - { - bAllowRemoval = bAllowRemoval && this != m_pHead->m_pCurrent; - int probabilityChange = m_nHighestProbaInSubtree > m_nKeyProbability ? m_nHighestProbaInSubtree : m_nKeyProbability; - m_pParent->childHasChanged( this, probabilityChange, bAllowRemoval ); - } - } - } -} - -void Node::reevaluateSuggestion(bool& bNodeProbabilityChanged) -{ - if ( m_nChildren ) // find child with highest probability - { - int nSuggest = 0; - Node* pSuggest = NULL; - - // find child with highest probability in array - evaluateSeparateStorage( nSuggest, pSuggest ); - - // do the same thing within list - for ( std::list<Node*>::iterator i = m_lChildren.begin(); i != m_lChildren.end(); ++i ) - { - if ( (*i)->m_nHighestProbaInSubtree > nSuggest ) - { - nSuggest = (*i)->m_nHighestProbaInSubtree; - pSuggest = (*i); - } - if ( (*i)->m_nKeyProbability > nSuggest ) - { - nSuggest = (*i)->m_nKeyProbability; - pSuggest = (*i); - } - } - - // determine whether we need to inform our parent - bNodeProbabilityChanged = m_nHighestProbaInSubtree != nSuggest; - - m_pSuggest = pSuggest; - m_nHighestProbaInSubtree = nSuggest; - } - else // there are no children - { - m_pSuggest = NULL; - m_nHighestProbaInSubtree = 0; - - bNodeProbabilityChanged = true; - } -} - -Node* Node::our_pNodeNullPointer = NULL; - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |