-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsublist.c
108 lines (91 loc) · 2.58 KB
/
sublist.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
/*
* 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 "sublist.h"
#include <common/tree.h>
struct sub_node
{
RB_ENTRY(sub_node) entry;
char *topic;
uint32_t count;
};
static int sub_cmp(struct sub_node *e1, struct sub_node *e2)
{
return strcmp(e1->topic, e2->topic);
}
RB_HEAD(sublist, sub_node) sub_head = RB_INITIALIZER(&sub_head);
RB_PROTOTYPE(sublist, sub_node, entry, sub_cmp);
RB_GENERATE(sublist, sub_node, entry, sub_cmp);
static struct sub_node *sublist_find(char *topic)
{
struct sub_node key;
key.topic = topic;
return RB_FIND(sublist, &sub_head, &key);
}
bool sublist_insert(char *topic)
{
struct sub_node *found;
if (NULL != (found = sublist_find(topic)))
{
found->count++;
return false;
}
else
{
struct sub_node *data;
if (NULL == (data = malloc(sizeof(struct sub_node))))
return false;
if (NULL == (data->topic = strdup(topic)))
{
free(data);
return false;
}
data->count = 1;
if (NULL == RB_INSERT(sublist, &sub_head, data)) // NULL means OK
return true;
else
{
LOG_CRITICAL("sublist_insert sub collision"); // can't happen
return false;
}
}
}
bool sublist_remove(char *topic)
{
struct sub_node key;
struct sub_node *res;
key.topic = topic;
res = RB_FIND(sublist, &sub_head, &key);
if (NULL == res)
return false;
if (--res->count == 0)
{
RB_REMOVE(sublist, &sub_head, res);
free(res->topic);
free(res);
return true;
}
return false;
}
void sublist_foreach(sublist_iterate_func f, void *userdata)
{
struct sub_node *i;
RB_FOREACH(i, sublist, &sub_head)
{
f(i->topic, i->count, userdata);
}
}