-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhashdict.cpp
More file actions
29 lines (28 loc) · 775 Bytes
/
hashdict.cpp
File metadata and controls
29 lines (28 loc) · 775 Bytes
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
//insert e into hash table HT
template <typename Key, typename E>
void hashdict<Key,E>::
hashInsert(const Key& k, const E& e)
{
int home; //home position
int pos = home =h(k); //initial probe sequence
for (int i=1; EMPTYKEY != (HT[pos]).key();i++)
{
pos = (home + p(k,i)) % M; //probe
Assert(k != (HT[pos]).key(), "Duplicates not allowed");
}
KVpair<Key,E> temp(k,e);
HT[pos] =temp;
}
//search for the record with Key K
template <typename Key, typename E>
E hashdict<key,E>::hashSearch(const Key& k) const
{
int home;
int pos = home = h(k);
for (int i=1;(k != (HT[pos]).key()) &&
(EMPTYKEY != (HT[pos]).key());i++)
pos = (home + p(k,i)) % M;
if (k == (HT[pos]).key())
return (HT[pos]).value();
else return NULL;
}