-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_main.c
105 lines (86 loc) · 2.17 KB
/
hash_main.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "src/ed/hash.h"
typedef struct
{
int x, y;
} Celula;
Celula *celula_create(int x, int y)
{
Celula *c = malloc(sizeof(Celula));
c->x = x;
c->y = y;
return c;
}
void celula_destroy(Celula *c)
{
free(c);
}
void hash_key_destroy(void *a) {
Celula *c = (Celula *)a;
celula_destroy(c);
}
int celula_hash(HashTable *h, void *key)
{
Celula *c = (Celula *)key;
// 83 e 97 sao primos e o operador "^" é o XOR bit a bit
return ((c->x * 83) ^ (c->y * 97)) % hash_table_size(h);
}
int celula_cmp(void *c1, void *c2)
{
HashTableItem *d1 = (HashTableItem *)c1;
Celula *a = (Celula *)d1->key;
Celula *b = (Celula *)c2;
if (a->x == b->x && a->y == b->y) {
return 0;
}
else
return 1;
}
int main()
{
int i, n, x, y;
char cmd[10];
HashTable *h = hash_table_construct(19, celula_hash, celula_cmp, hash_key_destroy, free);
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("\n%s", cmd);
if (!strcmp(cmd, "SET"))
{
int *pos = malloc(sizeof(int));
scanf("%d %d %d", &x, &y, pos);
Celula *cel = celula_create(x, y);
void *prev = hash_table_set(h, cel, pos);
// se o par ja existia, podemos liberar a celula e a posicao antiga
if (prev)
{
free(prev);
celula_destroy(cel);
}
}
else if (!strcmp(cmd, "GET"))
{
scanf("%d %d", &x, &y);
Celula *cel = celula_create(x, y);
int *pos = (int *)hash_table_get(h, cel);
printf("%d\n", *pos);
celula_destroy(cel);
}
}
/*
HashTableIterator *it = hash_table_iterator(h);
while (!hash_table_iterator_is_over(it))
{
printf("Cheguei\n");
HashTableItem *item = hash_table_iterator_next(it);
Celula *cel = (Celula *)item->key;
int *pos = (int *)item->val;
celula_destroy(cel);
free(pos);
}
hash_table_iterator_destroy(it);*/
hash_table_destroy(h);
return 0;
}