Kaydet (Commit) 856df6d4 authored tarafından Noel Grandin's avatar Noel Grandin

use std::unique_ptr in TrieNode

Change-Id: I1482f846370e0b8e6f76d46fc5020e2dcb152223
Reviewed-on: https://gerrit.libreoffice.org/43495Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 8c9cfba6
......@@ -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);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment