-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.h
More file actions
144 lines (119 loc) · 2.5 KB
/
HashMap.h
File metadata and controls
144 lines (119 loc) · 2.5 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
#ifndef U05_HASH_HASHMAP_HASHMAP_H_
#define U05_HASH_HASHMAP_HASHMAP_H_
#include "HashEntry.h"
template <class K, class T>
class HashMap
{
private:
HashEntry<K, T> **tabla;
unsigned int tamanio;
static unsigned int hashFunc(K clave);
unsigned int (*hashFuncP)(K clave);
public:
explicit HashMap(unsigned int k);
HashMap(unsigned int k, unsigned int (*hashFuncP)(K clave));
T get(K clave);
void put(K clave, T valor);
void remove(K clave);
~HashMap();
bool esVacio();
void print();
};
template <class K, class T>
HashMap<K, T>::HashMap(unsigned int k)
{
tamanio = k;
tabla = new HashEntry<K, T> *[tamanio];
for (int i = 0; i < tamanio; i++)
{
tabla[i] = NULL;
}
hashFuncP = hashFunc;
}
template <class K, class T>
HashMap<K, T>::HashMap(unsigned int k, unsigned int (*fp)(K))
{
tamanio = k;
tabla = new HashEntry<K, T> *[tamanio];
for (int i = 0; i < tamanio; i++)
{
tabla[i] = NULL;
}
hashFuncP = fp;
}
template <class K, class T>
HashMap<K, T>::~HashMap()
{
for (int i = 0; i < tamanio; i++)
{
if (tabla[i] != NULL)
{
delete tabla[i];
}
}
}
template <class K, class T>
T HashMap<K, T>::get(K clave)
{
unsigned int pos = hashFuncP(clave) % tamanio;
if (tabla[pos] == NULL)
{
throw 404;
}
if(tabla[pos]->getClave() == clave){
return tabla[pos]->getValor();
}else{
throw 409;
}
}
template <class K, class T>
void HashMap<K, T>::put(K clave, T valor)
{
unsigned int pos = hashFuncP(clave) % tamanio;
if (tabla[pos] != NULL)
{
//Manejar la Colision!!!!!!!
throw 409;
}
tabla[pos] = new HashEntry<K, T>(clave, valor); //Corresponde a una fila en la tabla HASH
}
template <class K, class T>
void HashMap<K, T>::remove(K clave) {}
template <class K, class T>
bool HashMap<K, T>::esVacio()
{
for (int i = 0; i < tamanio; i++)
{
if (tabla[i] != NULL)
{
return false;
}
}
return true;
}
template <class K, class T>
unsigned int HashMap<K, T>::hashFunc(K clave)
{
return (unsigned int)clave;
}
template <class K, class T>
void HashMap<K, T>::print()
{
std::cout << "i"
<< " "
<< "Clave"
<< "\t\t"
<< "Valor" << std::endl;
std::cout << "--------------------" << std::endl;
for (int i = 0; i < tamanio; i++)
{
std::cout << i << " ";
if (tabla[i] != NULL)
{
std::cout << tabla[i]->getClave() << "\t\t";
std::cout << tabla[i]->getValor();
}
std::cout << std::endl;
}
}
#endif // U05_HASH_HASHMAP_HASHMAP_H_