Skip to content
Permalink
Browse files

Use custom Hashtable implementation in StrTrie

  • Loading branch information...
bobbymcr committed Jun 3, 2019
1 parent b614fdc commit af55386784cfc5e6f189fd6094b5bd025fafa356
Showing with 18 additions and 15 deletions.
  1. +16 −14 src/Words.Native.Core/StrTrie.cpp
  2. +2 −1 src/Words.Native.Core/StrTrie.h
@@ -4,7 +4,7 @@ using namespace std;
using namespace Words;

StrTrie::StrTrie()
: nodes_(), size_(0)
: nodes_(0.5f), size_(0)
{
}

@@ -65,40 +65,42 @@ void StrTrie::insert(const Str& value)
return;
}

Map::iterator it = nodes_.find(value);
if (it != nodes_.end())
bool b;
if (!nodes_.insert(value, true, &b))
{
if (it->second)
if (!b)
{
return;
++size_;
}

it = nodes_.erase(it);
return;
}

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

Str v = value;
while (v.length() > 1)
{
v = v.chop();
if (nodes_.find(v) != nodes_.cend())
bool prev;
if (!nodes_.insert(v, false, &prev))
{
if (prev)
{
nodes_.insert(v, true);
}

return;
}

nodes_.insert(make_pair(v, false));
}
}

StrTrie::NodeKind StrTrie::find(const Str& value) const
{
const auto& it = nodes_.find(value);
if (it == nodes_.cend())
bool b;
if (!nodes_.get(value, b))
{
return None;
}

return it->second ? Terminal : Prefix;
return b ? Terminal : Prefix;
}
@@ -1,6 +1,7 @@
#pragma once

#include "Str.h"
#include "Hashtable.h"
#include <unordered_map>
#include <istream>

@@ -27,7 +28,7 @@ namespace Words
NodeKind find(const Str& value) const;

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

0 comments on commit af55386

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