summaryrefslogtreecommitdiff
path: root/server/util/btreemap.c
blob: 43646c34c7b30d08602b9a533f10b0c0071d94b7 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 SSSD

 Service monitor

 Copyright (C) Stephen Gallagher	2008

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include "talloc.h"
#include "util/btreemap.h"
#include "util/util.h"

struct btreemap
{
    /* NULL keys are not allowed */
    const void *key;

    /* NULL values are permitted */
    void *value;

    struct btreemap *left;
    struct btreemap *right;

    /* comparator must return -1, 0 or 1
     * and should only be set for the root node
     * other nodes will be ignored.
     */
    btreemap_comparison_fn comparator;
};

/* btreemap_search_key
 * Searches a btreemap for an entry with a specific key
 * If found, it will return BTREEMAP_FOUND and node will
 * be set to the appropriate node.
 * If not found, it will set the following:
 * BTREEMAP_EMPTY: The map was empty, create a new map when adding keys
 * BTREEMAP_CREATE_LEFT: A new node created should use node->left
 * BTREEMAP_CREATE_RIGHT: A new node created should use node->right
 */
int btreemap_search_key(struct btreemap *map,
                        const void *key, struct btreemap **node)
{
    struct btreemap *tempnode;
    int result;
    int found = BTREEMAP_EMPTY;

    if (!map)
    {
        *node = NULL;
        return BTREEMAP_EMPTY;
    }

    tempnode = map;
    while (found == BTREEMAP_EMPTY) {
        result = tempnode->comparator(key, tempnode->key);
        if (result > 0)
        {
            if (tempnode->right)
                tempnode=tempnode->right;
            else
            {
                found = BTREEMAP_CREATE_RIGHT;
            }
        } else if (result < 0)
        {
            if (tempnode->left)
                tempnode=tempnode->left;
            else
            {
                found = BTREEMAP_CREATE_LEFT;
            }
        } else
        {
            /* This entry matched */
            found = BTREEMAP_FOUND;
        }
    }

    *node = tempnode;
    return found;
}

void *btreemap_get_value(struct btreemap *map, const void *key)
{
    struct btreemap *node;
    int found;

    if (!map || !key)
    {
        return NULL;
    }

    /* Search for the key */
    found = btreemap_search_key(map, key, &node);
    if (found == 0)
    {
        return node->value;
    }

    /* If the key was not found, return NULL */
    return NULL;
}

/* Keys and values must be talloc contexts
 * comparator must return -1, 0 or 1
 */
int btreemap_set_value(TALLOC_CTX *mem_ctx,
                       struct btreemap **map, const void *key, void *value,
                       btreemap_comparison_fn comparator)
{
    struct btreemap *node;
    struct btreemap *new_node;
    int found;

    if (!key)
    {
        return EINVAL;
    }

    /* Search for the key */
    found = btreemap_search_key(*map, key, &node);

    if (found == BTREEMAP_FOUND)
    {
        /* Update existing value */
        node->value = value;
        return EOK;
    }

    /* Need to add a value to the tree */
    new_node = talloc_zero(mem_ctx, struct btreemap);

    if (!new_node)
    {
        return ENOMEM;
    }
    new_node->key = key;
    new_node->value = value;
    new_node->comparator = comparator;
    new_node->left = NULL;
    new_node->right = NULL;


    if (found == BTREEMAP_EMPTY)
    {
        *map = new_node;
    }
    if (found == BTREEMAP_CREATE_LEFT)
    {
        node->left = new_node;
    } else if (found == BTREEMAP_CREATE_RIGHT)
    {
        node->right = new_node;
    }
    return EOK;
}

static int _btreemap_get_keys(TALLOC_CTX *mem_ctx, struct btreemap *map,
                              const void ***array, int *count, int depth)
{
    int ret;
    const void **tmp_array;

    if (map == NULL) return EOK;

    /* Left Node */
    ret = _btreemap_get_keys(mem_ctx, map->left, array, count, depth+1);
    if (ret != EOK) {
        return ret;
    }

    /* Current Node */
    (*count)++;
    tmp_array = talloc_realloc(mem_ctx, *array, const void *, *count);
    if (tmp_array == NULL) {
        /* Out of memory */
        *count = 0;
        talloc_free(*array);
        *array = NULL;
        return ENOMEM;
    }
    *array = tmp_array;
    (*array)[(*count)-1] = map->key;

    /* Right Node */
    ret = _btreemap_get_keys(mem_ctx, map->right, array, count, depth+1);
    if (ret != EOK) {
        return ret;
    }

    return EOK;
}
/* Return an array of keys in sort order
 */
int btreemap_get_keys(TALLOC_CTX *mem_ctx,
                      struct btreemap *map,
                      const void ***array, int *count)
{
    *array = NULL;
    *count = 0;
    return _btreemap_get_keys(mem_ctx, map, array, count, 0);
}