3 /* xbt/dict.h -- api to a generic dictionary */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #include "xbt/misc.h" /* BEGIN_DECL */
20 /*####[ Type definition ]####################################################*/
21 typedef struct xbt_dict_ *xbt_dict_t;
23 /*####[ Simple dict functions ]#############################################*/
25 xbt_dict_t xbt_dict_new(void);
26 void xbt_dict_free(xbt_dict_t *dict);
29 void xbt_dict_set (xbt_dict_t head,
32 void_f_pvoid_t *free_ctn);
33 void xbt_dict_set_ext(xbt_dict_t head,
37 void_f_pvoid_t *free_ctn);
39 /*----[ xbt_dict_get ]------------------------------------------------------*/
40 /* Search the given #key#. data=NULL when not found. */
41 /* Returns true if anything went ok, and false on internal error. */
42 /*---------------------------------------------------------------------------*/
43 xbt_error_t xbt_dict_get(xbt_dict_t head,const char *key,
44 /* OUT */void **data);
45 xbt_error_t xbt_dict_get_ext(xbt_dict_t head,const char *key,
47 /* OUT */void **data);
48 /*----[ xbt_dict_remove ]---------------------------------------------------*/
49 /* Remove the entry associated with the given #key#. */
50 /* Returns if ok. Removing a non-existant key is ok. */
51 /*---------------------------------------------------------------------------*/
52 xbt_error_t xbt_dict_remove(xbt_dict_t head,const char *key);
54 xbt_error_t xbt_dict_remove_ext(xbt_dict_t head,
55 const char *key, int key_len);
57 /*----[ xbt_dict_dump ]-----------------------------------------------------*/
58 /* Outputs the content of the structure. (for debuging purpose) */
59 /* #output# is a function to output the data.If NULL, data won't be displayed*/
60 /*---------------------------------------------------------------------------*/
61 void xbt_dict_dump(xbt_dict_t head,
62 void (*output)(void*));
63 /*----[ xbt_dict_print ]----------------------------------------------------*/
64 /* To dump multicache, this function dump a cache */
65 /*---------------------------------------------------------------------------*/
66 void xbt_dict_print(void *data);
67 /* To dump multicache, this one dumps a string */
68 void xbt_dict_prints(void *data);
71 /*####[ Multi cache functions ]##############################################*/
72 /* The are cache of cache of data. Any function there works the same way */
73 /* than their simple cache counterpart. */
74 /*###############################"###########################################*/
76 /*----[ xbt_multidict_free ]------------------------------------------------*/
77 /* This function does not exist. Use xbt_dict_free instead. */
78 /*---------------------------------------------------------------------------*/
80 /*----[ xbt_multidict_set ]-------------------------------------------------*/
81 /* Insert the data in the structure under the #keycount# #key#s. */
82 /* The key are destroyed in the process. Think to strdup it before. */
83 /* Returns if it was ok or not */
84 /*---------------------------------------------------------------------------*/
85 xbt_error_t xbt_multidict_set(xbt_dict_t *head,
86 int keycount,char **key,
87 void *data,void (*free_ctn)(void*));
89 xbt_error_t xbt_multidict_set_ext(xbt_dict_t *head,
90 int keycount,char **key,int *key_len,
91 void *data,void_f_pvoid_t *free_ctn);
93 /*----[ xbt_multidict_get ]-------------------------------------------------*/
94 /* Search the given #key#. data=NULL when not found. */
95 /* Returns true if anything went ok, and false on internal error. */
96 /*---------------------------------------------------------------------------*/
97 xbt_error_t xbt_multidict_get(xbt_dict_t head,
98 int keycount,const char **key,
99 /* OUT */void **data);
101 xbt_error_t xbt_multidict_get_ext(xbt_dict_t head,
102 int keycount,const char **key,int *key_len,
103 /* OUT */void **data);
105 /*----[ xbt_multidict_remove ]----------------------------------------------*/
106 /* Remove the entry associated with the given #key#. */
107 /* Returns if ok. Removing a non-existant key is ok. */
108 /*---------------------------------------------------------------------------*/
109 xbt_error_t xbt_multidict_remove(xbt_dict_t head,
110 int keycount,const char **key);
112 xbt_error_t xbt_multidict_remove_ext(xbt_dict_t head,
113 int keycount,const char **key,int *key_len);
115 /*####[ Cache cursor functions ]#############################################*/
116 /* To traverse (simple) caches */
117 /* Don't add or remove entries to the cache while traversing !!! */
118 /*###########################################################################*/
119 typedef struct xbt_dict_cursor_ *xbt_dict_cursor_t;
120 /* creator/destructor */
121 xbt_dict_cursor_t xbt_dict_cursor_new(const xbt_dict_t head);
122 void xbt_dict_cursor_free(xbt_dict_cursor_t *cursor);
124 /* back to first element
125 it is not enough to reinit the cache after an add/remove in cache*/
126 void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor);
129 xbt_error_t xbt_dict_cursor_get_key (xbt_dict_cursor_t cursor,
131 xbt_error_t xbt_dict_cursor_get_data (xbt_dict_cursor_t cursor,
134 void xbt_dict_cursor_first (const xbt_dict_t dict,
135 xbt_dict_cursor_t *cursor);
136 void xbt_dict_cursor_step (xbt_dict_cursor_t cursor);
137 int xbt_dict_cursor_get_or_free (xbt_dict_cursor_t *cursor,
140 #define xbt_dict_foreach(dict,cursor,key,data) \
141 for (cursor=NULL, xbt_dict_cursor_first((dict),&(cursor)) ; \
142 xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));\
143 xbt_dict_cursor_step(cursor) )
149 #endif /* _XBT_DICT_H */