Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New functions to search in dynars
[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:
24  *  \verbatim unsigned int ID;
25  char        *name;
26  unsigned int name_len;\endverbatim
27  *
28  *  It is impossible to remove an element from such a data structure.
29  *
30  *  @todo
31  *  Such a datastructure was necessary/useful to store the GRAS type 
32  *  descriptions, but it should be reworked to become generic.
33  *
34  */
35
36
37 /** @defgroup XBT_set_cons Set and set elements, constructor/destructor
38  *  @ingroup XBT_set
39  *
40  *  @{
41  */
42 /** \brief Opaque type representing a set */
43 typedef struct xbt_set_ *xbt_set_t; 
44 /** \brief It must be possible to cast set elements to this type */
45 struct xbt_set_elm_ {
46   unsigned int ID;      /**< Identificator (system assigned) */
47   char        *name;    /**< Name (user assigned) */
48   unsigned int name_len;/**< Length of the name */
49 };
50
51 /*####[ Functions ]##########################################################*/
52 xbt_set_t xbt_set_new (void);
53 void xbt_set_free(xbt_set_t *set);
54
55 /** @} */
56 typedef struct xbt_set_elm_  s_xbt_set_elm_t;
57 typedef struct xbt_set_elm_ *  xbt_set_elm_t;
58 /** @defgroup XBT_set_basic Sets basic usage
59  *  @ingroup XBT_set
60  *
61  *  @{
62  */
63
64 void xbt_set_add (xbt_set_t set, xbt_set_elm_t  elm, void_f_pvoid_t *free_func);
65
66 xbt_set_elm_t xbt_set_get_by_name    (xbt_set_t set, const char *key);
67 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t set, const char *key, int key_len);
68 xbt_set_elm_t xbt_set_get_by_id      (xbt_set_t set, int         id);
69                                       
70 /** @} */
71 /** @defgroup XBT_set_curs Sets cursors
72  *  @ingroup XBT_set
73  *
74  *  \warning Don't add or remove entries to the cache while traversing
75  *
76  *  @{
77  */
78
79 /** @brief Cursor type */
80 typedef struct xbt_set_cursor_ *xbt_set_cursor_t;
81
82 void         xbt_set_cursor_first       (xbt_set_t         set,
83                                           xbt_set_cursor_t *cursor);
84 void         xbt_set_cursor_step        (xbt_set_cursor_t  cursor);
85 int          xbt_set_cursor_get_or_free (xbt_set_cursor_t *cursor,
86                                           xbt_set_elm_t    *elm);
87
88 /** @brief Iterates over the whole set
89  *  @hideinitializer
90  */
91 #define xbt_set_foreach(set,cursor,elm)                       \
92   for ((cursor) = NULL, xbt_set_cursor_first((set),&(cursor)) ;   \
93        xbt_set_cursor_get_or_free(&(cursor),(xbt_set_elm_t*)&(elm));          \
94        xbt_set_cursor_step(cursor) )
95
96 /* @} */
97 SG_END_DECL()
98
99 #endif /* _XBT_SET_H */