-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashMap.h
158 lines (128 loc) · 3.72 KB
/
HashMap.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// Created by Barlas on 1/13/2021.
//
#ifndef REQUENT_WORDS_HASHMAP_H
#define REQUENT_WORDS_HASHMAP_H
#include "FileOpener.h"
template<typename K, typename V>
class HashNode{
public:
V value; // kac kere kullanildigi
K key; // string
//constructor
HashNode(K key, V value){
this->value = value;
this->key = key;
}
};
//template for generic type
template<typename K, typename V>
class HashMap {
private:
HashNode<K,V> **HMap;
int capacity; // Max size
int size;
void insert(int hashIndex, HashNode<K,V> *node) {
//find next free space
while(HMap[hashIndex] != NULL && HMap[hashIndex]->key != node->key) {
hashIndex++;
hashIndex %= capacity;
}
size++;
HMap[hashIndex] = node;
}
int calculateHashValue(K word){
unsigned int hashedWord = std::hash<K>{}(word);
int address = hashedWord % capacity;
return address;
}
public:
HashMap(int capacity){
this->capacity = capacity;
size = 0;
HMap = new HashNode<K,V>*[capacity];
for(int i=0; i < capacity;i++){
HMap[i] = NULL;
}
}
void put(K key, V value) {
// hashing
HashNode<K, V> *newNode = new HashNode<K,V>(key, value);
int hashIndex = calculateHashValue(key);
// Chech if index empty
insert(hashIndex, newNode);
}
V get(K &key){
int hashIndex = calculateHashValue(key);
while (HMap[hashIndex] != NULL) {
if (HMap[hashIndex]->key == key) {
return HMap[hashIndex]->value;
}
else{
hashIndex = (hashIndex+1) % capacity;
}
}
return 1;
}
void display(){
for(int i=0 ; i<capacity ; i++){
if(HMap[i] != NULL)
std::cout << i << "key = " << HMap[i]->key
<<" value = "<< HMap[i]->value << std::endl;
}
}
void calculateTopTen(){
HashNode<K,V> **topTen = new HashNode<K,V>*[10];
for(int i=0; i < 10;i++){
topTen[i] = NULL;
}
for(int i=0 ; i<capacity ; i++) {
if(HMap[i] != NULL) {
int minIndex=0;
int minValue=0;
for (int j = 0; j < 10; ++j) {
if (topTen[j] != NULL) {
if(minValue == 0 || topTen[j]->value < minValue) {
minIndex = j;
minValue = topTen[j]->value;
}
}
else {
minValue=0;
minIndex=j;
break;
}
}
if(minValue < HMap[i]->value){
topTen[minIndex] = HMap[i];
}
}
}
for(int j=0; j < 10;j++){
int maxIndex=0;
int maxValue=0;
for(int i=0; i < 10;i++) {
if(topTen[i] != NULL && topTen[i]->value > maxValue){
maxIndex = i;
maxValue = topTen[i]->value;
}
}
std::cout << topTen[maxIndex]->key << " " << topTen[maxIndex]->value <<std::endl;
topTen[maxIndex] = NULL;
}
}
// Checks whether key exists in hashmap
bool isExist(K key){
int hashIndex = calculateHashValue(key);
while (HMap[hashIndex] != NULL) {
if (HMap[hashIndex]->key == key) {
return true;
}
else{
hashIndex = (hashIndex+1) % capacity;
}
}
return false;
}
};
#endif //REQUENT_WORDS_HASHMAP_H