-
Notifications
You must be signed in to change notification settings - Fork 19
/
idset.c
142 lines (119 loc) · 3.16 KB
/
idset.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
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
/*
* Copyright (c) 2012, Toby Jaffey <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <common/common.h>
#include <common/logging.h>
#include <common/util.h>
#include "idset.h"
#include <common/tree.h>
static int id_cmp(struct idset_node *e1, struct idset_node *e2)
{
return (e1->id < e2->id ? -1 : e1->id > e2->id);
}
RB_GENERATE(idset, idset_node, entry, id_cmp);
void idset_destroy(idset_t *ids)
{
if (NULL != ids)
{
struct idset_node *var;
struct idset_node *nxt;
for (var = RB_MIN(idset, &ids->root); var != NULL; var = nxt)
{
nxt = RB_NEXT(idset, &ids->root, var);
RB_REMOVE(idset, &ids->root, var);
free(var);
}
free(ids);
}
}
idset_t *idset_create(void)
{
idset_t *ids;
if (NULL == (ids = (idset_t *)calloc(sizeof(idset_t), 1)))
goto fail;
RB_INIT(&ids->root);
return ids;
fail:
idset_destroy(ids);
return NULL;
}
int idset_insert(idset_t *ids, uint32_t id)
{
struct idset_node *data;
if (NULL == (data = malloc(sizeof(struct idset_node))))
return 1;
data->id = id;
if (NULL == RB_INSERT(idset, &ids->root, data)) // NULL means OK
{
ids->count++;
return 0;
}
else
{
LOG_ERROR("idset_insert id collision");
crash();
free(data);
return 1;
}
}
int idset_remove(idset_t *ids, uint32_t id)
{
struct idset_node key;
struct idset_node *res;
key.id = id;
res = RB_FIND(idset, &ids->root, &key);
if (NULL == res)
return 1;
RB_REMOVE(idset, &ids->root, res);
free(res);
ids->count--;
return 0;
}
bool idset_find(idset_t *ids, uint32_t id)
{
struct idset_node key;
struct idset_node *res;
key.id = id;
res = RB_FIND(idset, &ids->root, &key);
if (NULL == res)
return false;
return true;
}
void idset_foreach(idset_t *ids, idset_iterate_func f, void *userdata)
{
struct idset_node *var, *nxt;
// can't use foreach if we want to allow deletion
for (var = RB_MIN(idset, &ids->root); var != NULL; var = nxt)
{
nxt = RB_NEXT(idset, &ids->root, var);
if (f(ids, var->id, userdata))
{
RB_REMOVE(idset, &ids->root, var);
free(var);
}
}
}
void idset_print(idset_t *ids)
{
struct idset_node *i;
RB_FOREACH(i, idset, &ids->root)
{
LOG_INFO("%u", i->id);
}
}
size_t idset_size(idset_t *ids)
{
return ids->count;
}