Initial import (again)
[srvx.git] / src / dict.h
1 /* dict.h - Abstract dictionary type
2  * Copyright 2000-2004 srvx Development Team
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.  Important limitations are
8  * listed in the COPYING file that accompanies this software.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, email srvx-maintainers@srvx.net.
17  */
18
19 #if !defined(DICT_H)
20 #define DICT_H
21
22 /* helper types */
23 typedef void (*free_f)(void*);
24 typedef int (*dict_iterator_f)(const char *key, void *data, void *extra);
25
26 /* exposed ONLY for the iteration macros; if you use these, DIE */
27 struct dict_node {
28     const char *key;
29     void *data;
30     struct dict_node *l, *r, *prev, *next;
31 };
32
33 struct dict {
34     free_f free_keys, free_data;
35     struct dict_node *root, *first, *last;
36     unsigned int count;
37 };
38
39 /* "published" API */
40 typedef struct dict *dict_t;
41 typedef struct dict_node *dict_iterator_t;
42
43 #define dict_first(DICT) ((DICT) ? (DICT)->first : NULL)
44 #define iter_key(ITER) ((ITER)->key)
45 #define iter_data(ITER) ((ITER)->data)
46 #define iter_next(ITER) ((ITER)->next)
47
48 dict_t dict_new(void);
49 /* dict_foreach returns key of node causing halt (non-zero return from
50  * iterator function) */
51 const char* dict_foreach(dict_t dict, dict_iterator_f it, void *extra);
52 void dict_insert(dict_t dict, const char *key, void *data);
53 void dict_set_free_keys(dict_t dict, free_f free_keys);
54 void dict_set_free_data(dict_t dict, free_f free_data);
55 unsigned int dict_size(dict_t dict);
56 /* if present!=NULL, then *present=1 iff node was found (if node is
57  * not found, return value is NULL, which may be a valid datum) */
58 void* dict_find(dict_t dict, const char *key, int *present);
59 int dict_remove2(dict_t dict, const char *key, int no_dispose);
60 #define dict_remove(DICT, KEY) dict_remove2(DICT, KEY, 0)
61 char *dict_sanity_check(dict_t dict);
62 void dict_delete(dict_t dict);
63
64 #endif /* !defined(DICT_H) */