-
Notifications
You must be signed in to change notification settings - Fork 1
/
htab_lookup_add.c
52 lines (47 loc) · 1.22 KB
/
htab_lookup_add.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
/*
* Řešení IJC-DU2
* Autor: Tomáš Matuš, FIT VUT Brno
* Login: xmatus37
* Datum: 19.4.2021
* Přeloženo: gcc 10.2.0
*/
#include <stdlib.h>
#include <string.h>
#include "htab.h"
#include "htab_struct.h"
htab_pair_t * htab_lookup_add(htab_t * t, htab_key_t key) {
htab_pair_t *itm = htab_find(t, key);
if (itm != NULL) {
return itm;
}
// init new item
struct htab_item *newItm = malloc(sizeof(struct htab_item));
if (newItm == NULL) {
return NULL;
}
// allocate length of key + \0 because strlen() excludes that
size_t keySize = strlen(key) + 1;
newItm->pair.key = malloc(keySize);
if (newItm->pair.key == NULL) {
free(newItm);
return NULL;
}
memcpy((char*)newItm->pair.key, key, keySize);
newItm->next = NULL;
newItm->pair.value = 0;
// calculate index of new key
size_t index = htab_hash_function(key) % t->arr_size;
// empty index
if (t->arr[index] == NULL) {
t->arr[index] = newItm;
} else {
struct htab_item *tmp;
tmp = t->arr[index];
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = newItm;
}
t->size++;
return &newItm->pair;
}