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 213 214 215 216
|
#ifndef HashTable_H
#define HashTable_H
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include "item.h"
using namespace std;
template <typename K, typename T>
class HashTable
{
int size;
friend long long int stringToNumber(string str);
bool chkIfNumberIsPrime(int num)
{
bool flag=true;
int numSqrt;
numSqrt=(int)sqrt((double)num);
for(int x=2;x<=numSqrt; x++)
if (num % x ==0)
return false;
return true;
}
int calcPrimeNumber(int num)
{
while(!chkIfNumberIsPrime(num++));
return num-1;
}
Item<K,T>* find(K _key)
{
Item<K,T>* ptr=NULL;
int i=0;
do
ptr=&table[hash(_key,i++)];
while (!(ptr->status==Item<K,T>::Status::empty) && ptr->key!=_key && i<=size);
if(ptr->key==_key)
return ptr;
else
return NULL;
}
long long int stringToNumber(string str)
{
long long int key=0;
int size=str.length();
int *arr = new int[size];
int temp;
for(int i=0; i<size; i++)
{
arr[i]=int(str[i]);
temp=(int)pow((double)128,i);
key+=arr[i]*temp;
}
delete []arr;
return key;
}
public:
Item<K,T>* table;
HashTable(int _size)
{
table = new Item<K,T>[calcPrimeNumber(_size)];
size=_size;
}
~HashTable() { delete []table; }
int h2(int k) { return 1; }
int h1(int k) { return k % size; }
int hash(int k, int i) { return (h1(k)+i*h2(k)) % size; }
bool hashInsert(K _key, T _value)
{
Item<K,T>* ptr=NULL;
int i=0;
long long int innerKey;
//if (typeid(K) == typeid(string))
innerKey=stringToNumber(_key);
//else
// innerKey=(long long int)_key;
//keyStr.assign(item->key);
while ((ptr==NULL || ptr->status==Item<K,T>::Status::full) && i<=size)
ptr=&table[hash(innerKey,i++)];
if (i<=size)
{
ptr->key=_key;
ptr->value=_value;
ptr->status=Item<K,T>::Status::full;
return true;
}
else
{
cout<<"hash table overflow.\n";
return false;
}
}
Item<K,T>* hashSearch(K _key){ return (find(_key)); }
//I understood that delete function get key and look for it. if it's found and the key equal the key in the node it deleted.
bool hashDelete(K _key)
{
Item<K,T>* ptr=find(_key);
if (ptr)
{
ptr->key=NULL;
ptr->value=NULL;
ptr->status=Item<K,T>::Status::deleted;
return true;
}
else
return false;
}
friend ostream & operator<<<K,T>(ostream &os, HashTable<K,T> &ht);
};
//template <class K,class T>
//long long int stringToNumber(string str)
// {
// long long int key=0;
// int size=str.length();
// int *arr = new int[size];
// int temp;
//
// for(int i=0; i<size; i++)
// {
// arr[i]=int(str[i]);
// temp=(int)pow((double)128,i);
// key+=arr[i]*temp;
// }
//
// delete []arr;
// return key;
// }
template <class K, class T>
ostream & operator<<(ostream &os, HashTable<K,T> &ht)
{
Item<K,T> *item;
string statusStr="", valueStr="", keyStr="";
os<<left<<setw(7)<<"Key"<<left<<setw(7)<<"Value"<<setw(7)<<"Status"<<endl;
os<<"--------------------\n";
for (int x=0; x<ht.size; x++)
{
item=&ht.table[x];
if ((item->key)==K())
keyStr="NULL";
else
{
//keyStr.assign(item->key);
//if string
//keyStr=item->key;
//else
// keyStr=to_string((long long)item->key);
}
if ((item->value)==T())
valueStr="NULL";
else
{
//if string
valueStr=item->value;
//else
//valueStr=to_string((long long)item->value);
}
switch(item->status)
{
case -1: statusStr="deleted"; break;
case 0: statusStr="empty"; break;
case 1: statusStr="full"; break;
}
os<<left<<setw(7)<<keyStr<<left<<setw(7)<<valueStr<<setw(7)<<statusStr<<endl;
}
return os;
}
#endif
|