-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
100 lines (84 loc) · 2.56 KB
/
HashTable.cpp
File metadata and controls
100 lines (84 loc) · 2.56 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
#include <string>
#include <fstream>
#include "HashTable.h"
HashTable::HashTable()
{
for(int i = 0; i < table_size; i++)
{
HT[i] = new Element; //allocating each pointer (HT[i]) as an Element type
HT[i]->word = ""; //initializing each pointer to word with ""
HT[i]->counter = 0; //initializing each pointer to counter to 0
}
}
HashTable::~HashTable()
{
for(int i = 0; i < table_size; i++)
{
if(HT[i]->word != "")
delete HT[i];
}
//deleting each pointer that is not null!
}
unsigned int HashTable::HashFunction(std::string key)
{
unsigned int index = 0;
int seed = 131; //using a seed parameter to prevent hash collisions for different data
for(int i = 0; i < (int)key.length(); i++)
{
index = (index * seed) + key[i];
}
//Hash Function: Each ASCII number of string s is being added with the current index * seed
return index % table_size;
}
void HashTable::AddString(std::string key)
{
float start=clock();
unsigned int index = HashFunction(key); //call HashFunction to get the index
if(HT[index]->word == "") //That means that HT[index] is empty
{
//occupying index...//
HT[index]->word = key;
HT[index]->counter = 1;
}
else //That means that HT[index] is already occupied so there are 2 possible cases:
{
if (HT[index]->word == key) //if string key is equal to the HT[index]->word then we just increase the counter by 1
{
HT[index]->counter += 1;
}
else //in the else statement it means that we came up with a collision
{
AvoidCollision(index); //call AvoidCollision(index) to update the value of index(see line 74-81)
//occupying the correct space
HT[index]->word = key;
HT[index]->counter = 1;
}
}
timeOfHash+=timecount(start);
}
void HashTable::searchHashTable(string *Q)
{
string key;
float begin=clock();
cout<<endl<<"\t\t\t\t\tHASHTABLE"<<endl;
cout<<"time of creation: "<<timeOfHash<<endl;
for(int i=0;i<1000;i++){
key=Q[i];
unsigned int index = HashFunction(key); //call HashFunction to get the index of string key
while (HT[index]->word != key)
{
index++; //update the index until HT[index]->word is equal to key!
}
cout<<HT[index]->word<<" "<<HT[index]->counter<<endl;
}
float time=timecount(begin);
cout<<endl<<"time: "<<time<<endl;
}
void HashTable::AvoidCollision(unsigned int & index) //call by reference
{
while(HT[index]->word != "")
{
index++; //just a linear probing
}
index %= table_size; //update index
}