aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/trie.c
blob: 2e6a0d82a9d212219b85b645dea8a86464715b2b (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>
#include "utils.h"


b_trie_t*
b_trie_new(b_free_func_t free_func)
{
    b_trie_t *trie = b_malloc(sizeof(b_trie_t));
    trie->root = NULL;
    trie->free_func = free_func;
    return trie;
}


static void
b_trie_free_node(b_trie_t *trie, b_trie_node_t *node)
{
    if (node == NULL)
        return;
    if (node->data != NULL && trie->free_func != NULL)
        trie->free_func(node->data);
    b_trie_free_node(trie, node->next);
    b_trie_free_node(trie, node->child);
    free(node);
}


void
b_trie_free(b_trie_t *trie)
{
    if (trie == NULL)
        return;
    b_trie_free_node(trie, trie->root);
    free(trie);
}


void
b_trie_insert(b_trie_t *trie, const char *key, void *data)
{
    if (data == NULL || key == NULL)
        return;

    b_trie_node_t *parent = NULL;
    b_trie_node_t *previous;
    b_trie_node_t *current;
    b_trie_node_t *tmp;

    while (1) {

        if (trie->root == NULL || (parent != NULL && parent->child == NULL)) {
            current = b_malloc(sizeof(b_trie_node_t));
            current->key = *key;
            current->data = NULL;
            current->next = NULL;
            current->child = NULL;
            if (trie->root == NULL)
                trie->root = current;
            else
                parent->child = current;
            parent = current;
            goto clean;
        }

        tmp = parent == NULL ? trie->root : parent->child;
        previous = NULL;

        while (tmp != NULL && tmp->key != *key) {
            previous = tmp;
            tmp = tmp->next;
        }

        parent = tmp;

        if (previous == NULL || parent != NULL)
            goto clean;

        current = b_malloc(sizeof(b_trie_node_t));
        current->key = *key;
        current->data = NULL;
        current->next = NULL;
        current->child = NULL;
        previous->next = current;
        parent = current;

clean:
        if (*key == '\0') {
            if (parent->data != NULL && trie->free_func != NULL)
                trie->free_func(parent->data);
            parent->data = data;
            break;
        }
        key++;
    }
}


void*
b_trie_lookup(b_trie_t *trie, const char *key)
{
    if (trie == NULL || trie->root == NULL || key == NULL)
        return NULL;

    b_trie_node_t *parent = trie->root;
    b_trie_node_t *tmp;
    while (1) {
        for (tmp = parent; tmp != NULL; tmp = tmp->next) {

            if (tmp->key == *key) {
                if (tmp->key == '\0')
                    return tmp->data;
                parent = tmp->child;
                break;
            }
        }
        if (tmp == NULL)
            return NULL;

        if (*key == '\0')
            break;
        key++;
    }
    return NULL;
}


static void
b_trie_size_node(b_trie_node_t *node, unsigned int *count)
{
    if (node == NULL)
        return;

    if (node->key == '\0')
        (*count)++;

    b_trie_size_node(node->next, count);
    b_trie_size_node(node->child, count);
}


unsigned int
b_trie_size(b_trie_t *trie)
{
    if (trie == NULL)
        return 0;

    unsigned int count = 0;
    b_trie_size_node(trie->root, &count);
    return count;
}


static void
b_trie_foreach_node(b_trie_node_t *node, b_string_t *str, void (*func)(const char *key, void *data))
{
    if (node == NULL)
        return;

    if (node->key == '\0') {
        func(str->str, node->data);
        b_string_free(str, true);
    }

    if (node->child != NULL) {
        b_string_t *child = b_string_new();
        child = b_string_append(child, str->str);
        child = b_string_append_c(child, node->key);
        b_trie_foreach_node(node->child, child, func);
    }

    if (node->next != NULL)
        b_trie_foreach_node(node->next, str, func);

    if (node->child != NULL && node->next == NULL)
        b_string_free(str, true);
}


void
b_trie_foreach(b_trie_t *trie, void (*func)(const char *key, void *data))
{
    if (trie->root == NULL)
        return;

    b_string_t *str = b_string_new();
    b_trie_foreach_node(trie->root, str, func);
}