-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.c
54 lines (47 loc) · 1.08 KB
/
HashTable.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "HashTable.h"
u32 hash(u32 nombre_real, hash_table ht)
{
return nombre_real % ht->size;
};
hash_table new_ht(u32 size)
{
hash_table new_ht = malloc(sizeof(struct hash_table_s));
new_ht->size = size;
new_ht->ocupation = 0;
new_ht->buckets = calloc(size, sizeof(list));
for (u32 i = 0; i < size; ++i)
{
new_ht->buckets[i] = new_list();
}
return new_ht;
};
void destroy_ht(hash_table ht)
{
for (u32 i = 0; i < ht->size; ++i)
destroy_list(ht->buckets[i]);
free(ht->buckets);
free(ht);
}
bool in_ht(u32 key, hash_table ht)
{
return in_list(key, ht->buckets[hash(key, ht)]);
}
void ht_put(u32 key, u32 val, hash_table ht)
{
u32 hsh = hash(key, ht);
ht->buckets[hsh] = addl(key, val, ht->buckets[hsh]);
ht->ocupation += 1;
}
void ht_delete_bucket(u32 key, hash_table ht)
{
destroy_list(ht->buckets[key]);
ht->buckets[key] = new_list();
}
u32 ht_get(u32 key, hash_table ht)
{
u32 hsh = hash(key, ht);
return search(key, ht->buckets[hsh]);
};