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