-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashSet.h
86 lines (81 loc) · 2.63 KB
/
HashSet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// Created by Oluwaseun Suberu on 11/22/20.
//
#include <iostream>
#include "AVLTree.h"
using namespace std;
#ifndef SEARCHENGINETEMPLATES_HASHSET_H
#define SEARCHENGINETEMPLATES_HASHSET_H
template <typename t>
class HashSet{
private:
AVLTree<t>* table;
int size;
int amountOfBuckets;
public:
HashSet(){
amountOfBuckets = 10;
table = new AVLTree<t>[amountOfBuckets];
size = 0;
}
HashSet(int size){
amountOfBuckets = size;
table = new AVLTree<t>[amountOfBuckets];
this->size = 0;
}
//default insert function, checks for the variable type
void insert(const t& value){
long long int hash = 0;
// if (strcmp(typeid(value).name(), "NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE") ==0) {
hash = compute_hash(value);
// }
// else {
// std::hash<t> hasher;
// hash = hasher(value);
// }
int index = hashFunction(hash);
table[index].insert(value);
this->size++;
}
//stringer hasher
long long int compute_hash(const t& s) { //found on the internet (https://cp-algorithms.com/string/string-hashing.html)
const int p = 31;
const int m = 1e9+9;
long long int hash_value = 0;
long long p_pow = 1;
for (int i = 0; i < s.length(); i++) {
char c = s.at(i);
if (isalpha(c) && c != ' ') {
hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
}
p_pow = (p_pow*p) % m;
}
return hash_value;
}
//outputs index
int hashFunction(long long int& x) {
return (x % amountOfBuckets);
}
//returns number of elements
int getSize(){
return this->size;
}
//checks if author exists
bool contains(const t& value){
long long int hash = 0;
// if (strcmp(typeid(value).name(), "NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE") ==0)
hash = compute_hash(value);
// else {
// std::hash<t> hasher;
// hash = hasher(value);
// }
int index = hashFunction(hash);
return table[index].containsForSet(value);
}
void emptyAVLs(){
for(int i = 0; i < amountOfBuckets; i++){
table[i].emptyTree();
}
}
};
#endif //SEARCHENGINETEMPLATES_HASHSET_H