AlgoPlus v0.1.0
Loading...
Searching...
No Matches
hash_table.h
1#ifndef HASH_TABLE_H
2#define HASH_TABLE_H
3
4#ifdef __cplusplus
5#include <functional>
6#include <iostream>
7#include <list>
8#include <optional>
9#include <string>
10#include <unordered_map>
11#include <vector>
12#include <cstdint>
13#endif
14
32template <typename KeyType, typename ValueType> class hash_table {
33 public:
34 using BucketType = std::unordered_map<size_t, std::list<std::pair<KeyType, ValueType>>>;
35
41 hash_table(std::vector<std::pair<KeyType, ValueType>> v = {}) {
42 if (!v.empty()) {
43 for (auto &x : v) {
44 this->insert(x.first, x.second);
45 }
46 }
47 }
48
54 hash_table(const hash_table &h) : bucketList(h.bucketList), hash(h.hash) {
55
56
57 }
58
65 if(&this == h){
66 return *this;
67 }
68 bucketList = h.bucketList;
69 hash = h.hash;
70 return *this;
71 }
72
76 ~hash_table() { bucketList.clear(); }
77
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) {
90 pair.second = value;
91 return;
92 }
93 }
94 list.emplace_back(key, value);
95 }
96
103 std::optional<ValueType> retrieve(const KeyType &key) {
104 auto &list = bucketList[hash(key)];
105 for (auto &pair : list) {
106 if (pair.first == key) {
107 return pair.second;
108 }
109 }
110 return std::nullopt;
111 }
112
121 void remove(const KeyType &key) {
122 auto &list = bucketList[hash(key)];
123 list.remove_if([key](const auto &pair) { return pair.first == key; });
124 }
125
126 class Iterator;
127
128 Iterator begin() {
129 auto startIterator = bucketList.begin();
130 while (startIterator != bucketList.end() && startIterator->second.empty())
131 startIterator++;
132 return Iterator(startIterator, bucketList.end());
133 }
134
135 Iterator end() {
136 return Iterator(bucketList.end(), bucketList.end());
137 }
138
143 friend std::ostream &operator<<(std::ostream &out, hash_table<KeyType, ValueType> &h) {
144 out << '[';
145 for (auto &[key, list] : h.bucketList) {
146 for (auto &pair : h.bucketList[key]) {
147 out << "{" << pair.first << ", " << pair.second << "} ";
148 }
149 out << '\n';
150 }
151 out << ']';
152 return out;
153 }
154
155 private:
156 std::hash<KeyType> hash;
157 BucketType bucketList;
158};
159
163template <typename KeyType, typename ValueType>
164class hash_table<KeyType, ValueType>::Iterator {
165 private:
166 using BucketIterator = typename BucketType::iterator;
167 using ListIterator = typename std::list<std::pair<KeyType, ValueType>>::iterator;
168 BucketIterator bucketIter, bucketEnd;
169 ListIterator listIter;
170
171 std::unordered_map<size_t, std::list<std::pair<KeyType, ValueType>>>
172 bucketList;
173 std::vector<size_t> key_values;
174 int64_t index{};
175
176 public:
182 explicit Iterator(BucketIterator start, BucketIterator end)
183 : bucketIter(start), bucketEnd(end) {
184 if (bucketIter != bucketEnd) {
185 listIter = bucketIter->second.begin();
186 }
187 }
188
196 const std::unordered_map<size_t, std::list<std::pair<KeyType, ValueType>>>
197 &bucket) {
198 this->bucketList = bucket;
199 return *(this);
200 }
201
208 if (++listIter == bucketIter->second.end()) {
209 while (++bucketIter != bucketEnd && bucketIter->second.empty());
210 if (bucketIter != bucketEnd) {
211 listIter = bucketIter->second.begin();
212 }
213 }
214 return *this;
215 }
216
223 Iterator it = *this;
224 ++*(this);
225 return it;
226 }
227
234 if (this->index > 0) {
235 this->index--;
236 }
237 return *(this);
238 }
239
246 Iterator it = *this;
247 --*(this);
248 return it;
249 }
250
259 bool operator!=(const Iterator& it) const {
260 return this->bucketIter != it.bucketIter
261 || (bucketIter != bucketEnd && this->listIter != it.listIter);
262 }
263
270 std::pair<KeyType, ValueType>& operator*() {
271 return *listIter;
272 }
273};
274
275#endif // HASH_TABLE_H
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