Skip to content

Commit 7af54e0

Browse files
author
VirtualRoyalty
committed
linking hashing implemented
1 parent c5ef0c7 commit 7af54e0

File tree

10 files changed

+239
-70
lines changed

10 files changed

+239
-70
lines changed

algorithm/include/HashMap.h

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
#pragma once
22

3+
34
#include "KeyHash.h"
45
#include "HashNode.h"
5-
6+
#include "VComparator.h"
67

78
// Hash map class template
8-
template <typename K, typename V, size_t tableSize, typename F = KeyHash<K, tableSize> >
9+
template <typename K, typename V, size_t tableSize>
910
class HashMap
1011
{
1112
private:
1213
HashMap(const HashMap& other);
1314
const HashMap& operator=(const HashMap& other);
14-
// hash table
1515
HashNode<K, V>* table[tableSize];
16-
F hashFunc;
16+
KeyHash<K> hashFunc;
17+
VComparator<K> comp;
1718

1819
public:
1920
HashMap() :
2021
table(),
21-
hashFunc()
22+
hashFunc(tableSize),
23+
comp()
2224
{
2325
}
2426

@@ -38,30 +40,33 @@ class HashMap
3840
}
3941
}
4042

41-
bool get(const K& key, V& value)
43+
bool search(const K& key, V& value)
4244
{
43-
unsigned long hashValue = hashFunc(key);
45+
unsigned long hashValue = hashFunc[key];
4446
HashNode<K, V>* entry = table[hashValue];
45-
47+
unsigned int ccounter = 0;
4648
while (entry != NULL) {
47-
if (entry->getKey() == key) {
49+
ccounter++;
50+
// std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl;
51+
if (comp.compare(entry->getKey(), key)) {
4852
value = entry->getValue();
53+
std::cout << "Number of tries: "<< ccounter <<std::endl;
4954
return true;
5055
}
5156

5257
entry = entry->getNext();
5358
}
54-
59+
std::cout << "Number of tries: "<< ccounter <<std::endl;
60+
std::cout << "UKNOWN KEY!"<< std::endl;
5561
return false;
5662
}
5763

58-
void put(const K& key, const V& value)
64+
void insert(const K& key, const V& value)
5965
{
60-
unsigned long hashValue = hashFunc(key);
66+
unsigned long hashValue = hashFunc[key];
6167
HashNode<K, V>* prev = NULL;
6268
HashNode<K, V>* entry = table[hashValue];
63-
64-
while (entry != NULL && entry->getKey() != key) {
69+
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
6570
prev = entry;
6671
entry = entry->getNext();
6772
}
@@ -87,11 +92,11 @@ class HashMap
8792

8893
void remove(const K& key)
8994
{
90-
unsigned long hashValue = hashFunc(key);
95+
unsigned long hashValue = hashFunc[key];
9196
HashNode<K, V>* prev = NULL;
9297
HashNode<K, V>* entry = table[hashValue];
9398

94-
while (entry != NULL && entry->getKey() != key) {
99+
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
95100
prev = entry;
96101
entry = entry->getNext();
97102
}
@@ -114,4 +119,4 @@ class HashMap
114119
delete entry;
115120
}
116121
}
117-
};
122+
};

algorithm/include/HashNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ class HashNode
4747
{
4848
_next = next;
4949
}
50-
};
50+
};

algorithm/include/KeyHash.h

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
#pragma once
2+
#include <random>
3+
#include <math.h>
24

