-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.h
More file actions
76 lines (55 loc) · 1.99 KB
/
HashTable.h
File metadata and controls
76 lines (55 loc) · 1.99 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
#ifndef HASHTABLE_H
#define HASHTABLE_H
// Hash table implementation using separate chaining
// This header file defines the HashTable class, which provides methods for
// inserting, retrieving, and removing key-value pairs. It also includes
// methods for calculating the load factor and resizing the hash table when
// necessary. The hash table uses an array of linked lists to handle
// collisions, and it employs a hash function to map keys to bucket indices.
#include <iostream>
using namespace std;
class HashTable {
friend ostream &operator<<(ostream &os, const HashTable &ht);
private:
// The array of prime numbers is used to determine the size of the hash table
static const int HASH_SIZES[];
// Load factor threshold is used to determine when to rehash the hash table
static const double LOAD_FACTOR_THRESHOLD;
// Number of buckets in the hash table
int buckets;
// Number of elements in the hash table
int count;
// Each bucket in the hash table is represented by a linked list
struct Node {
int key;
int value;
Node *next;
};
// Array of pointers to the head of each linked list (bucket)
Node **table;
// Hash function to map keys to bucket indices
int hash_function(int key);
// Rehash the hash table when the load factor exceeds a certain threshold
void rehash();
public:
// Constructor to initialize the hash table with a default size
HashTable();
// Destructor to clean up memory
~HashTable();
// Copy constructor
HashTable(const HashTable &other);
// Assignment operator
HashTable &operator=(const HashTable &other);
// Get the current size of the hash table
int size() const;
// Calculate the load factor of the hash table
double load_factor() const;
// Insert a key-value pair into the hash table
void insert(int key, int value);
// Retrieve a value by key from the hash table
// Returns -1 if the key is not found
int get(int key);
// Remove a key-value pair from the hash table
void remove(int key);
};
#endif