summaryrefslogtreecommitdiff
path: root/interpreter/util/slset.c
blob: 1d1af9adc278043d04645d2e0a1b6cdb5ba4d0e4 (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
/* $Id: slset.c 4800 2009-10-02 13:36:20Z potyra $
 *
 * Tiny implementation of a single linked set.
 *
 * Copyright (C) 2008-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include "slset.h"
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>

#if 0
#define DEBUG_MEM_FUNC
#endif

#ifdef DEBUG_MEM_FUNC
extern char etext;
extern char _init;

/* check if a function pointer is inside the text segment. */
#define CHECK_FUNC_ADDR(fun_ptr) { \
	if (fun_ptr != NULL) { \
		assert((void *)fun_ptr < (void *)&etext); \
		assert((void *)&_init < (void *)fun_ptr); \
	} \
}

#else /* ! DEBUG_MEM_FUNC */
#define CHECK_FUNC_ADDR(fun_ptr) 
#endif /* DEBUG_MEM_FUNC */

static int
__attribute__((__const__))
slset_compare_builtin(const void *o1, const void *o2)
{
	if (o1 < o2) {
		return -1;
	}

	if (o1 == o2) {
		return 0;
	}

	return 1;
}

static void
slset_destroy_set(struct slset *s, bool d_data, void (*deallocator)(void *))
{
	struct slset_entry *tmp;
	struct slset_entry *i = s->first;

	while (i != NULL) {
		tmp = i;
		i = i->next;
		if (d_data) {
			deallocator(tmp->data);
		}
		deallocator(tmp);
	}
}


struct slset *
slset_create(
	int (*compare)(const void *o1, const void *o2), 
	void *(*allocator)(size_t)
)
{
	struct slset *ret;
	CHECK_FUNC_ADDR(compare);

	ret = allocator(sizeof(struct slset));
	assert(ret != NULL);

	ret->compare = compare;
	if (ret->compare == NULL) {
		ret->compare = &slset_compare_builtin;
	}
	ret->first = NULL;
	return ret;
}

void
slset_destroy(struct slset *s, void (*deallocator)(void *))
{
	slset_destroy_set(s, false, deallocator);
	deallocator(s);
}

void
slset_destroy_data(struct slset *s, void (*deallocator)(void *))
{
	slset_destroy_set(s, true, deallocator);
	deallocator(s);
}


void
slset_add(
	struct slset *s, 
	void *data,
	void *(*allocator)(size_t)
)
{
	struct slset_entry *entry;
	struct slset_entry *i;
	struct slset_entry *last;
	bool past_entry = false;

	CHECK_FUNC_ADDR(s->compare);

	entry = allocator(sizeof(struct slset_entry));
	assert(entry != NULL);

	entry->data = data;

	if (s->first == NULL) {
		entry->next = NULL;
		s->first = entry;
		return;
	}

	/* entries already present. */
	last = NULL;
	for (i = s->first; i != NULL; i = i->next) {
		int cmp = s->compare(i->data, data);

		if (cmp > 0) {
			past_entry = true;
			break;
		}

		if (cmp == 0) {
			/* entry already present. do nothing. */
			return;
		}

		last = i;
	}

	if (past_entry) {
		int cmp;

		if (last == NULL) {
			entry->next = s->first;
			s->first = entry;
			return;
		}

		cmp = s->compare(last->data, data);
		assert(cmp == -1);

		entry->next = last->next;
		last->next = entry;

		return;
	}

	/* gone all through the list, but not past the entry where to 
	   insert -> insert after tail. */
	entry->next = NULL;
	last->next = entry;
	/* s->first doesn't change */
}

void
slset_move_all(
	struct slset *src, 
	struct slset *dst,
	void *(*allocator)(size_t),
	void (*deallocator)(void *)
)
{
	struct slset_entry *i;

	assert(src != NULL);
	assert(dst != NULL);
	assert(src->compare == dst->compare);

	/* FIXME use a faster implementation */
	for (i = src->first; i != NULL; i = i->next) {
		slset_add(dst, i->data, allocator);
	}

	slset_destroy_set(src, false, deallocator);
	src->first = NULL;
}

bool
slset_empty(const struct slset *s)
{
	return s->first == NULL;
}

bool
slset_contains(const struct slset *haystack, const void *needle)
{
	struct slset_entry *i;

	CHECK_FUNC_ADDR(haystack->compare);

	for (i = haystack->first; i != NULL; i = i->next) {
		int cmp = haystack->compare(i->data, needle);

		if (cmp == 0) {
			return true;
		}

		if (cmp > 0) {
			/* already past element */
			break;
		}

	}

	return false;
}

void
slset_remove(struct slset *s, const void *data, void (*deallocator)(void *))
{
	struct slset_entry *i;
	struct slset_entry *prev;

	CHECK_FUNC_ADDR(s->compare);

	prev = s->first;
	for (i = s->first; i != NULL; i = i->next) {
		int cmp = s->compare(i->data, data);

		if (cmp == 0) {
			/* found */
			if (prev == i) {
				/* first element */
				s->first = prev->next;
				free(i);
				return;
			}

			prev->next = i->next;
			deallocator(i);
			return;
		}
		
		if (cmp > 0) {
			/* past element */
			return;
		}
		prev = i;
	}
}

void
slset_clear(struct slset *s, void (*deallocator)(void *))
{
	struct slset_entry *i;
	struct slset_entry *n;

	for (i = s->first; i != NULL; /* nothing */) {
		n = i;
		i = i->next;
		deallocator(n);
	}

	s->first = NULL;
}

void
slset_truncate_at(struct slset *s, const void *data, bool del_data)
{	
	struct slset_entry *i;
	struct slset_entry *prev = NULL;

	CHECK_FUNC_ADDR(s->compare);

	for (i = s->first; i != NULL; i = i->next) {
		int cmp = s->compare(i->data, data);
		if (cmp != -1) {
			break;
		}

		prev = i;
	}

	/* nothing to delete? */
	if (i == NULL) {
		return;
	}

	/* first entry? */
	if (prev == NULL) {
		s->first = NULL;
	} else {
		prev->next = NULL;
	}

	while (i != NULL) {
		prev = i;
		i = i->next;

		if (del_data) {
			free(prev->data);
		}

		free(prev);
	}
}

void
slset_debug_print(
	const struct slset *s, 
	const char *(*print_data)(const void *))
{
	const struct slset_entry *i;

	fprintf(stderr, "fauhdli slset: printing set instance %p\n", s);

	for (i = s->first; i != NULL; i = i->next) {
		fprintf(stderr, "fauhdli slset: "
			"tstruct %p, data %s\n", i, print_data(i->data));
	}
}