Skip to content

Commit 65d4349

Browse files
committed
Improve the hash func in C++ hash table
1 parent 773ab70 commit 65d4349

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

Operating-System-Concepts/hash-table/hash-table-using-stl/hashmap.h

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
#include <iostream>
1111
#include <cstddef>
1212

13-
template<typename K, std::size_t size>
13+
template<typename K>
1414
struct KeyHash
1515
{
16-
unsigned long operator() (const K& key)
16+
unsigned long operator() (const K& key, size_t tableSize)
1717
{
18-
return reinterpret_cast<unsigned long>(key) % size;
18+
return reinterpret_cast<unsigned long>(key) % tableSize;
1919
}
2020
};
2121

22-
template<typename K, typename V, std::size_t size = 1024, typename F = KeyHash<K, size>>
22+
template<typename K, typename V, std::size_t tableSize = 1024, typename F = KeyHash<K>>
2323
class HashMap
2424
{
2525
private:
26-
std::array<std::vector<HashNode<K, V>>, size> table { };
26+
std::array<std::vector<HashNode<K, V>>, tableSize> table { };
2727
F hashFunc;
2828

2929
public:
@@ -32,8 +32,9 @@ class HashMap
3232

3333
void put(const K& key, const V& value)
3434
{
35-
unsigned long hash = hashFunc(key);
35+
unsigned long hash = hashFunc(key, tableSize);
3636

37+
std::cout << key << " , " << value << " , " << hash << "\n";
3738
for (auto node : table[hash]) {
3839
if (node.getKey() == key) {
3940
// key already present in map. Simply overwrite value
@@ -49,7 +50,7 @@ class HashMap
4950

5051
void remove(const K& key)
5152
{
52-
unsigned long hash = hashFunc(key);
53+
unsigned long hash = hashFunc(key, tableSize);
5354

5455
if (table[hash].size() == 1) {
5556
if (table[hash][0].getKey() == key) {
@@ -67,7 +68,7 @@ class HashMap
6768

6869
bool get(const K& key, V& value)
6970
{
70-
unsigned long hash = hashFunc(key);
71+
unsigned long hash = hashFunc(key, tableSize);
7172

7273
for (auto node : table[hash]) {
7374
if (node.getKey() == key) {

Operating-System-Concepts/hash-table/hash-table-using-stl/test.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,30 @@
1313
#include <string>
1414
#include <cstddef>
1515

16-
struct MyKeyHash {
17-
unsigned long operator()(const int& k) const
16+
struct StringKeyHash {
17+
unsigned long operator()(const std::string& key, size_t tableSize) const
1818
{
19-
return k % 10;
19+
unsigned long hash = 5381;
20+
21+
for (auto ch : key) {
22+
hash = ((hash << 5) + hash) + static_cast<int>(ch);
23+
}
24+
25+
return hash % tableSize;
2026
}
2127
};
2228

2329
int main()
2430
{
25-
HashMap<int, std::string, 10, MyKeyHash> map;
26-
map.put(1, "alice");
27-
map.put(2, "bob");
28-
map.put(3, "charlie");
29-
map.put(4, "eve");
31+
HashMap<std::string, std::string, 1024, StringKeyHash> map;
32+
map.put("abc", "alice");
33+
map.put("xyz", "bob");
34+
map.put("mno", "charlie");
35+
map.put("pqr", "eve");
3036

31-
map.remove(4);
37+
map.remove("pqr");
3238
std::string value;
33-
map.get(3, value);
39+
map.get("abc", value);
3440
std::cout << value << "\n";
3541

3642
return 0;

0 commit comments

Comments
 (0)