-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Cufeed/cuf
Errors (Hash)
- Loading branch information
Showing
7 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "ChainHashTable.h" | ||
template <class Key, class Value> | ||
Value* ChainHashTable<Key, Value>::Find(Key key) { | ||
size_t index = this->GetHash(key, M); | ||
auto& list = this->data[index]; | ||
for (auto& rec : list) { | ||
if (rec.key == key) { | ||
return &rec.value; | ||
} | ||
} | ||
throw std::runtime_error("Key not found"); | ||
} | ||
|
||
template <class Key, class Value> | ||
void ChainHashTable<Key, Value>::Insert(Key key, Value value) { | ||
if (this->length == M) { | ||
TDatList<STableRec>* newData = new TDatList<STableRec>[2 * M]; | ||
for (size_t i = 0; i < M; ++i) { | ||
for (auto& rec : this->data[i]) { | ||
size_t newIndex = this->GetHash(rec.key, 2 * M); | ||
newData[newIndex].push_back(rec); | ||
} | ||
} | ||
delete[] this->data; | ||
this->data = newData; | ||
M *= 2; | ||
} | ||
|
||
size_t index = this->GetHash(key, M); | ||
auto& list = this->data[index]; | ||
for (auto& rec : list) { | ||
if (rec.key == key) { | ||
rec.value = value; | ||
return; | ||
} | ||
} | ||
list.push_back({ key, value }); | ||
this->length++; | ||
} | ||
|
||
template <class Key, class Value> | ||
void ChainHashTable<Key, Value>::Delete(Key key) { | ||
size_t index = this->GetHash(key, M); | ||
auto& list = this->data[index]; | ||
for (auto it = list.begin(); it != list.end(); ++it) { | ||
if (it->key == key) { | ||
list.erase(it); | ||
this->length--; | ||
return; | ||
} | ||
} | ||
throw std::runtime_error("Key not found"); | ||
} | ||
|
||
template <class Key, class Value> | ||
size_t ChainHashTable<Key, Value>::Reset(void) noexcept { | ||
this->position = 0; | ||
this->actriveRec = nullptr; | ||
return 0; | ||
} | ||
|
||
template <class Key, class Value> | ||
size_t ChainHashTable<Key, Value>::GoNext(void) noexcept { | ||
while (this->position < M && this->data[this->position].empty()) { | ||
this->position++; | ||
} | ||
if (this->position < M) { | ||
this->actriveRec = &this->data[this->position].front(); | ||
this->position++; | ||
return this->position - 1; | ||
} | ||
this->actriveRec = nullptr; | ||
return M; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
#include <gtest/gtest.h> | ||
#include "ChainHashTable.h" | ||
using namespace std; | ||
#include <gtest/gtest.h> | ||
#include "ChainHashTable.h" | ||
using namespace std; | ||
|
||
TEST(ChainHashTable, InsertAndFind) { | ||
ChainHashTable<int, string> hashTable; | ||
int key = 1; | ||
string value = "test"; | ||
hashTable.Insert(key, value); | ||
ASSERT_EQ(*hashTable.Find(key), value); | ||
} | ||
|
||
TEST(ChainHashTable, Delete) { | ||
ChainHashTable<int, string> hashTable; | ||
hashTable.Insert(5, "test5"); | ||
hashTable.Delete(5); | ||
ASSERT_THROW(hashTable.Find(5), runtime_error); | ||
} | ||
|
||
TEST(ChainHashTable, FindNonExistentKey) { | ||
ChainHashTable<int, string> hashTable; | ||
ASSERT_THROW(hashTable.Find(100), runtime_error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "HashTable.h" | ||
template <class Key, class Value> | ||
bool HashTable<Key, Value>::IsEmpty() const noexcept | ||
{ | ||
return this->length == 0; | ||
} | ||
|
||
template <class Key, class Value> | ||
bool HashTable<Key, Value>::IsFull() const noexcept | ||
{ | ||
return this->length == this->maxSize; | ||
} | ||
|
||
template <class Key, class Value> | ||
bool HashTable<Key, Value>::IsTabEnded(void) const noexcept | ||
{ | ||
return this->position >= length; | ||
} | ||
|
||
template <class Key, class Value> | ||
Key HashTable<Key, Value>::GetKey(void) const | ||
{ | ||
if (this->actriveRec == nullptr) { | ||
throw std::runtime_error("No active record"); | ||
} | ||
return this->actriveRec->key; | ||
} | ||
|
||
template <class Key, class Value> | ||
Value* HashTable<Key, Value>::GetValuePtr(void) const | ||
{ | ||
if (this->actriveRec == nullptr) { | ||
throw std::runtime_error("No active record"); | ||
} | ||
return &this->actriveRec->value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "RepeatMixingTable.h" | ||
template <class Key, class Value> | ||
Value* RepeatMixingTable<Key, Value>::Find(Key key) { | ||
size_t ind = GetHash(key, this->maxSize); | ||
|
||
while (data[ind].key != 0 && data[ind].key != key) { | ||
ind = (ind + 1) % this->maxSize; | ||
} | ||
|
||
if (data[ind].key == 0) { | ||
throw std::runtime_error("Key not found"); | ||
} | ||
|
||
return &this->data[ind].value; | ||
} | ||
|
||
|
||
|
||
template <class Key, class Value> | ||
void RepeatMixingTable<Key, Value>::Insert(Key key, Value value) { | ||
if (this->length == this->maxSize) { | ||
throw std::runtime_error("Table is full"); | ||
} | ||
|
||
size_t num = -1; | ||
size_t ind = GetHash(key, this->maxSize); | ||
|
||
while (data[ind].key != 0 && data[ind].key != key) { | ||
ind = (ind + 1) % this->maxSize; | ||
} | ||
|
||
if (data[ind].key == 0) { | ||
num = ind; | ||
} | ||
|
||
if (num != -1) { | ||
this->data[num] = Table<Key, Value>::STableRec<Key, Value>(key, value); | ||
this->length++; | ||
} | ||
else { | ||
this->data[ind].value = value; | ||
} | ||
} | ||
|
||
|
||
|
||
template <class Key, class Value> | ||
size_t RepeatMixingTable<Key, Value>::Reset(void) noexcept { | ||
this->position = 0; | ||
return 0; | ||
} | ||
|
||
template <class Key, class Value> | ||
size_t RepeatMixingTable<Key, Value>::GoNext(void) noexcept { | ||
do { | ||
this->position = (this->position + 1) % this->maxSize; | ||
} while (data[this->position].key == 0); | ||
return this->position; | ||
} | ||
|
||
template <class Key, class Value> | ||
void RepeatMixingTable<Key, Value>::Delete(Key key) { | ||
size_t ind = GetHash(key, this->maxSize); | ||
|
||
while (data[ind].key != 0 && data[ind].key != key) { | ||
ind = (ind + 1) % this->maxSize; | ||
} | ||
|
||
if (data[ind].key == 0) { | ||
throw std::runtime_error("Key not found"); | ||
} | ||
|
||
this->data[ind].key = 0; | ||
this->length--; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
#include <gtest/gtest.h> | ||
#include "RepeatMixingTable.h" | ||
using namespace std; | ||
|
||
TEST(RepeatMixingTable, InsertAndFind) { | ||
RepeatMixingTable<int, string> table; | ||
table.Insert(1, "one"); | ||
ASSERT_EQ(*table.Find(1), "one"); | ||
} | ||
|
||
TEST(RepeatMixingTable, Delete) { | ||
RepeatMixingTable<int, string> table; | ||
table.Insert(2, "two"); | ||
table.Delete(2); | ||
ASSERT_THROW(table.Find(2), std::runtime_error); | ||
} |