#pragma once #include #include #include #include #include template class HashTable { public: HashTable(size_t size) : size { size } , table(size) { } bool insert(K key, V value) { std::list>& list = get_bucket(key); if (bucket_contains_key(list, key)) { return false; } list.push_back(std::pair(key, value)); return true; } std::optional get(K key) { std::list>& list = get_bucket(key); auto iter = bucket_find_key(list, key); if (iter != list.end()) { return std::optional((*iter).second); } return std::optional(); } bool remove(K key) { std::list>& list = get_bucket(key); auto iter = bucket_find_key(list, key); if (iter != list.end()) { list.erase(iter); return true; } return false; } void print() { size_t index { 0 }; for (auto bucket : table) { std::cout << "Bucket " << index++ << ": ["; for (auto pair : bucket) { std::cout << "(" << pair.first << ", " << pair.second << ")"; } std::cout << "]" << "\n"; } } private: size_t size; std::vector>> table; std::hash hash_function; auto bucket_find_key(std::list>& list, K key) { return std::find_if(list.begin(), list.end(), [&key](const std::pair& pair) { return pair.first == key; }); } bool bucket_contains_key(std::list>& list, K key) { return list.begin() != list.end() && bucket_find_key(list, key) != list.end(); } std::list>& get_bucket(K key) { size_t index = hash_function(key) % size; return table.at(index); } };