summaryrefslogtreecommitdiff
path: root/dict.c
blob: aad50fd9b935d03b65209f59bd6f79fd3392063e (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
/*
 * This file is part of ltrace.
 * Copyright (C) 2011,2012 Petr Machata
 * Copyright (C) 2003,2004,2008,2009 Juan Cespedes
 * Copyright (C) 2006 Ian Wienand
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "common.h"

/*
 * Dictionary based on code by Morten Eriksen <mortene@sim.no>.
 */

struct dict_entry {
	unsigned int hash;
	void *key;
	void *value;
	struct dict_entry *next;
};

/* #define DICTTABLESIZE 97 */
#define DICTTABLESIZE 997	/* Semi-randomly selected prime number. */
/* #define DICTTABLESIZE 9973 */
/* #define DICTTABLESIZE 99991 */
/* #define DICTTABLESIZE 999983 */

struct dict {
	struct dict_entry *buckets[DICTTABLESIZE];
	unsigned int (*key2hash) (const void *);
	int (*key_cmp) (const void *, const void *);
};

Dict *
dict_init(unsigned int (*key2hash) (const void *),
	  int (*key_cmp) (const void *, const void *))
{
	Dict *d;
	int i;

	debug(DEBUG_FUNCTION, "dict_init()");

	d = malloc(sizeof(Dict));
	if (!d) {
		perror("malloc()");
		exit(1);
	}
	for (i = 0; i < DICTTABLESIZE; i++) {	/* better use memset()? */
		d->buckets[i] = NULL;
	}
	d->key2hash = key2hash;
	d->key_cmp = key_cmp;
	return d;
}

void
dict_clear(Dict *d) {
	int i;
	struct dict_entry *entry, *nextentry;

	debug(DEBUG_FUNCTION, "dict_clear()");
	assert(d);
	for (i = 0; i < DICTTABLESIZE; i++) {
		for (entry = d->buckets[i]; entry != NULL; entry = nextentry) {
			nextentry = entry->next;
			free(entry);
		}
		d->buckets[i] = NULL;
	}
	free(d);
}

int
dict_enter(Dict *d, void *key, void *value) {
	struct dict_entry *entry, *newentry;
	unsigned int hash;
	unsigned int bucketpos;

	debug(DEBUG_FUNCTION, "dict_enter()");

	hash = d->key2hash(key);
	bucketpos = hash % DICTTABLESIZE;

	assert(d);
	newentry = malloc(sizeof(struct dict_entry));
	if (!newentry) {
		perror("malloc");
		exit(1);
	}

	newentry->hash = hash;
	newentry->key = key;
	newentry->value = value;
	newentry->next = NULL;

	entry = d->buckets[bucketpos];
	while (entry && entry->next)
		entry = entry->next;

	if (entry)
		entry->next = newentry;
	else
		d->buckets[bucketpos] = newentry;

	debug(3, "new dict entry at %p[%d]: (%p,%p)", d, bucketpos, key, value);
	return 0;
}

void *
dict_remove(Dict *d, void *key)
{
	assert(d != NULL);
	debug(DEBUG_FUNCTION, "dict_remove(%p)", key);

	unsigned int hash = d->key2hash(key);
	unsigned int bucketpos = hash % DICTTABLESIZE;

	struct dict_entry **entryp;
	for (entryp = &d->buckets[bucketpos]; (*entryp) != NULL;
	     entryp = &(*entryp)->next) {
		struct dict_entry *entry = *entryp;
		if (hash != entry->hash)
			continue;
		if (d->key_cmp(key, entry->key) == 0) {
			*entryp = entry->next;
			void *value = entry->value;
			free(entry);
			return value;
		}
	}
	return NULL;
}

void *
dict_find_entry(Dict *d, const void *key)
{
	unsigned int hash;
	unsigned int bucketpos;
	struct dict_entry *entry;

	debug(DEBUG_FUNCTION, "dict_find_entry()");

	hash = d->key2hash(key);
	bucketpos = hash % DICTTABLESIZE;

	assert(d);
	for (entry = d->buckets[bucketpos]; entry; entry = entry->next) {
		if (hash != entry->hash) {
			continue;
		}
		if (!d->key_cmp(key, entry->key)) {
			break;
		}
	}
	return entry ? entry->value : NULL;
}

void
dict_apply_to_all(Dict *d,
		  void (*func) (void *key, void *value, void *data), void *data) {
	int i;

	debug(DEBUG_FUNCTION, "dict_apply_to_all()");

	if (!d) {
		return;
	}
	for (i = 0; i < DICTTABLESIZE; i++) {
		struct dict_entry *entry = d->buckets[i];
		while (entry) {
			func(entry->key, entry->value, data);
			entry = entry->next;
		}
	}
}

/*****************************************************************************/

unsigned int
dict_key2hash_string(const void *key)
{
	const char *s = (const char *)key;
	unsigned int total = 0, shift = 0;

	assert(key);
	while (*s) {
		total = total ^ ((*s) << shift);
		shift += 5;
		if (shift > 24)
			shift -= 24;
		s++;
	}
	return total;
}

int
dict_key_cmp_string(const void *key1, const void *key2)
{
	assert(key1);
	assert(key2);
	return strcmp((const char *)key1, (const char *)key2);
}

unsigned int
dict_key2hash_int(const void *key)
{
	return (unsigned long)key;
}

int
dict_key_cmp_int(const void *key1, const void *key2)
{
	return key1 - key2;
}

Dict *
dict_clone2(Dict * old, void * (*key_clone)(void *, void *),
	    void * (*value_clone)(void *, void *), void * data)
{
	Dict *d;
	int i;

	debug(DEBUG_FUNCTION, "dict_clone()");

	d = malloc(sizeof(Dict));
	if (!d) {
		perror("malloc()");
		exit(1);
	}
	memcpy(d, old, sizeof(Dict));
	for (i = 0; i < DICTTABLESIZE; i++) {	/* better use memset()? */
		struct dict_entry *de_old;
		struct dict_entry **de_new;

		de_old = old->buckets[i];
		de_new = &d->buckets[i];
		while (de_old) {
			void * nkey, * nval;
			*de_new = malloc(sizeof(struct dict_entry));
			if (!*de_new) {
				perror("malloc()");
				exit(1);
			}
			memcpy(*de_new, de_old, sizeof(struct dict_entry));

			/* The error detection is rather weak :-/ */
			nkey = key_clone(de_old->key, data);
			if (nkey == NULL && de_old->key != NULL) {
				perror("key_clone");
			err:
				/* XXX Will this actually work?  We
				 * simply memcpy the old dictionary
				 * over up there.  */
				dict_clear(d);
				free(de_new);
				return NULL;
			}

			nval = value_clone(de_old->value, data);
			if (nval == NULL && de_old->value != NULL) {
				perror("value_clone");
				goto err;
			}

			(*de_new)->key = nkey;
			(*de_new)->value = nval;
			de_new = &(*de_new)->next;
			de_old = de_old->next;
		}
	}
	return d;
}

struct wrap_clone_cb
{
	void * (*key_clone)(void *);
	void * (*value_clone)(void *);
};

static void *
value_clone_1(void * arg, void * data)
{
	return ((struct wrap_clone_cb *)data)->value_clone(arg);
}

static void *
key_clone_1(void * arg, void * data)
{
	return ((struct wrap_clone_cb *)data)->key_clone(arg);
}

Dict *
dict_clone(Dict * old, void * (*key_clone)(void *),
	   void * (*value_clone)(void *))
{
	struct wrap_clone_cb cb = { key_clone, value_clone };
	return dict_clone2(old, &key_clone_1, &value_clone_1, &cb);
}