10#include <unordered_map>
32template <
typename KeyType,
typename ValueType>
class hash_table {
34 using BucketType = std::unordered_map<size_t, std::list<std::pair<KeyType, ValueType>>>;
41 hash_table(std::vector<std::pair<KeyType, ValueType>> v = {}) {
44 this->
insert(x.first, x.second);
68 bucketList = h.bucketList;
86 void insert(
const KeyType &key,
const ValueType &value) {
87 auto &list = bucketList[hash(key)];
88 for (
auto &pair : list) {
89 if (pair.first == key) {
94 list.emplace_back(key, value);
103 std::optional<ValueType>
retrieve(
const KeyType &key) {
104 auto &list = bucketList[hash(key)];
105 for (
auto &pair : list) {
106 if (pair.first == key) {
122 auto &list = bucketList[hash(key)];
123 list.remove_if([key](
const auto &pair) {
return pair.first == key; });
129 auto startIterator = bucketList.begin();
130 while (startIterator != bucketList.end() && startIterator->second.empty())
132 return Iterator(startIterator, bucketList.end());
136 return Iterator(bucketList.end(), bucketList.end());
145 for (
auto &[key, list] : h.bucketList) {
146 for (
auto &pair : h.bucketList[key]) {
147 out <<
"{" << pair.first <<
", " << pair.second <<
"} ";
156 std::hash<KeyType> hash;
157 BucketType bucketList;
163template <
typename KeyType,
typename ValueType>
166 using BucketIterator =
typename BucketType::iterator;
167 using ListIterator =
typename std::list<std::pair<KeyType, ValueType>>::iterator;
168 BucketIterator bucketIter, bucketEnd;
169 ListIterator listIter;
171 std::unordered_map<size_t, std::list<std::pair<KeyType, ValueType>>>
173 std::vector<size_t> key_values;
182 explicit Iterator(BucketIterator start, BucketIterator end)
183 : bucketIter(start), bucketEnd(end) {
184 if (bucketIter != bucketEnd) {
185 listIter = bucketIter->second.begin();
196 const std::unordered_map<
size_t, std::list<std::pair<KeyType, ValueType>>>
198 this->bucketList = bucket;
208 if (++listIter == bucketIter->second.end()) {
209 while (++bucketIter != bucketEnd && bucketIter->second.empty());
210 if (bucketIter != bucketEnd) {
211 listIter = bucketIter->second.begin();
234 if (this->index > 0) {
260 return this->bucketIter != it.bucketIter
261 || (bucketIter != bucketEnd && this->listIter != it.listIter);
Iterator class.
Definition hash_table.h:164
std::pair< KeyType, ValueType > & operator*()
operator * for Type Iterator
Definition hash_table.h:270
Iterator & operator--()
operator – for type Iterator
Definition hash_table.h:233
Iterator & operator=(const std::unordered_map< size_t, std::list< std::pair< KeyType, ValueType > > > &bucket)
operator = for hash table iterator class
Definition hash_table.h:195
Iterator operator--(int)
operator – for type Iterator
Definition hash_table.h:245
bool operator!=(const Iterator &it) const
operator != for Type Iterator
Definition hash_table.h:259
Iterator(BucketIterator start, BucketIterator end)
Construct a new Iterator object.
Definition hash_table.h:182
Iterator & operator++()
operator ++ for type Iterator
Definition hash_table.h:207
Iterator operator++(int)
operator ++ for type Iterator
Definition hash_table.h:222
A simple implementation of a hash table.
Definition hash_table.h:32
hash_table & operator=(const hash_table &h)
operator = for the hash_table class
Definition hash_table.h:64
void insert(const KeyType &key, const ValueType &value)
Inserts a key-value pair into the hash table.
Definition hash_table.h:86
std::optional< ValueType > retrieve(const KeyType &key)
Retrieves the value associated with the given key.
Definition hash_table.h:103
hash_table(const hash_table &h)
Copy constructor of the hash_table.
Definition hash_table.h:54
friend std::ostream & operator<<(std::ostream &out, hash_table< KeyType, ValueType > &h)
<< operator for hash_table class
Definition hash_table.h:143
void remove(const KeyType &key)
Removes the key-value pair associated with the given key from the hash table.
Definition hash_table.h:121
hash_table(std::vector< std::pair< KeyType, ValueType > > v={})
Construct a new hash table object.
Definition hash_table.h:41
~hash_table()
Destroy the hash table object.
Definition hash_table.h:76