-
Notifications
You must be signed in to change notification settings - Fork 0
/
htab_basic.c
38 lines (30 loc) · 876 Bytes
/
htab_basic.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
// htab_basic.c
// Řešení IJC-DU2, příklad 2), 30.3. 2023
// Autor: Jakub Antonín Štigler, FIT
// Přeloženo: clang 15.0.7
// Funguje s gcc 12.2.1
// C standard: C11
#include "_htab.h" // size_t, htab_t, NULL
#include <stdlib.h> // calloc, malloc, free
#include "htab_primes.h" // get_prime
htab_t *htab_init(const size_t n) {
htab_t *tab = malloc(sizeof(*tab));
if (!tab)
return NULL;
tab->size = 0;
// the size of the table is always prime to ensure better distribution
// even for poor hashing functions
tab->arr_size = get_prime(n);
tab->arr_ptr = calloc(tab->arr_size, sizeof(*tab->arr_ptr));
if (!tab->arr_ptr) {
free(tab);
return NULL;
}
return tab;
}
size_t htab_size(const htab_t *t) {
return t->size;
}
size_t htab_bucket_count(const htab_t *t) {
return t->arr_size;
}