-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.h
More file actions
151 lines (129 loc) · 3.08 KB
/
Hash.h
File metadata and controls
151 lines (129 loc) · 3.08 KB
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
#pragma once
#ifndef Hash_H
#define Hash_H
#include <iostream>
#include "List.h"
#include "Armors.h"
#include <cmath>
using namespace std;
class Hash {
private:
//number of buckets
int numBucket;
List* table;
int collision;
double LF;
public:
//constructor
Hash();
//overloaded constructor
Hash(int);
//Destructor
~Hash();
//basic hash functions
bool insertItem(Armors* A);
bool deleteItem(string);
bool searchHash(Armors*, Armors*&);
int hashFunction(string);
int badHashFunc(string);
void printHash(void visit(Armors*));
void printBuckList(int, void visit(Armors*));
void stat();
//Setter
void setCol(int a) { collision = a; }
void setLF(double a) { LF = a; }
//getter
int getCol() { return collision; }
double getLF() { return LF; }
};
//print by bucket number
void Hash::printBuckList(int index, void visit(Armors*)) {
if (table[index].getCount() > 0)
table[index].displayList(visit);
else
cout << "There is no item in the " << index << " bucket";
}
//print unsorted
void Hash::printHash(void visit(Armors*)) {
for (int i = 0; i < numBucket; i++) {
cout << "Bucket number " << i << ": ";
table[i].displayList(visit);
}
}
void Hash::stat() {
int collision = 0;
int numItem = 0;
double loadFactor = 0.0;
for (int i = 0; i < numBucket; i++) {
if (table[i].getCount() > 1) {
collision += table[i].getCount() - 1;
}
}
for (int i = 0; i < numBucket; i++) {
numItem += table[i].getCount();
}
setCol(collision);
cout << "Collision: " << collision << endl;
loadFactor = ((numItem - collision) * 100 / numBucket);
setLF(loadFactor);
cout << "Load Facotr " << loadFactor << "%" << endl;
}
//Overloaded Constructor
Hash::Hash(int b) {
this->numBucket = b;
table = new List[numBucket];
cout << "Hash array of " << b << " number of index created\n";
}
bool Hash::searchHash(Armors * A, Armors * &returnItem) {
bool found = false;
int index = hashFunction(A->getCodename());
if (table[index].searchListP(A, returnItem)) {
found = true;
}
return found;
}
//Inserting an item into the hash table
bool Hash::insertItem(Armors * A) {
bool found = false;
int index = hashFunction(A->getCodename());
if (table[index].insertNode(A)) {
found = true;
cout << A->getCodename() << " has been inserted\n\n";
}
return found;
}
//what should be the parameter
bool Hash::deleteItem(string target) {
bool found = false;
int index = hashFunction(target);
if (table[index].deleteNode(target)) {
found = true;
}
return found;
}
int Hash::hashFunction(string unikey) {
int sum = 0;
int len = unikey.size();
for (int i = 0; i < len; i++) {
sum += unikey[i];
}
int key = (7 * sum + 19) % numBucket;
return key;
}
int Hash::badHashFunc(string unikey) {
int sum = 0;
int len = unikey.size();
for (int i = 0; i < len; i++) {
sum += unikey[i];
}
int key = sum % 4;
return key;
}
Hash::~Hash() {
for (int i = 0; i < numBucket; i++) {
if (table[i].getCount() > 0) {
table[i].~List();
}
}
}
#endif Hash-H