Skip to content
Permalink
Browse files

StrTrie - AddNodesMultipleTimesLongerFirst

  • Loading branch information...
bobbymcr committed May 26, 2019
1 parent b84a0fd commit 8814a065192f9eada215d946381baeeed73ca51e
Showing with 26 additions and 4 deletions.
  1. +9 −3 src/Words.Native.Core/StrTrie.cpp
  2. +2 −1 src/Words.Native.Core/StrTrie.h
  3. +15 −0 test/Words.Native.Test/StrTrieTest.cpp
@@ -65,13 +65,19 @@ void StrTrie::insert(const Str& value)
return;
}

if (nodes_.find(value) != nodes_.cend())
Map::iterator it = nodes_.find(value);
if (it != nodes_.end())
{
return;
if (it->second)
{
return;
}

it = nodes_.erase(it);
}

++size_;
nodes_.insert(make_pair(value, true));
nodes_.insert(it, make_pair(value, true));

Str v = value;
while (v.length() > 1)
@@ -27,7 +27,8 @@ namespace Words
NodeKind find(const Str& value) const;

private:
std::unordered_map<Str, bool> nodes_;
typedef std::unordered_map<Str, bool> Map;
Map nodes_;
size_t size_;

StrTrie(const StrTrie&) = delete;
@@ -98,6 +98,21 @@ namespace Words
Assert::AreEqual(StrTrie::Terminal, trie.find("ABC"));
}

TEST_METHOD(AddNodesMultipleTimesLongerFirst)
{
StrTrie trie;

trie.insert("ABC");
trie.insert("ABC");
trie.insert("AB");
trie.insert("AB");

Assert::AreEqual(size_t(2), trie.size());
Assert::AreEqual(StrTrie::Prefix, trie.find("A"));
Assert::AreEqual(StrTrie::Terminal, trie.find("AB"));
Assert::AreEqual(StrTrie::Terminal, trie.find("ABC"));
}

TEST_METHOD(AddEmptyNode)
{
StrTrie trie;

0 comments on commit 8814a06

Please sign in to comment.
You can’t perform that action at this time.