-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtab_lookup_add.c
71 lines (58 loc) · 1.57 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
// 2) htab_lookup_add.c
// Ondřej Zobal (xzobal01)
// FIT VUT v Brně
// 15.4.2022
#include "htab.h"
#include "htab_t.h"
#include "htab_item_t.h"
#include <stdio.h>
#define AVG_LEN_MAX 3
void htab_la_check_for_resize(htab_t *t) {
if (t->size / t->arr_size > AVG_LEN_MAX){
printf("resize\n");
htab_resize(t, t->size*2);
}
}
htab_pair_t *htab_lookup_add(htab_t * t, htab_key_t key) {
// Computing hash
size_t hash = htab_hash_function(key);
// Looking up the has in the hash table
htab_item_t *items = t->arr_ptr[hash % t->arr_size];
htab_item_t *itemsPrev = NULL;
// Itarating through the list.
while(items != NULL) {
if(!strcmp(items->pair.key, key)) {
return &items->pair;
break;
}
itemsPrev = items;
items = items->next;
}
// no items is either null or just before null.
// lookup was not sucessfull
// creating new item
htab_item_t *newItem = malloc(sizeof(htab_item_t));
if (newItem == NULL) {
exit(1);
}
char *keyString = malloc(sizeof(char) * (strlen(key)+1));
strcpy(keyString, key);
newItem->pair.key = keyString;
newItem->pair.value = 0;
newItem->next = NULL;
t->size++;
if(items != NULL) {
items->next = newItem;
}
else if (itemsPrev != NULL) {
itemsPrev->next = newItem;
}
else {
t->arr_ptr[hash % t->arr_size] = newItem;
}
// Resize if smol.
if (t->size / t->arr_size > AVG_LEN_MAX){
htab_resize(t, t->size*2);
}
return &newItem->pair;
}