-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTable.cpp
More file actions
348 lines (306 loc) · 10.1 KB
/
hashTable.cpp
File metadata and controls
348 lines (306 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! Problem Statement: Implement a Hash Table with Fixed Slots
//!
//! Design a hash table with **26 slots** to store and manage keys.
//! The hash table supports the following operations:
//!
//! ## Input Format:
//! 1. Each input is a string:
//! - The first character is either:
//! - `A`: Add a key to the table.
//! - `D`: Delete a key from the table.
//! - The rest of the string is the key (lowercase letters from `a` to `z`).
//! - Ignore invalid inputs (e.g., keys longer than 10 characters, non-lowercase characters).
//!
//! ## Hash Table Details:
//! - **Size**: 26 slots, each indexed by a hash value.
//! - **Slot States**:
//! - **Never Used**: The slot has never been occupied.
//! - **Tombstoned**: The slot once held a key but is now marked as deleted.
//! - **Occupied**: The slot currently holds a key.
//! - **Key Constraints**:
//! - Keys are lowercase English letters.
//! - Maximum key length: 10 characters.
//!
//! ## Hashing:
//! - Compute the hash value of a key using its **last character** (e.g., the hash value of `"abc"` is based on `'c'`).
//! - Use **open addressing with linear probing** for collision resolution:
//! - If the slot for a hash value is occupied or tombstoned, check the next slot, wrapping around if necessary.
//!
//! ## Operations:
//! 1. **Add (`A key`)**:
//! - Search for the key:
//! - If the key already exists in the table, do nothing.
//! - If the key doesn't exist:
//! - Compute its hash value and find the first free slot (either "never used" or "tombstoned").
//! - Insert the key into that slot.
//! 2. **Delete (`D key`)**:
//! - Search for the key:
//! - If the key exists, mark its slot as "tombstoned."
//! - If the key doesn't exist, do nothing.
#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <iostream>
using u8 = uint8_t;
using i8 = int8_t;
using u16 = uint16_t;
using i16 = int16_t;
using u32 = uint32_t;
using i32 = int32_t;
using u64 = uint64_t;
using i64 = int64_t;
template <typename T> using vec = std::vector<T>;
template <typename T> struct is_string {
static constexpr bool value = false;
};
template <typename Traits, typename Alloc>
struct is_string<std::basic_string<char, Traits, Alloc>> {
static constexpr bool value = true;
};
template <typename T> constexpr bool is_string_like() {
using expr = std::__remove_cvref_t<std::remove_pointer_t<std::decay_t<T>>>;
if constexpr (std::is_same_v<expr, char>)
return true;
return false;
}
template <typename T> constexpr bool is_string_like_v = is_string_like<T>();
template <typename T> constexpr bool is_string_v = is_string<T>::value;
template <typename T>
concept StringType = is_string_v<T> or is_string_like_v<T> ;
enum class opType
{
Unknown = 0,
Add = 1,
Delete = 2
};
template<StringType T>
class parser
{
public:
struct OpTok
{
opType m_Operations = opType::Unknown;
std::string m_Tokens;
OpTok(char const& lOpType, std::string&& lToken)
{
if(lOpType == 'A')
m_Operations = opType::Add;
else if(lOpType == 'D')
m_Operations = opType::Delete;
m_Tokens = std::move(lToken);
}
friend std::ostream& operator<<(std::ostream &lOutStream, OpTok const& lOutData)
{
lOutStream << '[';
switch(lOutData.m_Operations)
{
case opType::Add:
lOutStream << "Add";
break;
case opType::Delete:
lOutStream << "Delete";
break;
default:
lOutStream << "Unknown";
break;
}
lOutStream << ", " << lOutData.m_Tokens << ']';
return lOutStream;
}
};
private:
static constexpr auto m_IsValidOpr = [](const char &lRefOpr) -> bool {
return (('A' == lRefOpr) or ('D' == lRefOpr));
};
static constexpr auto m_IsValidTok = [](const std::string &lRefTok) -> bool {
return ((10 >= lRefTok.size()) and
std::all_of(lRefTok.begin(), lRefTok.end(),
[](char const &lTokChar) { return lTokChar >= 'a' and lTokChar <= 'z'; }));
};
vec<OpTok> m_vOpTokens;
public:
parser(T const &lBuffer) { tokenizer(lBuffer); }
void tokenizer(T const& lBuffer)
{
std::stringstream lStringStream = [&]() -> std::stringstream
{
if constexpr(is_string_v<T>)
return std::stringstream(lBuffer);
else
return std::stringstream(std::string(lBuffer));
}();
while(lStringStream.good())
{
std::string lTmpTok;
lStringStream >> lTmpTok;
if(lTmpTok.size() <= 1)
continue;
const char lOpType = lTmpTok[0];
lTmpTok = lTmpTok.substr(1);
if(m_IsValidOpr(lOpType) and m_IsValidTok(lTmpTok))
{
m_vOpTokens.emplace_back(lOpType, std::move(lTmpTok));
}
}
}
friend std::ostream& operator<<(std::ostream& lOutStream, parser const& lOutData)
{
for(auto const& lRefOpToks : lOutData.m_vOpTokens)
lOutStream << lRefOpToks << ' ';
return lOutStream;
}
inline i32 size() const { return m_vOpTokens.size(); }
inline auto begin() const { return m_vOpTokens.begin(); }
inline auto end() const { return m_vOpTokens.end(); }
};
template<StringType T = const char *>
class HashTable
{
private:
enum class TableState
{
NeverUsed = 0,
TombStoned = 1,
Occupied = 2
};
struct HashCell
{
TableState m_CellState = TableState::NeverUsed;
std::string m_CellData;
};
static constexpr u32 HASH_TABLE_SIZE = 26u;
HashCell m_DataTable[HASH_TABLE_SIZE];
parser<T> m_ParsedData;
public:
HashTable(T const& lDataToParse) : m_ParsedData(lDataToParse) {}
HashTable() : m_ParsedData("") {}
i32 Find(std::string const& lTableKey) const
{
i32 lTableIdx = lTableKey.back() - 'a';
i32 lMaxItrs = HASH_TABLE_SIZE;
while(lMaxItrs > 0)
{
if (m_DataTable[lTableIdx].m_CellState != TableState::Occupied or
m_DataTable[lTableIdx].m_CellData == lTableKey)
return lTableIdx;
else
lTableIdx = (lTableIdx + 1) % HASH_TABLE_SIZE;
--lMaxItrs;
}
return -1;
}
bool Add(std::string const& lTableKey)
{
i32 lTableIdx = Find(lTableKey);
if(lTableIdx >= 0)
{
m_DataTable[lTableIdx] = {TableState::Occupied, lTableKey};
return true;
}
return false;
}
bool Delete(std::string const& lTableKey)
{
i32 lTableIdx = Find(lTableKey);
if(lTableIdx >= 0)
{
if(TableState::Occupied == m_DataTable[lTableIdx].m_CellState)
m_DataTable[lTableIdx].m_CellState = TableState::TombStoned;
return true;
}
return false;
}
HashTable<T>& TokenizeData(T const& lDataToParse)
{
m_ParsedData.tokenizer(lDataToParse);
return *this;
}
HashTable& ExecuteTokenizedData()
{
for(auto const& lRefParsedData : m_ParsedData)
{
switch (lRefParsedData.m_Operations)
{
case opType::Add:
std::cout << "Add: " << lRefParsedData.m_Tokens << ' ' << std::boolalpha << Add(lRefParsedData.m_Tokens) << '\n';
break;
case opType::Delete:
std::cout << "Delete: " << lRefParsedData.m_Tokens << ' ' << std::boolalpha << Delete(lRefParsedData.m_Tokens) << '\n';
break;
default:
std::cout << "Error: Unknown type provided...\n";
break;
}
}
return *this;
}
friend std::ostream& operator<<(std::ostream &lOutStream, HashTable const& lOutData)
{
lOutStream << "========== HashTable internal parser state ==========\n";
lOutStream << lOutData.m_ParsedData << '\n';
i32 lIndex = 0;
lOutStream << "============== HashTable internal state ==============\n";
for(auto const& lRefExecutedState : lOutData.m_DataTable)
{
lOutStream << '[' << std::setw(2) << lIndex << "]:: [" << std::setw(11);
switch(lRefExecutedState.m_CellState)
{
case TableState::NeverUsed:
lOutStream << "NeverUsed";
break;
case TableState::TombStoned:
lOutStream << "Tombstoned";
break;
case TableState::Occupied:
lOutStream << "Occupied";
break;
}
lOutStream << ", " << std::setw(10) << lRefExecutedState.m_CellData << "]\n";
++lIndex;
}
return lOutStream;
}
};
template<StringType T = const char *>
class HashTableBuilder
{
private:
std::string m_InitialData;
bool m_ExecuteTokens = false;
public:
// Method to set initial data
HashTableBuilder& withInitialData(T const& lInitData)
{
if constexpr(is_string_v<T>)
m_InitialData = lInitData;
else
m_InitialData = std::string(lInitData);
return *this;
}
HashTableBuilder& ExecuteTokenizedData(bool const& lOptVal)
{
m_ExecuteTokens = lOptVal;
return *this;
}
// Build method to create the HashTable object
HashTable<std::string> build() const
{
if(m_ExecuteTokens)
return HashTable<std::string>(m_InitialData).ExecuteTokenizedData();
else
return HashTable<std::string>(m_InitialData);
}
};
int main(void)
{
const char *lPtrTestString = "Aapple Agrape Dapple Astrawberry Aorange";
HashTable lTableObj = HashTableBuilder()
.withInitialData(lPtrTestString)
.ExecuteTokenizedData(true)
.build();
std::cout << lTableObj;
return 0;
}