-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtab_lookup_add.c
81 lines (64 loc) · 1.6 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
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
/**
* @file htab_init.c
* @author Samuel Stolarik (xstola03)
* @brief 2nd part of DU2 IJC
*
* @date 2022-04-10
*
*/
#include "htab.h"
#include "p_htab.h"
#include <stdbool.h>
#include <string.h>
htab_item_t * insert_first(htab_item_t ** head, htab_key_t key);
htab_pair_t * htab_lookup_add(htab_t * t, htab_key_t key)
{
size_t index = htab_hash_function(key) % t -> arr_size;
htab_item_t * cur = t -> ptr[index];
bool found = false;
//walk the list
while (cur != NULL)
{
if (strcmp(key, cur -> pair.key) == 0)
{
found = true;
cur -> pair.value += 1;
break;
}
cur = cur -> next;
}
if (found == true){
return &cur -> pair;
}
htab_item_t * new = insert_first(&t -> ptr[index], key);
if (new == NULL){
return NULL;
}
t -> size += 1; //number of records in the htab
if (t -> size / t -> arr_size > AVG_LEN_MAX){
htab_resize(t, t -> arr_size * 2);
}
return &new -> pair;
}
htab_item_t * insert_first(htab_item_t ** head, htab_key_t key)
{
if (key == NULL){
return NULL;
}
htab_item_t * new = malloc(sizeof(htab_item_t));
if (new == NULL){
return NULL;
}
htab_item_t * tmp = *head;
*head = new;
new -> next = tmp;
char * new_key = malloc(strlen(key) + 1); //one byte for the terminating '\0'
if (new_key == NULL){
free(new);
return NULL;
}
memcpy(new_key, key, strlen(key) + 1); //same as above
new -> pair.key = new_key;
new -> pair.value = 1;
return new;
}