[Libreoffice-commits] core.git: editeng/source

Noel Grandin noel.grandin at collabora.co.uk
Thu Oct 19 07:05:32 UTC 2017


 editeng/source/lookuptree/Trie.cxx |   45 +++++++++----------------------------
 1 file changed, 12 insertions(+), 33 deletions(-)

New commits:
commit 856df6d40690500453092419b2c71e91e2c5de25
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Wed Oct 18 11:29:15 2017 +0200

    use std::unique_ptr in TrieNode
    
    Change-Id: I1482f846370e0b8e6f76d46fc5020e2dcb152223
    Reviewed-on: https://gerrit.libreoffice.org/43495
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

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<TrieNode*>  mChildren;
-    TrieNode*               mLatinArray[LATIN_ARRAY_SIZE];
-
+    std::vector<std::unique_ptr<TrieNode>>  mChildren;
+    std::unique_ptr<TrieNode> 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<TrieNode*>::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<TrieNode>(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<TrieNode*>::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<OUString>& 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<TrieNode*>::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);
     }
 }
 


More information about the Libreoffice-commits mailing list