aboutsummaryrefslogtreecommitdiff
blob: fa485c75784a54d113dd5b7fac4bd20557a3be6f (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright 2005-2023 Gentoo Foundation
 * Distributed under the terms of the GNU General Public License v2
 *
 * Copyright 2005-2010 Ned Ludd        - <solar@gentoo.org>
 * Copyright 2005-2014 Mike Frysinger  - <vapier@gentoo.org>
 * Copyright 2019-     Fabian Groffen  - <grobian@gentoo.org>
 */

#include "main.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <xalloc.h>

#include "set.h"

static unsigned int
fnv1a32(const char *s)
{
	unsigned int ret = 2166136261UL;
	for (; *s != '\0'; s++)
		ret = (ret ^ (unsigned int)*s) * 16777619;
	return ret;
}

/* create a set */
set *
create_set(void)
{
	return xzalloc(sizeof(set));
}

/* add elem to a set (unpure: could add duplicates, basically hash) */
set *
add_set(const char *name, set *q)
{
	int pos;
	set_elem *ll = xmalloc(sizeof(*ll));
	set_elem *w;

	if (q == NULL)
		q = create_set();

	ll->next = NULL;
	ll->name = xstrdup(name);
	ll->hash = fnv1a32(ll->name);
	ll->val = NULL;

	pos = ll->hash % _SET_HASH_SIZE;
	if (q->buckets[pos] == NULL) {
		q->buckets[pos] = ll;
	} else {
		for (w = q->buckets[pos]; w->next != NULL; w = w->next)
			;
		w->next = ll;
	}

	q->len++;
	return q;
}

/* add elem to set if it doesn't exist yet (pure definition of hash) */
set *
add_set_unique(const char *name, set *q, bool *unique)
{
	unsigned int hash;
	int pos;
	set_elem *ll;
	set_elem *w;
	bool uniq = false;

	if (q == NULL)
		q = create_set();

	hash = fnv1a32(name);
	pos = hash % _SET_HASH_SIZE;

	if (q->buckets[pos] == NULL) {
		q->buckets[pos] = ll = xmalloc(sizeof(*ll));
		ll->next = NULL;
		ll->name = xstrdup(name);
		ll->hash = hash;
		ll->val = NULL;
		uniq = true;
	} else {
		ll = NULL;
		for (w = q->buckets[pos]; w != NULL; ll = w, w = w->next) {
			if (w->hash == hash && strcmp(w->name, name) == 0) {
				uniq = false;
				break;
			}
		}
		if (w == NULL) {
			ll = ll->next = xmalloc(sizeof(*ll));
			ll->next = NULL;
			ll->name = xstrdup(name);
			ll->hash = hash;
			ll->val = NULL;
			uniq = true;
		}
	}

	if (uniq)
		q->len++;
	if (unique)
		*unique = uniq;
	return q;
}

/* add ptr to set with name as key, return existing value when key
 * already exists or NULL otherwise */
set *
add_set_value(const char *name, void *ptr, void **prevptr, set *q)
{
	unsigned int hash;
	int pos;
	set_elem *ll;
	set_elem *w;

	if (q == NULL)
		q = create_set();

	hash = fnv1a32(name);
	pos = hash % _SET_HASH_SIZE;

	if (prevptr != NULL)
		*prevptr = NULL;
	if (q->buckets[pos] == NULL) {
		q->buckets[pos] = ll = xmalloc(sizeof(*ll));
		ll->next = NULL;
		ll->name = xstrdup(name);
		ll->hash = hash;
		ll->val = ptr;
	} else {
		ll = NULL;
		for (w = q->buckets[pos]; w != NULL; ll = w, w = w->next) {
			if (w->hash == hash && strcmp(w->name, name) == 0) {
				if (prevptr != NULL)
					*prevptr = w->val;
				return q;
			}
		}
		if (w == NULL) {
			ll = ll->next = xmalloc(sizeof(*ll));
			ll->next = NULL;
			ll->name = xstrdup(name);
			ll->hash = hash;
			ll->val = ptr;
		}
	}

	q->len++;
	return q;
}

/* returns whether name is in set, and if so, the set-internal key
 * representation (an internal copy of name made during addition) */
const char *
contains_set(const char *name, set *q)
{
	unsigned int hash;
	int pos;
	set_elem *w;
	const char *found;

	if (q == NULL)
		return NULL;

	hash = fnv1a32(name);
	pos = hash % _SET_HASH_SIZE;

	found = NULL;
	if (q->buckets[pos] != NULL) {
		for (w = q->buckets[pos]; w != NULL; w = w->next) {
			if (w->hash == hash && strcmp(w->name, name) == 0) {
				found = w->name;
				break;
			}
		}
	}

	return found;
}

/* returns the value for name, or NULL if not found (cannot
 * differentiate between value NULL and unset) */
void *
get_set(const char *name, set *q)
{
	unsigned int hash;
	int pos;
	set_elem *w;

	if (q == NULL)
		return NULL;

	hash = fnv1a32(name);
	pos = hash % _SET_HASH_SIZE;

	if (q->buckets[pos] != NULL) {
		for (w = q->buckets[pos]; w != NULL; w = w->next) {
			if (w->hash == hash && strcmp(w->name, name) == 0)
				return w->val;
		}
	}

	return NULL;
}

/* remove elem from a set. matches ->name and frees name,item, returns
 * val if removed, NULL otherwise
 * note that when val isn't set, NULL is returned, so the caller should
 * use the removed argument to determine if something was removed from
 * the set. */
void *
del_set(const char *s, set *q, bool *removed)
{
	unsigned int hash;
	int pos;
	set_elem *ll;
	set_elem *w;
	void *ret;
	bool rmd;

	if (q == NULL) {
		if (removed != NULL)
			*removed = false;
		return NULL;
	}

	hash = fnv1a32(s);
	pos = hash % _SET_HASH_SIZE;

	ret = NULL;
	rmd = false;
	if (q->buckets[pos] != NULL) {
		ll = NULL;
		for (w = q->buckets[pos]; w != NULL; ll = w, w = w->next) {
			if (w->hash == hash && strcmp(w->name, s) == 0) {
				if (ll == NULL) {
					q->buckets[pos] = w->next;
				} else {
					ll->next = w->next;
				}
				ret = w->val;
				free(w->name);
				free(w);
				rmd = true;
				break;
			}
		}
	}

	if (rmd)
		q->len--;
	if (removed != NULL)
		*removed = rmd;
	return ret;
}

/* return the contents of a set as an array of strings
 * the length of the list is returned, and the array is terminated with
 * a NULL (not included in returned length)
 * the caller should free l, but not the strings within */
size_t
list_set(set *q, char ***l)
{
	int i;
	set_elem *w;
	char **ret;

	ret = *l = xmalloc(sizeof(char *) * (cnt_set(q) + 1));
	for (i = 0; q != NULL && i < _SET_HASH_SIZE; i++) {
		for (w = q->buckets[i]; w != NULL; w = w->next) {
			*ret = w->name;
			ret++;
		}
	}
	*ret = NULL;
	return q->len;
}

size_t
array_set(set *q, array_t *ret)
{
	int i;
	set_elem *w;
	array_t blank = array_init_decl;

	*ret = blank;

	/* allow using empty set */
	if (q == NULL)
		return 0;

	for (i = 0; i < _SET_HASH_SIZE; i++) {
		for (w = q->buckets[i]; w != NULL; w = w->next)
			xarraypush_ptr(ret, w->name);
	}

	return q->len;
}

size_t
values_set(set *q, array_t *ret)
{
	int i;
	set_elem *w;
	array_t blank = array_init_decl;

	*ret = blank;

	/* allow using empty set */
	if (q == NULL)
		return 0;

	for (i = 0; i < _SET_HASH_SIZE; i++) {
		for (w = q->buckets[i]; w != NULL; w = w->next)
			xarraypush_ptr(ret, w->val);
	}

	return q->len;
}

size_t
cnt_set(set *q)
{
	return q == NULL ? 0 : q->len;
}

/* clear out a set */
void
clear_set(set *q)
{
	int i;
	set_elem *w;
	set_elem *e;

	if (q == NULL)
		return;

	for (i = 0; i < _SET_HASH_SIZE; i++) {
		for (w = q->buckets[i]; w != NULL; w = e) {
			e = w->next;
			free(w->name);
			free(w);
		}
		q->buckets[i] = NULL;
	}
	q->len = 0;
}

/* clear and free a set */
void
free_set(set *q)
{
	if (q == NULL)
		return;

	clear_set(q);
	free(q);
}

#ifdef EBUG
static void
print_set(const set *q)
{
	set_elem *w;
	int i;

	for (i = 0; i < _SET_HASH_SIZE; i++) {
		for (w = q->buckets[i]; w != NULL; w = w->next)
			puts(w->name);
	}
}
#endif