3-
template <typename K, size_t tableSize>
4-
struct KeyHash {
5-
unsigned long operator()(const K& key) const
5+
template <typename K>
6+
class KeyHash{
7+
private:
8+
size_t tableSize;
9+
public:
10+
KeyHash(size_t tableSize){
11+
this->tableSize = tableSize;
12+
}
13+
unsigned long operator[](const K& key) const
614
{
7-
return reinterpret_cast<unsigned long>(key) % tableSize;
15+
return reinterpret_cast<unsigned long>(key) % this->tableSize;
16+
}
17+
18+
};
19+
20+
template <>
21+
class KeyHash<int>{
22+
private:
23+
size_t tableSize;
24+
int a, b;
25+
public:
26+
KeyHash(size_t tableSize){
27+
this->tableSize = tableSize;
28+
std::mt19937 mers(42);
29+
std::uniform_int_distribution<int> a_uid(1, this->tableSize-1);
30+
std::uniform_int_distribution<int> b_uid(0, this->tableSize-1);
31+
this->a = a_uid(mers);
32+
this->b = b_uid(mers);
833
}
9-
};
34+
unsigned long operator[](const int& key) const
35+
{
36+
// std::cout << this->a <<" "<< this->b << std::endl;
37+
return (this->a * key + this->b) % this->tableSize;
38+
}
39+
40+
};
41+
42+
43+
template <>
44+
class KeyHash<std::vector<int>>{
45+
private:
46+
size_t tableSize;
47+
int a;
48+
public:
49+
KeyHash(size_t tableSize){
50+
this->tableSize = tableSize;
51+
std::mt19937 mers(42);
52+
std::uniform_int_distribution<int> a_uid(1, this->tableSize-1);
53+
this->a = a_uid(mers);
54+
}
55+
unsigned long operator[](const std::vector<int>& key) const
56+
{
57+
unsigned long hkey = 0;
58+
for (int i=0; i<key.size(); i++){
59+
hkey += ((int)pow(this->a, i) * key[i]) % this->tableSize;
60+
}
61+
return hkey % this->tableSize;
62+
}
63+
64+
};

algorithm/include/VComparator.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
#include <random>
3+
#include <math.h>
4+
#include <iostream>
5+
6+
template <typename L>
7+
class VComparator
8+
{
9+
public:
10+
VComparator(){};
11+
bool compare(L a, L b) const{
12+
return a == b;
13+
}
14+
};
15+
16+
template <>
17+
class VComparator<const int*>
18+
{
19+
public:
20+
VComparator(){};
21+
bool compare(const int* a, const int* b) const{
22+
return a == b;
23+
}
24+
};
25+
26+
27+
template <>
28+
class VComparator<std::vector<int>>
29+
{
30+
public:
31+
VComparator(){};
32+
bool compare(std::vector<int> a, std::vector<int> b){
33+
if (a.size() != b.size())
34+
return false;
35+
for(auto i=0; i<a.size(); i++)
36+
if (a[i] != b[i])
37+
return false;
38+
return true;
39+
40+
}
41+
42+
};

algorithm/include/utils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
void write_csv(std::string filename, std::vector<std::pair<std::string, std::vector<int>>> dataset);
55
std::vector<std::pair<std::string, std::vector<int>>> read_csv(std::string filename);
66

7-
std::vector<int> gen_rand_vec(int N);
7+
std::vector<int> genRandVec(int N);
8+
9+
void testNumberLinkedHashMap();
10+
void testVectorLinkedHashMap();

algorithm/src/generators.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
#include <random>
77
#include <algorithm>
88

