1 /* xbt/set.h -- api to a generic dictionary */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
12 #include "xbt/misc.h" /* SG_BEGIN_DECL */
13 #include "xbt/function_types.h"
17 /** @addtogroup XBT_set
18 * @brief A data container consisting in \ref XBT_dict and \ref XBT_dynar
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:
26 unsigned int name_len;
27 // my other fields, constituting the payload
28 } my_element_type_t; \endverbatim
30 * Since we are casting elements around, no protection is ensured by the
31 * compiler. It is thus safer to define the headers using the macro
32 * defined to that extend:
36 // my other fields, constituting the payload
37 } my_element_type_t; \endverbatim
39 * It is now possible to remove an element from such a data structure.
42 * Such a datastructure was necessary/useful to store the GRAS type
43 * descriptions, but it should be reworked to become generic.
46 /** @defgroup XBT_set_cons Set and set elements, constructor/destructor
51 /** \brief Opaque type representing a set */
52 typedef struct xbt_set_ *xbt_set_t;
54 #define XBT_SET_HEADERS \
59 /** \brief It must be possible to cast set elements to this type */
60 typedef struct xbt_set_elm_ {
61 unsigned int ID; /**< Identificator (system assigned) */
62 char *name; /**< Name (user assigned) */
63 unsigned int name_len;
64 /**< Length of the name */
65 } s_xbt_set_elm_t, *xbt_set_elm_t;
67 /*####[ Functions ]##########################################################*/
68 XBT_PUBLIC(xbt_set_t) xbt_set_new(void);
69 XBT_PUBLIC(void) xbt_set_free(xbt_set_t * set);
72 /** @defgroup XBT_set_basic Sets basic usage
78 XBT_PUBLIC(void) xbt_set_add(xbt_set_t set, xbt_set_elm_t elm,
79 void_f_pvoid_t free_func);
80 XBT_PUBLIC(void) xbt_set_remove(xbt_set_t set, xbt_set_elm_t elm);
81 XBT_PUBLIC(void) xbt_set_remove_by_name(xbt_set_t set, const char *key);
82 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_name_or_null(xbt_set_t set,
84 XBT_PUBLIC(void) xbt_set_remove_by_name_ext(xbt_set_t set, const char *key,
86 XBT_PUBLIC(void) xbt_set_remove_by_id(xbt_set_t set, int id);
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,
92 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_id(xbt_set_t set, int id);
94 XBT_PUBLIC(unsigned long) xbt_set_length(const xbt_set_t set);
98 /** @defgroup XBT_set_curs Sets cursors
101 * \warning Don't add or remove entries to the cache while traversing
106 /** @brief Cursor type */
107 typedef struct xbt_set_cursor_ *xbt_set_cursor_t;
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);
115 /** @brief Iterates over the whole set
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) )
125 #endif /* _XBT_SET_H */