-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtab_resize.c
54 lines (45 loc) · 1.18 KB
/
htab_resize.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
/**
* @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"
void htab_resize(htab_t *t, size_t newn)
{
if (t == NULL){
return;
}
if (newn <= 0){
htab_free(t);
}
if (newn == t -> arr_size){
return;
}
htab_t * tmp = htab_init(newn);
if (tmp == NULL){
return;
}
//transfer all items into the new table with diff size
for (int i = 0; i < t -> arr_size; i++){
htab_item_t * cur_item = t -> ptr[i];
while (cur_item != NULL)
{
htab_lookup_add(tmp, cur_item -> pair.key); //insert into the table once
htab_find(tmp, cur_item -> pair.key) -> value = cur_item -> pair.value; //set the right value
cur_item = cur_item -> next;
}
}
//free old htab keys and array_ptr
htab_clear(t);
free(t -> ptr);
//move new array_ptr into the old htab
t -> ptr = tmp -> ptr;
t -> arr_size = tmp -> arr_size;
//delete the temporary table
free(tmp); //not freeing tmp -> ptr because that had been assigned to the old htab
return;
}