-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
138 lines (119 loc) · 3.27 KB
/
hash.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
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
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SEED 0x12345678
typedef struct {
uintptr_t *table;
int size;
int max;
uintptr_t deleted;
char *(*get_key)(void *);
} thash;
uint32_t hashf(const char *str, uint32_t h) {
/* One-byte-at-a-time Murmur hash
Source: https://github.com/aappleby/smhasher/blob/master/src/Hashes.cpp */
for (; *str; ++str) {
h ^= *str;
h *= 0x5bd1e995;
h ^= h >> 15;
}
return h;
}
int hash_insere(thash *h, void *bucket) {
uint32_t hash = hashf(h->get_key(bucket), SEED);
int pos = hash % (h->max);
int i = 0;
// se esta cheio
if (h->max == (h->size) + 1) {
free(bucket);
return EXIT_FAILURE;
}
// inserindo
while (h->table[pos] != 0) {
if (h->table[pos] == h->deleted)
break;
pos = (hash + i * hash) % h->max;
i += 1;
}
h->table[pos] = (uintptr_t)bucket;
h->size += 1;
return EXIT_SUCCESS;
}
int hash_constroi(thash *h, int nbuckets, char *(*get_key)(void *)) {
h->table = calloc(
sizeof(void *),
nbuckets + 1); // pois para caber 10 elementos precisamos de 11 espaços
if (h->table == NULL)
return EXIT_FAILURE;
h->max = nbuckets + 1;
h->size = 0;
h->deleted = (uintptr_t) & (h->size);
h->get_key = get_key;
return EXIT_SUCCESS;
}
void *hash_busca(thash h, const char *key) {
uint32_t hash = hashf(key, SEED);
int pos = hash % (h.max);
int i = 0;
while (h.table[pos] != 0) {
if (strcmp(h.get_key((void *)h.table[pos]), key) == 0)
return (void *)h.table[pos];
pos = (hash + i * hash) % h.max;
i += 1;
}
return NULL;
}
void **hash_busca_repetidos(thash h, const char *key) {
uint32_t hash = hashf(key, SEED);
int pos = hash % (h.max);
int i = 0;
void **ret = malloc(sizeof(void *));
ret[0] = NULL;
int rep = 0;
while (h.table[pos] != 0) {
if (strcmp(h.get_key((void *)h.table[pos]), key) == 0) {
// se ja tem a cidade na lista, não adiciona
int ja_tem = 0;
for (int j = 0; ret[j] != NULL; j++) {
if (ret[j] == (void *)h.table[pos]) {
ja_tem = 1;
break;
}
}
if (ja_tem == 0) {
ret = realloc(ret, sizeof(void *) * (rep + 1));
ret[rep] = (void *)h.table[pos];
ret[rep + 1] = NULL;
rep += 1;
}
}
pos = (hash + i * hash) % h.max;
i += 1;
}
return ret;
}
int hash_remove(thash *h, const char *key) {
uint32_t hash = hashf(key, SEED);
int pos = hash % (h->max);
int i = 0;
while (h->table[pos] != 0) {
if (strcmp(h->get_key((void *)h->table[pos]), key) == 0) {
h->table[pos] = h->deleted;
h->size -= 1;
return EXIT_SUCCESS;
}
pos = (hash + i * hash) % h->max;
i += 1;
}
return EXIT_FAILURE;
}
void hash_apaga(thash *h) {
int pos;
for (pos = 0; pos < h->max; pos++) {
if (h->table[pos] != 0 && h->table[pos] != h->deleted)
free((void *)h->table[pos]);
}
free(h->table);
}