|
| 1 | +/* |
| 2 | + ConcurrentHashMap C++ Library |
| 3 | + A thread-safe hash map implementation in C++ with support for concurrent read and write operations. |
| 4 | + This C++ library provides a ConcurrentHashMap class that allows multiple threads to perform read and write operations on a hash map concurrently. It uses std::shared_timed_mutex to provide efficient and safe concurrent access. |
| 5 | + Author : Eray Ozturk | erayozturk1@gmail.com |
| 6 | + URL : github.com/diffstorm |
| 7 | + Date : 10/06/2020 |
| 8 | +*/ |
| 9 | + |
| 10 | +#ifndef CONCURRENT_HASH_MAP_H |
| 11 | +#define CONCURRENT_HASH_MAP_H |
| 12 | + |
| 13 | +#include <iostream> |
| 14 | +#include <vector> |
| 15 | +#include <mutex> |
| 16 | +#include <functional> |
| 17 | + |
| 18 | +template <typename Key, typename Value, typename Hash = std::hash<Key>> |
| 19 | +class ConcurrentHashMap { |
| 20 | +private: |
| 21 | + // Define the structure of each node in the hash map |
| 22 | + struct Node { |
| 23 | + Key key; |
| 24 | + Value value; |
| 25 | + Node* next; |
| 26 | + Node(const Key& k, const Value& v) : key(k), value(v), next(nullptr) {} |
| 27 | + }; |
| 28 | + |
| 29 | + // Define the hash map buckets and associated mutexes |
| 30 | + std::vector<std::vector<Node*>> buckets; |
| 31 | + std::vector<std::mutex> mutexes; |
| 32 | + Hash hashFunction; |
| 33 | + |
| 34 | + // Get the mutex for a given key |
| 35 | + std::mutex& getMutex(const Key& key) { |
| 36 | + std::size_t hashValue = hashFunction(key); |
| 37 | + return mutexes[hashValue % mutexes.size()]; |
| 38 | + } |
| 39 | + |
| 40 | +public: |
| 41 | + explicit ConcurrentHashMap(std::size_t num_buckets = 16) : buckets(num_buckets), mutexes(num_buckets) {} |
| 42 | + |
| 43 | + // Insert a key-value pair into the hash map |
| 44 | + void insert(const Key& key, const Value& value) { |
| 45 | + std::unique_lock lock(getMutex(key)); |
| 46 | + std::size_t hashValue = hashFunction(key); |
| 47 | + std::size_t index = hashValue % buckets.size(); |
| 48 | + |
| 49 | + Node* newNode = new Node(key, value); |
| 50 | + newNode->next = buckets[index].empty() ? nullptr : buckets[index][0]; // Cast to Node* |
| 51 | + buckets[index] = {newNode}; |
| 52 | + } |
| 53 | + |
| 54 | + // Retrieve the value associated with a key from the hash map |
| 55 | + bool get(const Key& key, Value& value) { |
| 56 | + std::unique_lock lock(getMutex(key)); |
| 57 | + std::size_t hashValue = hashFunction(key); |
| 58 | + std::size_t index = hashValue % buckets.size(); |
| 59 | + |
| 60 | + Node* current = buckets[index].empty() ? nullptr : buckets[index][0]; // Cast to Node* |
| 61 | + while (current != nullptr) { |
| 62 | + if (current->key == key) { |
| 63 | + value = current->value; |
| 64 | + return true; // Found the key |
| 65 | + } |
| 66 | + current = current->next; // No need to cast here |
| 67 | + } |
| 68 | + |
| 69 | + return false; // Key not found |
| 70 | + } |
| 71 | + |
| 72 | + // Remove a key-value pair from the hash map |
| 73 | + void remove(const Key& key) { |
| 74 | + std::unique_lock lock(getMutex(key)); |
| 75 | + std::size_t hashValue = hashFunction(key); |
| 76 | + std::size_t index = hashValue % buckets.size(); |
| 77 | + |
| 78 | + Node* current = buckets[index].empty() ? nullptr : buckets[index][0]; // Cast to Node* |
| 79 | + Node* prev = nullptr; |
| 80 | + |
| 81 | + while (current != nullptr) { |
| 82 | + if (current->key == key) { |
| 83 | + if (prev == nullptr) { |
| 84 | + buckets[index][0] = current->next; // No need to cast here |
| 85 | + } else { |
| 86 | + prev->next = current->next; // No need to cast here |
| 87 | + } |
| 88 | + delete current; |
| 89 | + return; |
| 90 | + } |
| 91 | + prev = current; |
| 92 | + current = current->next; // No need to cast here |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Print the contents of the hash map |
| 97 | + void print() { |
| 98 | + for (std::size_t i = 0; i < buckets.size(); ++i) { |
| 99 | + std::lock_guard lock(mutexes[i]); |
| 100 | + std::cout << "Bucket " << i << ": "; |
| 101 | + Node* current = buckets[i].empty() ? nullptr : buckets[i][0]; // Cast to Node* |
| 102 | + while (current != nullptr) { |
| 103 | + std::cout << "(" << current->key << ", " << current->value << ") "; |
| 104 | + current = current->next; // No need to cast here |
| 105 | + } |
| 106 | + std::cout << std::endl; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Destructor to clean up memory |
| 111 | + ~ConcurrentHashMap() { |
| 112 | + for (auto& bucket : buckets) { |
| 113 | + Node* current = bucket.empty() ? nullptr : bucket[0]; // Cast to Node* |
| 114 | + while (current != nullptr) { |
| 115 | + Node* temp = current; |
| 116 | + current = current->next; // No need to cast here |
| 117 | + delete temp; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +#endif // CONCURRENT_HASH_MAP_H |
0 commit comments