From 30e5a445254dac571d0b3ba2dc7a48e17ad58ec8 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Sat, 22 Jun 2013 18:54:16 +0200 Subject: Trie lookup tree code review fixes Change-Id: Ib2d2b668f2ec17742a069d63506cdd2d25d10f0d --- editeng/source/lookuptree/Trie.cxx | 61 ++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 22 deletions(-) (limited to 'editeng/source/lookuptree') diff --git a/editeng/source/lookuptree/Trie.cxx b/editeng/source/lookuptree/Trie.cxx index 1b8eeb7c84a5..4fbc19430bdb 100644 --- a/editeng/source/lookuptree/Trie.cxx +++ b/editeng/source/lookuptree/Trie.cxx @@ -16,6 +16,27 @@ using namespace std; /* TrieNode */ +struct TrieNode +{ + static const int LATIN_ARRAY_SIZE = 26; + + sal_Unicode mCharacter; + bool mMarker; + std::vector mChildren; + TrieNode* mLatinArray[LATIN_ARRAY_SIZE]; + + + TrieNode(sal_Unicode aCharacter = '\0'); + virtual ~TrieNode(); + + void markWord(); + TrieNode* findChild(sal_Unicode aCharacter); + TrieNode* traversePath(OUString sPath); + void addNewChild(TrieNode* pChild); + void collectSuggestions(OUString sPath, std::vector& rSuggestionList); + void collectSuggestionsForCurrentNode(TrieNode* pCurrent, OUString sPath, vector& rSuggestionList); +}; + TrieNode::TrieNode(sal_Unicode aCharacter) : mCharacter(aCharacter), mMarker(false) @@ -85,13 +106,7 @@ void TrieNode::collectSuggestions(OUString sPath, vector& rSuggestionL { TrieNode* pCurrent = mLatinArray[i]; if (pCurrent != NULL) - { - OUString aStringPath = sPath + OUString(pCurrent->mCharacter); - if(pCurrent->mMarker) - rSuggestionList.push_back(aStringPath); - // recursivly traverse tree - pCurrent->collectSuggestions(aStringPath, rSuggestionList); - } + collectSuggestionsForCurrentNode(pCurrent, sPath, rSuggestionList); } // traverse nodes for other characters @@ -100,16 +115,21 @@ void TrieNode::collectSuggestions(OUString sPath, vector& rSuggestionL { TrieNode* pCurrent = *iNode; if (pCurrent != NULL) - { - OUString aStringPath = sPath + OUString(pCurrent->mCharacter); - if(pCurrent->mMarker) - rSuggestionList.push_back(aStringPath); - // recursivly traverse tree - pCurrent->collectSuggestions(aStringPath, rSuggestionList); - } + collectSuggestionsForCurrentNode(pCurrent, sPath, rSuggestionList); } } +void TrieNode::collectSuggestionsForCurrentNode(TrieNode* pCurrent, OUString sPath, vector& rSuggestionList) +{ + OUString aStringPath = sPath + OUString(pCurrent->mCharacter); + if(pCurrent->mMarker) + { + rSuggestionList.push_back(aStringPath); + } + // recursivly descend tree + pCurrent->collectSuggestions(aStringPath, rSuggestionList); +} + TrieNode* TrieNode::traversePath(OUString sPath) { TrieNode* pCurrent = this; @@ -127,15 +147,12 @@ TrieNode* TrieNode::traversePath(OUString sPath) /* TRIE */ -Trie::Trie() -{ - mRoot = new TrieNode(); -} +Trie::Trie() : + mRoot(new TrieNode()) +{} Trie::~Trie() -{ - delete mRoot; -} +{} void Trie::insert(OUString sInputString) const { @@ -147,7 +164,7 @@ void Trie::insert(OUString sInputString) const // traverse the input string and modify the tree with new nodes / characters - TrieNode* pCurrent = mRoot; + TrieNode* pCurrent = mRoot.get(); sal_Unicode aCurrentChar; for ( sal_Int32 i = 0; i < sInputString.getLength(); i++ ) -- cgit