From 856df6d40690500453092419b2c71e91e2c5de25 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Wed, 18 Oct 2017 11:29:15 +0200 Subject: use std::unique_ptr in TrieNode Change-Id: I1482f846370e0b8e6f76d46fc5020e2dcb152223 Reviewed-on: https://gerrit.libreoffice.org/43495 Tested-by: Jenkins Reviewed-by: Noel Grandin --- editeng/source/lookuptree/Trie.cxx | 45 ++++++++++---------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/editeng/source/lookuptree/Trie.cxx b/editeng/source/lookuptree/Trie.cxx index b661d6d81010..0badcd99703c 100644 --- a/editeng/source/lookuptree/Trie.cxx +++ b/editeng/source/lookuptree/Trie.cxx @@ -22,12 +22,10 @@ struct TrieNode final sal_Unicode mCharacter; bool mMarker; - std::vector mChildren; - TrieNode* mLatinArray[LATIN_ARRAY_SIZE]; - + std::vector> mChildren; + std::unique_ptr mLatinArray[LATIN_ARRAY_SIZE]; explicit TrieNode(sal_Unicode aCharacter = '\0'); - ~TrieNode(); void markWord(); TrieNode* findChild(sal_Unicode aCharacter); @@ -41,26 +39,12 @@ TrieNode::TrieNode(sal_Unicode aCharacter) : mCharacter(aCharacter), mMarker(false) { - for (TrieNode* & i : mLatinArray) + for (auto & i : mLatinArray) { i = nullptr; } } -TrieNode::~TrieNode() -{ - vector::iterator iNode; - for(iNode = mChildren.begin(); iNode != mChildren.end(); ++iNode) - { - delete *iNode; - } - - for (TrieNode* i : mLatinArray) - { - delete i; - } -} - void TrieNode::markWord() { mMarker = true; @@ -71,11 +55,11 @@ void TrieNode::addNewChild(TrieNode* pChild) if (pChild->mCharacter >= 'a' && pChild->mCharacter <= 'z') { - mLatinArray[pChild->mCharacter - u'a'] = pChild; + mLatinArray[pChild->mCharacter - u'a'].reset(pChild); } else { - mChildren.push_back(pChild); + mChildren.push_back(std::unique_ptr(pChild)); } } @@ -84,16 +68,13 @@ TrieNode* TrieNode::findChild(sal_Unicode aInputCharacter) if (aInputCharacter >= 'a' && aInputCharacter <= 'z') { - return mLatinArray[aInputCharacter - u'a']; + return mLatinArray[aInputCharacter - u'a'].get(); } - vector::iterator iNode; - - for(iNode = mChildren.begin(); iNode != mChildren.end(); ++iNode) + for(auto const & pCurrent : mChildren) { - TrieNode* pCurrent = *iNode; if ( pCurrent->mCharacter == aInputCharacter ) - return pCurrent; + return pCurrent.get(); } return nullptr; @@ -102,19 +83,17 @@ TrieNode* TrieNode::findChild(sal_Unicode aInputCharacter) void TrieNode::collectSuggestions(const OUString& sPath, vector& rSuggestionList) { // first traverse nodes for alphabet characters - for (TrieNode* pCurrent : mLatinArray) + for (auto const & pCurrent : mLatinArray) { if (pCurrent != nullptr) - collectSuggestionsForCurrentNode(pCurrent, sPath, rSuggestionList); + collectSuggestionsForCurrentNode(pCurrent.get(), sPath, rSuggestionList); } // traverse nodes for other characters - vector::iterator iNode; - for(iNode = mChildren.begin(); iNode != mChildren.end(); ++iNode) + for(auto const & pCurrent : mChildren) { - TrieNode* pCurrent = *iNode; if (pCurrent != nullptr) - collectSuggestionsForCurrentNode(pCurrent, sPath, rSuggestionList); + collectSuggestionsForCurrentNode(pCurrent.get(), sPath, rSuggestionList); } } -- cgit