-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
141 lines (126 loc) · 4.24 KB
/
hash.cpp
File metadata and controls
141 lines (126 loc) · 4.24 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
#include <iostream>
#include <string>
#include "hash.h"
using namespace std;
// Constructor
HashTable::HashTable(int k) {
// Initialize the table with k slots
table_size = k;
table = new Node*[table_size];
// Initialize each slot to nullptr
for (int i = 0; i < table_size; ++i) {
table[i] = nullptr;
}
}
// Destructor
HashTable::~HashTable() {
// Delete all the nodes in the table
for (int i = 0; i < table_size; ++i) {
Node* current = table[i];
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
}
delete[] table;
}
// Hash function
int HashTable::hash_function(string text) {
// initialize the hash value
unsigned long hash = 0;
// loop through each character in the text
for (char c : text) {
// shift the hash value left by 15 bits, add the character
hash = (hash << 15) + hash + c;
// xor the hash value with the hash value shifted right by 23 bits
hash ^= (hash >> 23);
}
// return the hash value modulo the table size
return static_cast<int>(hash % table_size);
}
// Insert a key into the hash table
void HashTable::insert(string key) {
// initialize the index to the hash value modulo the table size
int index = hash_function(key) % table_size;
string value = key;
// create a new node with the key and value
Node* new_node = new Node(key);
// insert the new node at the beginning of the slot
new_node->next = table[index];
table[index] = new_node;
}
// Print the contents of the first 5 slots
void HashTable::print_first_five_slots() {
// loop through the first 5 slots
for (int i = 0; i < 5 && i < table_size; ++i) {
// print the slot number
cout << "Slot " << i << ":";
// initialize the current node to the head of the slot
Node* current = table[i];
// if the slot is empty, print a newline
if (current == nullptr) {
cout << endl;
} else { // otherwise, print the keys in the slot
while (current != nullptr) {
cout << " " << current->key;
current = current->next;
}
cout << endl;
}
}
}
// Print the lengths of the slots
void HashTable::print_slot_lengths() {
// loop through each slot
for (int i = 0; i < table_size; ++i) {
// initialize the length of the slot to 0 and the current node to the head of the slot
int length = 0;
Node* current = table[i];
// count the number of nodes in the slot
while (current != nullptr) {
length++;
current = current->next;
}
// print the slot number and the length
cout << "Slot " << i << ": " << length << endl;
}
}
// Print the standard deviation
void HashTable::print_standard_deviation() {
// initialize the mean, variance, and standard deviation
double mean = 0.0;
double variance = 0.0;
double standard_deviation = 0.0;
// loop through each slot
for (int i = 0; i < table_size; ++i) {
// initialize the length of the slot to 0 and the current node to the head of the slot
int length = 0;
Node* current = table[i];
// count the number of nodes in the slot
while (current != nullptr) {
length++;
current = current->next;
}
// add the length to the average length
mean += length;
}
// calculate the average length
mean = mean / table_size;
// loop through each slot
for (int i = 0; i < table_size; ++i) {
// initialize the length of the slot to 0 and the current node to the head of the slot
int length = 0;
Node* current = table[i];
// count the number of nodes in the slot
while (current != nullptr) {
length++;
current = current->next;
}
// add the squared difference between the length and the mean to the variance
variance += pow((length - mean),2);
}
// calculate the standard deviation from the variance
standard_deviation = sqrt(variance / table_size);
cout << standard_deviation << endl;
}