Skip to content

Commit 86324a2

Browse files
author
VirtualRoyalty
committed
fixes
1 parent b34f5dd commit 86324a2

File tree

6 files changed

+55
-18
lines changed

6 files changed

+55
-18
lines changed

algorithm/include/OpenedHashMap.h

Whitespace-only changes.

algorithm/include/OpenedHashNode.h

Whitespace-only changes.

algorithm/include/UniversalHash.h

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <random>
33
#include <math.h>
4+
#include <string>
45

56
template <typename K>
67
class KeyHash{
@@ -22,7 +23,7 @@ class KeyHash<int>{
2223
private:
2324
size_t tableSize;
2425
unsigned int a, b;
25-
unsigned int p = 1721;
26+
unsigned int p = 3571;
2627
public:
2728
KeyHash(size_t tableSize){
2829
this->tableSize = tableSize;
@@ -45,19 +46,45 @@ template <>
4546
class KeyHash<std::vector<int>>{
4647
private:
4748
size_t tableSize;
48-
int a;
49+
unsigned int a, p = 3571;
4950
public:
5051
KeyHash(size_t tableSize){
5152
this->tableSize = tableSize;
5253
std::mt19937 mers(42);
53-
std::uniform_int_distribution<int> a_uid(1, this->tableSize-1);
54+
std::uniform_int_distribution<int> a_uid(1, this->p-1);
5455
this->a = a_uid(mers);
56+
5557
}
5658
unsigned long operator[](const std::vector<int>& key) const
5759
{
5860
unsigned long hkey = 0;
59-
for (int i=0; i<key.size(); i++){
60-
hkey += ((int)pow(this->a, i) * key[i]) % this->tableSize;
61+
for (auto i=0; i<key.size(); i++){
62+
hkey = ((hkey * this->a) + key[i]) % this->p;
63+
}
64+
return hkey % this->tableSize;
65+
}
66+
67+
};
68+
69+
70+
template <>
71+
class KeyHash<std::string>{
72+
private:
73+
size_t tableSize;
74+
unsigned int a, p = 3571;
75+
public:
76+
KeyHash(size_t tableSize){
77+
this->tableSize = tableSize;
78+
std::mt19937 mers(42);
79+
std::uniform_int_distribution<int> a_uid(1, this->p-1);
80+
this->a = a_uid(mers);
81+
82+
}
83+
unsigned long operator[](const std::string key) const
84+
{
85+
unsigned long hkey = 0;
86+
for (auto i=0; i<key.size(); i++){
87+
hkey = ((hkey * this->a) + key[i]) % this->p;
6188
}
6289
return hkey % this->tableSize;
6390
}

algorithm/include/VComparator.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
2-
#include <random>
3-
#include <math.h>
2+
#include <string>
43
#include <iostream>
54

65
template <typename L>
@@ -32,11 +31,23 @@ class VComparator<std::vector<int>>
3231
bool compare(std::vector<int> a, std::vector<int> b){
3332
if (a.size() != b.size())
3433
return false;
35-
for(auto i=0; i<a.size(); i++)
34+
for(auto i=0; i<a.size(); i++){
3635
if (a[i] != b[i])
3736
return false;
37+
}
3838
return true;
3939

4040
}
4141

4242
};
43+
44+
template <>
45+
class VComparator<std::string>
46+
{
47+
public:
48+
VComparator(){};
49+
bool compare(std::vector<int> a, std::vector<int> b){
50+
return a == b;
51+
}
52+
53+
};

algorithm/src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ int main() {
3838
*/
3939

4040
// test integer number hashing
41-
std::cout << "Linked Hashing Numbers:\n";
42-
testNumberLinkedHashMap();
41+
// std::cout << "Linked Hashing Numbers:\n";
42+
// testNumberLinkedHashMap();
4343
//test vector of integer number hashing
4444
std::cout << "Linked Hashing Vectors:\n";
4545
testVectorLinkedHashMap();

algorithm/src/testVector.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
void testVectorLinkedHashMap()
1010
{
11-
std::cout << "\nEnter tableSize: ";
12-
const auto tableSize = 71;// = [](int t){ return std::cin >> t, t; }({});
11+
const size_t tableSize = 100;
1312

1413
std::size_t vecSize, itemSize;
1514
std::cout << "\nEnter vecSize: ";
@@ -23,12 +22,12 @@ void testVectorLinkedHashMap()
2322
// building hashMap
2423
HashMap <std::vector<int>, std::vector<int>, tableSize> hmap;
2524
for(auto const& vec: randVec){
26-
std::cout << "[ ";
27-
for(auto const& value: vec)
28-
{
29-
std::cout << value << " ";
30-
}
31-
std::cout << "]"<<std::endl;
25+
// std::cout << "[ ";
26+
// for(auto const& value: vec)
27+
// {
28+
// std::cout << value << " ";
29+
// }
30+
// std::cout << "]"<<std::endl;
3231
hmap.insert(vec, vec);
3332
}
3433

0 commit comments

Comments
 (0)