Skip to content

Commit

Permalink
Merge pull request #14 from Cufeed/cuf
Browse files Browse the repository at this point in the history
Errors (Hash)
  • Loading branch information
ermolaevv authored Mar 22, 2024
2 parents c3e7a94 + c067729 commit 3063883
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 5 deletions.
7 changes: 7 additions & 0 deletions table/ChainHashTable/include/ChainHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class ChainHashTable : HashTable<Key, Value> {
/// </summary>
static const size_t M = 11;

struct STableRec {
Key key;
Value value;

STableRec(const Key& key, const Value& value) : key(key), value(value) {}
};

TDatList<typename Table<Key, Value>::template STableRec<Key, Value>> data[M];
public:
ChainHashTable(size_t maxSize = 10000) : HashTable<Key, Value>(maxSize) {}
Expand Down
74 changes: 74 additions & 0 deletions table/ChainHashTable/src/ChainHashTable.hpp
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;
}
26 changes: 23 additions & 3 deletions table/ChainHashTable/test/test_ChainHashTable.cpp
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);
}
36 changes: 36 additions & 0 deletions table/HashTable/src/HashTable.hpp
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;
}
4 changes: 2 additions & 2 deletions table/RepeatMixingTable/include/RepeatMixingTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
template <class Key, class Value>
class RepeatMixingTable : HashTable<Key, Value> {
protected:
typename Table<Key, Value>::template STableRec<Key, Value>* data;
typename Table<Key, Value>::STableRec<Key, Value>* data;
public:
RepeatMixingTable(size_t maxSize = 10000) : HashTable<Key, Value>(maxSize) {
this->data = new typename Table<Key, Value>::template STableRec<Key, Value>[this->maxSize]
this->data = new typename Table<Key, Value>::template STableRec<Key, Value>[this->maxSize];
}
~RepeatMixingTable() { delete[] data; }

Expand Down
76 changes: 76 additions & 0 deletions table/RepeatMixingTable/src/RepeatMixingTable.hpp
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--;
}

13 changes: 13 additions & 0 deletions table/RepeatMixingTable/test/RepeatMixingTable.cpp
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);
}

0 comments on commit 3063883

Please sign in to comment.