summaryrefslogtreecommitdiffstats
path: root/ccan/ccan/strset/strset.h
blob: 9d6f1ae343f5b4904e3eaf725d26c6a89cc7f331 (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
#ifndef CCAN_STRSET_H
#define CCAN_STRSET_H
#include "config.h"
#include <ccan/typesafe_cb/typesafe_cb.h>
#include <stdlib.h>
#include <stdbool.h>

/**
 * struct strset - representation of a string set
 *
 * It's exposed here to allow you to embed it and so we can inline the
 * trivial functions.
 */
struct strset {
	union {
		struct node *n;
		const char *s;
	} u;
};

/**
 * strset_init - initialize a string set (empty)
 *
 * For completeness; if you've arranged for it to be NULL already you don't
 * need this.
 *
 * Example:
 *	struct strset set;
 *
 *	strset_init(&set);
 */
static inline void strset_init(struct strset *set)
{
	set->u.n = NULL;
}

/**
 * strset_empty - is this string set empty?
 * @set: the set.
 *
 * Example:
 *	if (!strset_empty(&set))
 *		abort();
 */
static inline bool strset_empty(const struct strset *set)
{
	return set->u.n == NULL;
}

/**
 * strset_get - is this a member of this string set?
 * @set: the set.
 * @member: the string to search for.
 *
 * Returns the member, or NULL if it isn't in the set (and sets errno
 * = ENOENT).
 *
 * Example:
 *	if (strset_get(&set, "hello"))
 *		printf("hello is in the set\n");
 */
char *strset_get(const struct strset *set, const char *member);

/**
 * strset_add - place a member in the string set.
 * @set: the set.
 * @member: the string to place in the set.
 *
 * This returns false if we run out of memory (errno = ENOMEM), or
 * (more normally) if that string already appears in the set (EEXIST).
 *
 * Note that the pointer is placed in the set, the string is not copied.  If
 * you want a copy in the set, use strdup().
 *
 * Example:
 *	if (!strset_add(&set, "goodbye"))
 *		printf("goodbye was already in the set\n");
 */
bool strset_add(struct strset *set, const char *member);

/**
 * strset_del - remove a member from the string set.
 * @set: the set.
 * @member: the string to remove from the set.
 *
 * This returns the string which was passed to strset_add(), or NULL if
 * the string was not in the map (in which case it sets errno = ENOENT).
 *
 * This means that if you allocated a string (eg. using strdup()), you can
 * free it here.
 *
 * Example:
 *	if (!strset_del(&set, "goodbye"))
 *		printf("goodbye was not in the set?\n");
 */
char *strset_del(struct strset *set, const char *member);

/**
 * strset_clear - remove every member from the set.
 * @set: the set.
 *
 * The set will be empty after this.
 *
 * Example:
 *	strset_clear(&set);
 */
void strset_clear(struct strset *set);

/**
 * strset_iterate - ordered iteration over a set
 * @set: the set.
 * @handle: the function to call.
 * @arg: the argument for the function (types should match).
 *
 * You should not alter the set within the @handle function!  If it returns
 * false, the iteration will stop.
 *
 * Example:
 *	static bool dump_some(const char *member, int *num)
 *	{
 *		// Only dump out num nodes.
 *		if (*(num--) == 0)
 *			return false;
 *		printf("%s\n", member);
 *		return true;
 *	}
 *
 *	static void dump_set(const struct strset *set)
 *	{
 *		int max = 100;
 *		strset_iterate(set, dump_some, &max);
 *		if (max < 0)
 *			printf("... (truncated to 100 entries)\n");
 *	}
 */
#define strset_iterate(set, handle, arg)				\
	strset_iterate_((set), typesafe_cb_preargs(bool, void *,	\
						   (handle), (arg),	\
						   const char *),	\
			(arg))
void strset_iterate_(const struct strset *set,
		     bool (*handle)(const char *, void *), const void *data);


/**
 * strset_prefix - return a subset matching a prefix
 * @set: the set.
 * @prefix: the prefix.
 *
 * This returns a pointer into @set, so don't alter @set while using
 * the return value.  You can use strset_iterate(), strset_test() or
 * strset_empty() on the returned pointer.
 *
 * Example:
 *	static void dump_prefix(const struct strset *set, const char *prefix)
 *	{
 *		int max = 100;
 *		printf("Nodes with prefix %s:\n", prefix);
 *		strset_iterate(strset_prefix(set, prefix), dump_some, &max);
 *		if (max < 0)
 *			printf("... (truncated to 100 entries)\n");
 *	}
 */
const struct strset *strset_prefix(const struct strset *set,
				   const char *prefix);

#endif /* CCAN_STRSET_H */