9-
std::vector<int> gen_rand_vec(int N){
10-
std::random_device rd;
11-
std::mt19937 mersenne(rd()); // инициализируем Вихрь Мерсенна случайным стартовым числом
129

13-
#include <vector>
1410

11+
int genRandomUid(){
12+
std::random_device rd;
13+
std::mt19937 mersenne(rd()); // инициализируем Вихрь Мерсенна случайным стартовым числом
14+
std::uniform_int_distribution<int> uid(0, 1000);
15+
return uid(mersenne);
16+
}
1517

16-
std::vector<int> rand_vec(N);
17-
std::generate(rand_vec.begin(), rand_vec.end(), mersenne);
18+
std::vector<int> genRandVec(int N){
1819

20+
std::vector<int> randVec(N);
21+
std::generate(randVec.begin(), randVec.end(), genRandomUid);
1922

20-
return rand_vec;
23+
24+
return randVec;
2125
}

algorithm/src/main.cpp

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,25 @@
33
#include <fstream>
44
#include <vector>
55
#include <utility>
6+
#include <random>
67

78
#include "utils.h"
8-
9+
#include "VComparator.h"
910
#include "HashNode.h"
1011
#include "KeyHash.h"
1112
#include "HashMap.h"
1213

13-
const size_t tableSize = 5;
14-
15-
struct MyKeyHash {
16-
unsigned long operator()(const int& key) const
17-
{
18-
return key % tableSize;
19-
}
20-
};
21-
2214

2315
int main() {
2416
// Make three vectors, each of length 100 filled with 1s, 2s, and 3s
25-
std::vector<int> vec1(10, 1);
26-
std::vector<int> vec2(10, 2);
27-
std::vector<int> vec3(10, 3);
28-
29-
// Wrap into a vector
30-
std::vector<std::pair<std::string, std::vector<int>>> vals = { {"First", vec1}, {"Second", vec2}, {"Three", vec3} };
31-
32-
std::string filepath = "../../data/output/example.csv";
17+
// std::vector<int> vec1(10, 1);
18+
// std::vector<int> vec2(10, 2);
19+
// std::vector<int> vec3(10, 3);
20+
//
21+
// // Wrap into a vector
22+
// std::vector<std::pair<std::string, std::vector<int>>> vals = { {"First", vec1}, {"Second", vec2}, {"Three", vec3} };
23+
//
24+
// std::string filepath = "../../data/outinsert/example.csv";
3325
// Write the vector to CSV
3426
/*
3527
write_csv(filepath, vals);
@@ -49,27 +41,12 @@ int main() {
4941
}
5042
*/
5143

52-
//test simple hash map
53-
HashMap <int, std::string, tableSize, MyKeyHash> hmap;
54-
55-
hmap.put(1, "val1");
56-
hmap.put(2, "val2");
57-
hmap.put(6, "val3");
58-
hmap.put(7, "val4");
44+
//test integer number hashing()
45+
// std::cout << "Linked Hashing Numbers:\n";
46+
// testNumberLinkedHashMap();
47+
//test vector of integer number hashing()
48+
std::cout << "Linked Hashing Vectors:\n";
49+
testVectorLinkedHashMap();
5950

60-
std::string value;
61-
hmap.get(6, value);
62-
std::cout << value << std::endl;
63-
bool res = hmap.get(3, value);
64-
if (res)
65-
std::cout << value << std::endl;
66-
/* hmap.remove(3);
67-
res = hmap.get(3, value);
68-
if (res)
69-
cout << value << endl;*/
70-
std::vector<int> rand_vec(20);
71-
rand_vec = gen_rand_vec(20);
72-
for(auto const& value: rand_vec)
73-
std::cout << value << " " << std::endl;
7451
return 0;
7552
}

algorithm/src/testNumber.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <random>
4+
5+
#include "utils.h"
6+
#include "HashMap.h"
7+
8+
9+
void testNumberLinkedHashMap()
10+
{
11+
std::cout << "\nEnter tableSize: ";
12+
const auto tableSize = 71;
13+
14+
std::size_t vecSize;
15+
std::cout << "\nEnter vecSize: ";
16+
std::cin >> vecSize;
17+
std::vector<int> randVec(vecSize);
18+
randVec = genRandVec(vecSize);
19+
20+
// building hasgMap
21+
HashMap <int, int, tableSize> hmap;
22+
for(auto const& value: randVec){
23+
std::cout << value << std::endl;
24+
hmap.insert(value, value);
25+
}
26+
27+
while(true){
28+
int value;
29+
std::cout << "\nEnter key: ";
30+
std::cin >> value;
31+
if (value == -1){
32+
break;
33+
}
34+
hmap.search(value, value);
35+
}
36+
37+
};
File renamed without changes.

0 commit comments

Comments
 (0)