-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing.h
212 lines (157 loc) · 4.78 KB
/
hashing.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifndef hashing_h
#define hashing_h
#include <time.h>
/*this hashing algorithm was originaly crated by D.Brakas and this algorithm works way better than mine and mine was a hashing with chaning not with open adresses. This project requires hashing with open adresses so thanks to professor who let us use it we will have a working complete algorithm for the data structures which at least will pass the compile/review point.*/
template <typename dataType, typename keyType>
class myPair
{
public:
myPair(dataType d, keyType k)
{
data=d;
key=k;
}
dataType data;
keyType key;
};
template <typename dataType, typename keyType>
class hashTable
{
private:
int size;
int elements;
myPair<dataType,keyType> **A;
void *deleted;
unsigned int hash(const keyType &);
unsigned int hash2(unsigned int );
myPair<dataType,keyType> *& search(const keyType &);
public:
hashTable();
hashTable(int n);
~hashTable();
bool insert(const dataType &, const keyType &);
int search(const keyType &, dataType &data);
int rtime(const keyType &, dataType &data);
bool remove(const keyType &);
};
template <typename dataType, typename keyType>
hashTable<dataType,keyType>::hashTable()
{//creation of hashing
size=10; //size of hashing
elements=0; //how many objs it has at the starting point
deleted = this;//new int();
A = new myPair<dataType,keyType> *[size];
for(int i=0;i<size;i++)
{
A[i]=nullptr;
//A[i]->d=1;
}
}
template <typename dataType, typename keyType>
hashTable<dataType,keyType>::hashTable(int n)
{ //rehashing v1
deleted = this;//new int();
size=n;
elements=0;
A = new myPair<dataType,keyType> *[size];
for(int i=0;i<size;i++)
{
A[i]=nullptr;
}
}
template <typename dataType, typename keyType>
hashTable<dataType,keyType>::~hashTable()
{//destructor for less usage of ram after the end of hashing
delete[] A;
A=nullptr;
elements=0;
size=0;
}
template <typename dataType, typename keyType>
unsigned int hashTable<dataType,keyType>::hash(const keyType &d)
{//it turn strs to long long ints
return d%size;
}
template <typename dataType, typename keyType>
unsigned int hashTable<dataType,keyType>::hash2(unsigned int d)
{//same as the previous
return (d+1)%size;
}
template <typename dataType, typename keyType>
bool hashTable<dataType,keyType>::insert (const dataType &data, const keyType &key)
{//working insert with deletes
if (search(key)!=nullptr)
{
search(key)->data+=1;
return false;
}
if (elements>=size/2)
{
myPair<dataType,keyType> **temp;
size*=2;
temp = new myPair<dataType,keyType>*[size];
for(int i=0;i<size;i++)
temp[i]=nullptr;
for (int i=0;i<size/2;i++)
{
if (A[i]!=nullptr && A[i]!=deleted)
{
A[i]->data+=1;
unsigned int p = hash(A[i]->key);
while (temp[p]!=nullptr)
p = hash2(p);
temp[p]=A[i];
A[i]=nullptr;
}
}
delete[]A;
A=temp;
}
unsigned int p = hash(key);
while (A[p]!=nullptr && A[p]!=deleted)
p = hash2(p);
A[p]=new myPair<dataType,keyType> (data,key);
A[p]->data+=1;
elements++;
return true;
}
template <typename dataType, typename keyType>
myPair<dataType,keyType> *& hashTable<dataType,keyType>::search(const keyType &key)
{ //searching for hashing
unsigned int p = hash(key);
while (A[p]==deleted || (A[p]!=nullptr && A[p]->key!=key))
p = hash2(p);
return A[p];
}
template <typename dataType, typename keyType>
int hashTable<dataType,keyType>::search(const keyType &key ,dataType &data)
{ //outputs the times a word appear in the txt
myPair<dataType,keyType> *p = search(key);
if (p==nullptr)
return false;
data = p->data;
return data-=4;
}
template <typename dataType, typename keyType>
int hashTable<dataType,keyType>::rtime(const keyType &key ,dataType &data)
{ //results of time in search
clock_t tStart = clock();
myPair<dataType,keyType> *p = search(key);
if (p==nullptr)
return -1;
data = p->data;
return ((clock() - tStart)/(CLOCKS_PER_SEC/100000));
return true;
}
template <typename dataType, typename keyType>
bool hashTable<dataType,keyType>::remove(const keyType &key)
{ //delete algorithm
myPair<dataType,keyType> * &p = search(key);
if (p==nullptr)
return false;
elements--;
delete p;
p = (myPair<dataType,keyType> *) deleted;
return true;
}
#endif