-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashQuad.cpp
74 lines (64 loc) · 1.29 KB
/
HashQuad.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
#include "HashQuad.h"
#include <malloc.h>
HashTable InitializeTable(int TableSize)
{
HashTable H;
int i;
if (TableSize < MIN_TABLE_SIZE)
{
return NULL;
}
H = (HashTable)malloc(sizeof(struct HashTbl));
if (H != NULL)
{
H->TableSize = NextPrime(H->TableSize);
H->TheCells = (Cell*)malloc(sizeof(Cell) * H->TableSize);
if (H->TheCells != NULL)
{
for (i = 0; i < H->TableSize; i++)
{
H->TheCells[i].Info = Empty;
}
}
}
return H;
}
Position Find(ElementType Key, HashTable H)
{
Position CurrentPos;
int CollisionNum = 0;
CurrentPos = Hash(Key, H->TableSize);
while (H->TheCells[CurrentPos].Info != Empty &&
H->TheCells[CurrentPos].Element != Key)
{
CurrentPos += 2 * ++CollisionNum - 1;
if (CurrentPos >= H->TableSize)
CurrentPos -= H->TableSize;
}
return CurrentPos;
}
void Insert(ElementType Key, HashTable H)
{
Position Pos;
Pos = Find(Key, H);
if (H->TheCells[Pos].Info != Legitimate)
{
H->TheCells[Pos].Info = Legitimate;
H->TheCells[Pos].Element = Key;
}
}
HashTable ReHash(HashTable H)
{
int i, OldSize;
Cell* OldCells;
OldCells = H->TheCells;
OldSize = H->TableSize;
H = InitializeTable(2 * OldSize);
for (i = 0; i < OldSize; i++)
{
if (OldCells[i].Info == Legitimate)
Insert(OldCells[i].Element, H);
free(OldCells + i);
}
return H;
}