Logo AND Algorithmique Numérique Distribuée

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