Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add xbt_set_get_by_name_or_null() [Silas De Munck]
[simgrid.git] / include / xbt / set.h
1 /* $Id$ */
2
3 /* xbt/set.h -- api to a generic dictionary                                 */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
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. */
9
10 #ifndef _XBT_SET_H
11 #define _XBT_SET_H
12
13 #include "xbt/misc.h"           /* SG_BEGIN_DECL */
14 #include "xbt/function_types.h"
15
16 SG_BEGIN_DECL()
17
18 /** @addtogroup XBT_set
19  *  @brief A data container consisting in \ref XBT_dict and \ref XBT_dynar
20  *
21  *  The elements stored in such a data structure can be retrieve both by
22  *  name and by ID. For this to work, the first fields of the structures
23  *  stored must begin with the following fields:
24  *  \verbatim struct {
25  unsigned int ID;
26  char        *name;
27  unsigned int name_len;
28  // my other fields, constituting the payload
29 } my_element_type_t; \endverbatim
30  *
31  *  Since we are casting elements around, no protection is ensured by the
32  * compiler. It is thus safer to define the headers using the macro
33  * defined to that extend:
34  *  \verbatim struct {
35  XBT_SET_HEADERS;
36
37  // my other fields, constituting the payload
38 } my_element_type_t; \endverbatim
39  *
40  *  It is now possible to remove an element from such a data structure.
41  *
42  *  @todo
43  *  Such a datastructure was necessary/useful to store the GRAS type
44  *  descriptions, but it should be reworked to become generic.
45  *
46  */
47 /** @defgroup XBT_set_cons Set and set elements, constructor/destructor
48  *  @ingroup XBT_set
49  *
50  *  @{
51  */
52 /** \brief Opaque type representing a set */
53      typedef struct xbt_set_ *xbt_set_t;
54
55 #define XBT_SET_HEADERS \
56   unsigned int ID;      \
57   char        *name;    \
58   unsigned int name_len
59
60 /** \brief It must be possible to cast set elements to this type */
61      typedef struct xbt_set_elm_ {
62        unsigned int ID; /**< Identificator (system assigned) */
63        char *name;      /**< Name (user assigned) */
64        unsigned int name_len;
65                         /**< Length of the name */
66      } s_xbt_set_elm_t, *xbt_set_elm_t;
67
68 /*####[ Functions ]##########################################################*/
69 XBT_PUBLIC(xbt_set_t) xbt_set_new(void);
70 XBT_PUBLIC(void) xbt_set_free(xbt_set_t * set);
71
72 /** @} */
73 /** @defgroup XBT_set_basic Sets basic usage
74  *  @ingroup XBT_set
75  *
76  *  @{
77  */
78
79 XBT_PUBLIC(void) xbt_set_add(xbt_set_t set, xbt_set_elm_t elm,
80                              void_f_pvoid_t free_func);
81 XBT_PUBLIC(void) xbt_set_remove(xbt_set_t set, xbt_set_elm_t elm);
82 XBT_PUBLIC(void) xbt_set_remove_by_name(xbt_set_t set, const char *key);
83 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_name_or_null(xbt_set_t set, const char *key);
84 XBT_PUBLIC(void) xbt_set_remove_by_name_ext(xbt_set_t set, const char *key,
85                                             int key_len);
86 XBT_PUBLIC(void) xbt_set_remove_by_id(xbt_set_t set, int id);
87
88 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_name(xbt_set_t set, const char *key);
89 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_name_ext(xbt_set_t set,
90                                                   const char *key,
91                                                   int key_len);
92 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_id(xbt_set_t set, int id);
93
94 XBT_PUBLIC(unsigned long) xbt_set_length(const xbt_set_t set);
95
96
97 /** @} */
98 /** @defgroup XBT_set_curs Sets cursors
99  *  @ingroup XBT_set
100  *
101  *  \warning Don't add or remove entries to the cache while traversing
102  *
103  *  @{
104  */
105
106 /** @brief Cursor type */
107      typedef struct xbt_set_cursor_ *xbt_set_cursor_t;
108
109 XBT_PUBLIC(void) xbt_set_cursor_first(xbt_set_t set,
110                                       xbt_set_cursor_t * cursor);
111 XBT_PUBLIC(void) xbt_set_cursor_step(xbt_set_cursor_t cursor);
112 XBT_PUBLIC(int) xbt_set_cursor_get_or_free(xbt_set_cursor_t * cursor,
113                                            xbt_set_elm_t * elm);
114
115 /** @brief Iterates over the whole set
116  *  @hideinitializer
117  */
118 #define xbt_set_foreach(set,cursor,elm)                       \
119   for ((cursor) = NULL, xbt_set_cursor_first((set),&(cursor)) ;   \
120        xbt_set_cursor_get_or_free(&(cursor),(xbt_set_elm_t*)&(elm));          \
121        xbt_set_cursor_step(cursor) )
122
123 /* @} */
124 SG_END_DECL()
125 #endif /* _XBT_SET_H */