3 * Copyright 2005 Juan Lang
5 * This is a pretty basic dictionary, or map if you prefer. It's not
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifndef __DICTIONARY_H__
23 #define __DICTIONARY_H__
31 /* Returns whether key a is less than, equal to, or greater than key b, in
32 * the same way (a - b) would for integers or strcmp(a, b) would for ANSI
35 typedef int (*comparefunc)(const void *a, const void *b, void *extra);
37 /* Called for every element removed from the dictionary. See
38 * dictionary_destroy, dictionary_insert, and dictionary_remove.
40 typedef void (*destroyfunc)(void *k, void *v, void *extra);
42 /* Called for each element in the dictionary. Return FALSE if you don't want
43 * to enumerate any more.
45 typedef BOOL (*enumeratefunc)(const void *k, const void *d, void *extra);
47 /* Constructs a dictionary, using c as a comparison function for keys.
48 * If d is not NULL, it will be called whenever an item is about to be removed
49 * from the table, for example when dictionary_remove is called for a key, or
50 * when dictionary_destroy is called.
51 * extra is passed to c (and d, if it's provided).
52 * Assumes c is not NULL.
54 struct dictionary *dictionary_create(comparefunc c, destroyfunc d, void *extra);
56 /* Assumes d is not NULL. */
57 void dictionary_destroy(struct dictionary *d);
59 /* Sets an element with key k and value v to the dictionary. If a value
60 * already exists with key k, its value is replaced, and the destroyfunc (if
61 * set) is called for the previous item.
62 * Assumes k and v can be bitwise-copied.
63 * Both k and v are allowed to be NULL, in case you want to use integer
64 * values for either the key or the value.
65 * Assumes d is not NULL.
67 void dictionary_insert(struct dictionary *d, const void *k, const void *v);
69 /* If a value with key k has been inserted into the dictionary, *v is set
70 * to its associated value. Returns FALSE if the key is not found, and TRUE
71 * if it is. *v is undefined if it returns FALSE. (It is not set to NULL,
72 * because this dictionary doesn't prevent you from using NULL as a value
73 * value; see dictionary_insert.)
74 * Assumes d and v are not NULL.
76 BOOL dictionary_find(struct dictionary *d, const void *k, void **v);
78 /* Removes the element with key k from the dictionary. Calls the destroyfunc
79 * for the dictionary with the element if found (so you may destroy it if it's
80 * dynamically allocated.)
81 * Assumes d is not NULL.
83 void dictionary_remove(struct dictionary *d, const void *k);
85 void dictionary_enumerate(struct dictionary *d, enumeratefunc e);
87 #endif /* ndef __DICTIONARY_H__ */