11#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 inline explicit hash_table(std::vector<std::pair<KeyType, ValueType>> v = {}) {
44 this->
insert(x.first, x.second);
65 bucketList = h.bucketList;
83 inline void insert(
const KeyType& key,
const ValueType& value) {
84 auto& list = bucketList[hash(key)];
85 for (
auto& pair : list) {
86 if (pair.first == key) {
91 list.emplace_back(key, value);
100 inline std::optional<ValueType>
retrieve(
const KeyType& key) {
101 auto& list = bucketList[hash(key)];
102 for (
auto& pair : list) {
103 if (pair.first == key) {
119 auto& list = bucketList[hash(key)];
120 list.remove_if([key](
const auto& pair) {
return pair.first == key; });
125 inline Iterator begin() {
126 auto startIterator = bucketList.begin();
127 while (startIterator != bucketList.end() && startIterator->second.empty()) {
130 return Iterator(startIterator, bucketList.end());
133 inline Iterator end() {
return Iterator(bucketList.end(), bucketList.end()); }
141 for (
auto& [key, list] : h.bucketList) {
142 for (
auto& pair : h.bucketList[key]) {
143 out <<
"{" << pair.first <<
", " << pair.second <<
"} ";
152 std::hash<KeyType> hash;
153 BucketType bucketList;
161 using BucketIterator =
typename BucketType::iterator;
162 using ListIterator =
typename std::list<std::pair<KeyType, ValueType>>::iterator;
163 BucketIterator bucketIter, bucketEnd;
164 ListIterator listIter;
166 std::unordered_map<size_t, std::list<std::pair<KeyType, ValueType>>> bucketList;
167 std::vector<size_t> key_values;
176 explicit Iterator(BucketIterator start, BucketIterator end)
177 : bucketIter(start), bucketEnd(end) {
178 if (bucketIter != bucketEnd) {
179 listIter = bucketIter->second.begin();
190 operator=(
const std::unordered_map<
size_t, std::list<std::pair<KeyType, ValueType>>>& bucket) {
191 this->bucketList = bucket;
201 if (++listIter == bucketIter->second.end()) {
202 while (++bucketIter != bucketEnd && bucketIter->second.empty())
204 if (bucketIter != bucketEnd) {
205 listIter = bucketIter->second.begin();
228 if (this->index > 0) {
254 return this->bucketIter != it.bucketIter ||
255 (bucketIter != bucketEnd && this->listIter != it.listIter);
264 std::pair<KeyType, ValueType>&
operator*() {
return *listIter; }
Iterator class.
Definition hash_table.h:159
std::pair< KeyType, ValueType > & operator*()
operator * for Type Iterator
Definition hash_table.h:264
Iterator & operator--()
operator – for type Iterator
Definition hash_table.h:227
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:190
Iterator operator--(int)
operator – for type Iterator
Definition hash_table.h:239
bool operator!=(const Iterator &it) const
operator != for Type Iterator
Definition hash_table.h:253
Iterator(BucketIterator start, BucketIterator end)
Construct a new Iterator object.
Definition hash_table.h:176
Iterator & operator++()
operator ++ for type Iterator
Definition hash_table.h:200
Iterator operator++(int)
operator ++ for type Iterator
Definition hash_table.h:216
hash_table & operator=(const hash_table &h)
operator = for the hash_table class
Definition hash_table.h:61
void insert(const KeyType &key, const ValueType &value)
Inserts a key-value pair into the hash table.
Definition hash_table.h:83
std::optional< ValueType > retrieve(const KeyType &key)
Retrieves the value associated with the given key.
Definition hash_table.h:100
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:139
void remove(const KeyType &key)
Removes the key-value pair associated with the given key from the hash table.
Definition hash_table.h:118
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:73