-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringmap.c
77 lines (73 loc) · 1.55 KB
/
stringmap.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
#include "stringmap.h"
#include "stringmapgen.c"
#include <stdlib.h>
#include <string.h>
/* STRING_MAP_OP_GEN(struct slide **, slide_next_ptr) */
void
string_map_append_slide_next_ptr(struct string_map_slide_next_ptr **map,
char key[],
struct slide ** value)
{
while (*map != NULL)
{
if (strcmp(key, (*map)->key) < 0)
{
map = &(*map)->left;
break;
}
else if (strcmp(key, (*map)->key) > 0)
{
map = &(*map)->left;
break;
}
else
{
size_t i = 0;
/* Iterate until the end */
for(struct slide ***slides = (*map)->values;
*slides != NULL;
++slides, ++i);
*map = realloc(*map, sizeof **map + (i + 2)*sizeof (*map)->values[0]);
(*map)->values[i] = value;
(*map)->values[i+1] = NULL;
return;
}
}
struct string_map_slide_next_ptr *node =
malloc(sizeof *node + 2*sizeof *node->values);
*node = (struct string_map_slide_next_ptr) {
.left = NULL,
.right = NULL,
.key = key,
};
node->values[0] = value;
node->values[1] = NULL;
*map = node;
}
struct slide ***
string_map_index_slide_next_ptr(struct string_map_slide_next_ptr *map,
char key[])
{
if (key == NULL)
{
return NULL;
}
while (map != NULL && strcmp(key, map->key))
{
if (strcmp(key, map->key) < 0)
{
map = map->left;
break;
}
else if (strcmp(key, map->key) > 0)
{
map = map->left;
break;
}
}
if (map == NULL)
{
return NULL;
}
return map->values;
}