Created
January 2, 2016 08:56
-
-
Save jackwhit3/a828908d4211a81eb59a to your computer and use it in GitHub Desktop.
Hashtable C++ Implementation (using templates)
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
#include <iostream> | |
#include <vector> | |
using namespace std; | |
template<class K, class V> | |
struct Bucket { | |
K key; | |
V val; | |
Bucket(const K &k, const V &v) : val(v), key(k) {} | |
}; | |
template<class T> struct myHash { | |
}; | |
template<> struct myHash<string> { | |
static size_t hashFunction(const string& s) { | |
return hash<string>()(s); | |
} | |
}; | |
template<> struct myHash<int> { | |
static size_t hashFunction(const int m) { | |
return hash<int>()(m); | |
} | |
}; | |
template<class K, class V, class HashGenerator = myHash<K> > | |
class HashTable { | |
public: | |
vector<vector<Bucket<K, V> > > table; | |
HashTable(int size) { | |
for(int i = 0; i < size; i++) { | |
vector<Bucket<K, V> > v; | |
table.push_back(v); | |
} | |
} | |
~HashTable() {} | |
void set(const K &k, const V &v) { | |
Bucket<K, V> b(k, v); | |
for(int i = 0; i < table[hash(k)].size(); i++) | |
if(table[hash(k)][i].key == k) { | |
table[hash(k)][i] = b; | |
return; | |
} | |
table[hash(k)].push_back(b); | |
} | |
V get(const K &k) { | |
for(int i = 0; i < table[hash(k)].size(); i++) | |
if(table[hash(k)][i].key == k) | |
return table[hash(k)][i].val; | |
} | |
bool exist(const K &k) { | |
for(int i = 0; i < table[hash(k)].size(); i++) | |
if(table[hash(k)][i].key == k) | |
return true; | |
return false; | |
} | |
size_t hash(const K &k) { | |
return HashGenerator::hashFunction(k) % table.size(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment