Created
February 6, 2015 05:14
-
-
Save RahulSD/cef73fd49abe200ca353 to your computer and use it in GitHub Desktop.
POC HashTable implementation using Uninitialized arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ToDO : | |
Add Generics | |
Better hasing function | |
Proper handling of size , and autoResize | |
Add more operations | |
*/ | |
#include<iostream> | |
using namespace std; | |
struct cell | |
{ | |
int key; | |
string value; | |
cell * link; | |
}; | |
class ArrayInit | |
{ | |
private : | |
//Array of Data to store | |
cell ** data; | |
int * dense; | |
int * parse; | |
int top; | |
int sz; | |
public : | |
void create(int sz) | |
{ | |
data = new cell * [sz]; | |
dense = new int[sz]; | |
parse = new int[sz]; | |
this->sz = sz; | |
top = 0; | |
} | |
cell * getElement(int i) | |
{ | |
if(parse[i] < top && dense[parse[i]] == i ) | |
return data[i]; | |
else | |
{ | |
dense[top] = i; | |
parse[dense[top]] = top; | |
data[i] = NULL; | |
top ++; | |
return NULL; | |
} | |
} | |
void setElement(cell * c, int i) | |
{ | |
if(parse[i] < top && dense[parse[i]] == i) | |
{ | |
data[i] = c; | |
}else | |
{ | |
dense[top] = i; | |
parse[i] = top; | |
top++; | |
data[i] = c; | |
} | |
} | |
~ArrayInit() | |
{ | |
delete data; | |
delete dense; | |
delete parse; | |
} | |
}; | |
class HashTable | |
{ | |
private: | |
ArrayInit dataStore; | |
int noOfBuckets ; | |
int noOfElementsAdded; | |
int getHash(int key) | |
{ | |
return ( key % noOfBuckets); | |
} | |
public: | |
enum Messages { | |
}; | |
void create(int noOfBuckets) | |
{ | |
dataStore.create(noOfBuckets); | |
this->noOfBuckets = noOfBuckets; | |
this->noOfElementsAdded = 0; | |
} | |
void insertElement(int key, string value) | |
{ | |
int h = getHash(key); | |
cell * newNode = new cell; | |
newNode->key = key; | |
newNode->value=value; | |
newNode->link = NULL; | |
cell * node = dataStore.getElement(h); | |
//NO Collision | |
if(node == NULL) | |
{ | |
dataStore.setElement(newNode,h); | |
}else | |
{ | |
//Collision occured , so put at the end of bucket | |
while(node->link != NULL) | |
node=node->link; | |
node->link = newNode; | |
} | |
noOfElementsAdded++; | |
} | |
string getValue(int key) | |
{ | |
int h = getHash(key); | |
cell * node = dataStore.getElement(h); | |
while(node != NULL) | |
{ | |
if(node->key == key) | |
break; | |
node=node->link; | |
} | |
if(node == NULL) | |
{ | |
return ""; | |
}else | |
{ | |
return node->value; | |
} | |
} | |
}; | |
int main() | |
{ | |
HashTable hs; | |
hs.create(1000); | |
hs.insertElement(10,"ABC"); | |
hs.insertElement(20,"BCD"); | |
hs.insertElement(30,"PQR"); | |
cout<<hs.getValue(10)<<endl; | |
cout<<hs.getValue(20)<<endl; | |
cout<<hs.getValue(30)<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment