-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashTable.cpp
More file actions
71 lines (61 loc) · 1.78 KB
/
Copy pathMyHashTable.cpp
File metadata and controls
71 lines (61 loc) · 1.78 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
//
// Created by Diego Alberto Ortiz Mariscal A01552000, Diego Mojarro A01638460,
// Luis Armando Salazar A01114901 on 11/26/20.
//
#include "MyHashTable.h"
#include <cmath>
using namespace std;
MyHashTable::MyHashTable() {
this->size = 0;
this->sizeA = 7069;
this->tabla = new MyLinkedList[this->sizeA];
}
MyHashTable::~MyHashTable() {
delete[] this->tabla;
}
bool MyHashTable::isEmpty() const {
return this->size == 0;
}
void MyHashTable::rehashing() {
MyLinkedList *tempTabla = this->tabla;
this->sizeA = ((this->sizeA * 2) + 1);
this->size = 0;
this->tabla = new MyLinkedList[this->sizeA];
for (int i = 0; i < (this->sizeA - 1) / 2; i++) {
while (!tempTabla[i].isEmpty()) {
auto head = tempTabla[i].getAt(0);
put(head->key, head->reg);
tempTabla[i].removeFirst();
}
}
delete[] tempTabla;
}
int MyHashTable::getPos(const string &key) const {
size_t hashT = hash<string>{}(key);
int hashCode = static_cast<int>(hashT);
return abs(hashCode) % this->sizeA;
}
void MyHashTable::put(const string &key, RegisterEntry ®) {
double loadFactor = size * 1.0 / sizeA;
if (loadFactor > 0.75) {
rehashing();
put(key, reg);
} else {
int pos = getPos(key);
if (!this->tabla[pos].isRepeated(key)) {
this->tabla[pos].insertFirst(key, reg);
this->size++;
} else {
this->tabla[pos].getAt(key)->addRegister(reg.getDateString(), reg.getDateInt());
}
}
}
RegisterEntry MyHashTable::get(const string &key) {
int pos = getPos(key);
MyLinkedList *lista = &this->tabla[pos];
return *lista->getAt(key);
}
void MyHashTable::remove(const string &key) {
int pos = getPos(key);
this->tabla[pos].removeAt(key);
}