-
Notifications
You must be signed in to change notification settings - Fork 0
/
TablaHashAbierto.cpp
109 lines (95 loc) · 2.46 KB
/
TablaHashAbierto.cpp
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
#ifndef TablaHashAbierto_CPP
#define TablaHashAbierto_CPP
#include "TablaLista.h"
#include "TablaHashAbierto.h"
#include "TablaHashAbiertoIteracion.h"
template <class C, class V, typename FHash>
TablaHashAbierto<C,V,FHash>::TablaHashAbierto(nat buckets, float fc)
{
if (buckets % 2 == 0)
buckets++;
arreglo = Array<Puntero<Tabla<C,V>>>(buckets);
largo = 0;
this->fc = fc;
for (nat i = 0; i < buckets; i++)
arreglo[i] = new TablaLista<C,V>();
}
template <class C, class V, typename FHash>
void TablaHashAbierto<C,V,FHash>::BorrarTodos()
{
for (nat i = 0; i < arreglo.Largo; i++)
arreglo[i]->BorrarTodos();
}
template <class C, class V, typename FHash>
bool TablaHashAbierto<C,V,FHash>::Agregar(const C& c, const V& v)
{
nat pos = H(c);
if (arreglo[pos]->Agregar(c, v))
{
assert(!EstaLlena());
largo++;
return true;
}
return false;
}
template <class C, class V, typename FHash>
bool TablaHashAbierto<C,V,FHash>::Pertenece(const C& c) const
{
nat pos = H(c);
return arreglo[pos]->Pertenece(c);
}
template <class C, class V, typename FHash>
const V& TablaHashAbierto<C,V,FHash>::Obtener(const C& c) const
{
assert(Pertenece(c));
nat pos = H(c);
return arreglo[pos]->Obtener(c);
}
template <class C, class V, typename FHash>
bool TablaHashAbierto<C,V,FHash>::IntentarObtener(const C& c, V& v) const
{
nat pos = H(c);
return arreglo[pos]->IntentarObtener(c, v);
}
template <class C, class V, typename FHash>
bool TablaHashAbierto<C,V,FHash>::Borrar(const C& c)
{
assert(Pertenece(c));
nat pos = H(c);
if (arreglo[pos]->Borrar(c))
{
largo--;
return true;
}
return false;
}
template <class C, class V, typename FHash>
Iterador<Tupla<C,V>> TablaHashAbierto<C,V,FHash>::ObtenerIterador() const
{
return new TablaHashAbiertoIteracion<C,V>(arreglo);
}
template <class C, class V, typename FHash>
Puntero<Tabla<C,V>> TablaHashAbierto<C,V,FHash>::Clonar() const
{
Puntero<Tabla<C,V>> tabla = new TablaHashAbierto<C,V,FHash>(arreglo.Largo, fc);
foreach (auto t, this)
tabla->Agregar(t.Dato1, t.Dato2);
return tabla;
}
template <class C, class V, typename FHash>
bool TablaHashAbierto<C,V,FHash>::EstaLlena() const
{
return fc < (static_cast<float>(Largo()) + 1) / arreglo.Largo;
}
template <class C, class V, typename FHash>
nat TablaHashAbierto<C,V,FHash>::Largo() const
{
return largo;
}
template <class C, class V, typename FHash>
nat TablaHashAbierto<C,V,FHash>::H(const C& c) const
{
const FuncionHash<C>& fHash = FHash();
return fHash(c) % arreglo.Largo;
}
#endif