Skip to content

Commit

Permalink
Hashtable - IncreasingTableSizeKeyNotFoundLowerLoadFactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Jun 1, 2019
1 parent 8d0b602 commit dbb3fda
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
21 changes: 14 additions & 7 deletions src/Words.Benchmark.Native/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Stopwatch.h"
#include "Hashtable.h"
#include <string>
#include <sstream>

using namespace std;
using namespace Words;
Expand Down Expand Up @@ -58,9 +59,9 @@ void MakeKey(char* k, int v)
}
}

void Insert(int n)
void Insert(int n, float loadFactor)
{
Hashtable<string, int> table;
Hashtable<string, int> table(loadFactor);
char raw[5];
raw[4] = '\0';
for (int i = 1; i <= n; ++i)
Expand All @@ -73,10 +74,16 @@ void Insert(int n)

int main()
{
Measure("Insert_10", []() { Insert(10); });
Measure("Insert_100", []() { Insert(100); });
Measure("Insert_1000", []() { Insert(1000); });
Measure("Insert_10000", []() { Insert(10000); });
Measure("Insert_100000", []() { Insert(100000); });
for (int i = 1; i <= 4; ++i)
{
float f = i / 4.0f;
for (int n = 10; n <= 100000; n *= 10)
{
stringstream s;
s << "Insert_" << n << "_" << f;
Measure(s.str().c_str(), [n, f]() { Insert(n, f); });
}
}

return 0;
}
36 changes: 24 additions & 12 deletions src/Words.Native.Core/Hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@

#include <algorithm>
#include <vector>
#include <stdexcept>

namespace
{
static const int Sizes[] =
static int nextSize(int current, float loadFactor)
{
5, 11, 23, 53, 113, 251, 509, 1019, 2039, 4079, 8179, 16369, 32749, 65521, 131063, 262133, 524269,
static const int sizes[] =
{
3, 5, 11, 23, 53, 113, 251, 509, 1019, 2039, 4079, 8179, 16369, 32749, 65521, 131063, 262133,524269,
1048571, 2097143, 4194287, 8388587, 16777183, 33554393, 67108837, 134217689, 268435399, 536870879,
1073741789, 2147483647
};
};

for (int next : sizes)
{
if (static_cast<int>(loadFactor * next) > current)
{
return next;
}
}

throw std::bad_alloc();
}
}

namespace Words
Expand All @@ -34,10 +48,11 @@ namespace Words
};

public:
Hashtable()
Hashtable(float loadFactor = 1.0f)
: eq_(),
hash_(),
buckets_(3),
buckets_(nextSize(0, loadFactor)),
loadFactor_(loadFactor),
size_(0)
{
}
Expand Down Expand Up @@ -84,6 +99,7 @@ namespace Words
TEq eq_;
THash hash_;
std::vector<Entry> buckets_;
float loadFactor_;
int size_;

size_t idx(const TKey& key) const
Expand Down Expand Up @@ -124,7 +140,8 @@ namespace Words
return e;
}

if (size_ == buckets_.size())
int maxSize = static_cast<int>(loadFactor_ * buckets_.size());
if (size_ >= maxSize)
{
resize();
index = idx(key);
Expand All @@ -142,12 +159,7 @@ namespace Words

void resize()
{
int i = -1;
while (Sizes[++i] <= buckets_.size())
{
}

std::vector<Entry> original(Sizes[i]);
std::vector<Entry> original(nextSize(static_cast<int>(buckets_.size()), loadFactor_));
std::swap(buckets_, original);
size_ = 0;
for (const Entry& e : original)
Expand Down
11 changes: 11 additions & 0 deletions test/Words.Native.Test/HashtableTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,18 @@ namespace Words
TEST_METHOD(IncreasingTableSizeKeyNotFound)
{
Hashtable<MyKey, int> table;
TestIncreasingTableSizeKeyNotFound(table);
}

TEST_METHOD(IncreasingTableSizeKeyNotFoundLowerLoadFactor)
{
Hashtable<MyKey, int> table(0.25f);
TestIncreasingTableSizeKeyNotFound(table);
}

private:
void TestIncreasingTableSizeKeyNotFound(Hashtable<MyKey, int>& table)
{
for (int i = 0; i < 1000; ++i)
{
bool inserted = table.insert({ i }, i + 1);
Expand Down

0 comments on commit dbb3fda

Please sign in to comment.