-
Notifications
You must be signed in to change notification settings - Fork 1
/
htab_find.c
39 lines (34 loc) · 905 Bytes
/
htab_find.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
/*
* Řešení IJC-DU2
* Autor: Tomáš Matuš, FIT VUT Brno
* Login: xmatus37
* Datum: 19.4.2021
* Přeloženo: gcc 10.2.0
*/
#include <string.h>
#include "htab.h"
#include "htab_struct.h"
htab_pair_t * htab_find(htab_t * t, htab_key_t key) {
size_t index = htab_hash_function(key) % t->arr_size;
struct htab_item *itm = t->arr[index];
// empty index
if (itm == NULL) {
return NULL;
}
// compare keys with first item on index
if (strcmp(itm->pair.key, key) == 0) {
// keys match
return &itm->pair;
} else {
// run through linked list and search for key
for (; itm->next != NULL; itm = itm->next) {
// compare keys
if (strcmp(itm->pair.key, key) == 0) {
// key in list matches
return &itm->pair;
}
}
}
// key not found
return NULL;
}