1 /* xbt/set.h -- api to a generic dictionary */
3 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/misc.h" /* SG_BEGIN_DECL */
12 #include "xbt/function_types.h"
16 /** @addtogroup XBT_set
17 * @brief A data container consisting in \ref XBT_dict and \ref XBT_dynar
19 * The elements stored in such a data structure can be retrieve both by
20 * name and by ID. For this to work, the first fields of the structures
21 * stored must begin with the following fields:
25 unsigned int name_len;
26 // my other fields, constituting the payload
27 } my_element_type_t; \endverbatim
29 * Since we are casting elements around, no protection is ensured by the
30 * compiler. It is thus safer to define the headers using the macro
31 * defined to that extend:
35 // my other fields, constituting the payload
36 } my_element_type_t; \endverbatim
38 * It is now possible to remove an element from such a data structure.
41 * Such a datastructure was necessary/useful to store the GRAS type
42 * descriptions, but it should be reworked to become generic.
45 /** @defgroup XBT_set_cons Set and set elements, constructor/destructor
50 /** \brief Opaque type representing a set */
51 typedef struct xbt_set_ *xbt_set_t;
53 #define XBT_SET_HEADERS \
58 /** \brief It must be possible to cast set elements to this type */
59 typedef struct xbt_set_elm_ {
60 unsigned int ID; /**< Identificator (system assigned) */
61 char *name; /**< Name (user assigned) */
62 unsigned int name_len;
63 /**< Length of the name */
64 } s_xbt_set_elm_t, *xbt_set_elm_t;
66 /*####[ Functions ]##########################################################*/
67 XBT_PUBLIC(xbt_set_t) xbt_set_new(void);
68 XBT_PUBLIC(void) xbt_set_free(xbt_set_t * set);
71 /** @defgroup XBT_set_basic Sets basic usage
77 XBT_PUBLIC(void) xbt_set_add(xbt_set_t set, xbt_set_elm_t elm,
78 void_f_pvoid_t free_func);
79 XBT_PUBLIC(void) xbt_set_remove(xbt_set_t set, xbt_set_elm_t elm);
80 XBT_PUBLIC(void) xbt_set_remove_by_name(xbt_set_t set, const char *key);
81 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_name_or_null(xbt_set_t set,
83 XBT_PUBLIC(void) xbt_set_remove_by_name_ext(xbt_set_t set, const char *key,
85 XBT_PUBLIC(void) xbt_set_remove_by_id(xbt_set_t set, int id);
87 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_name(xbt_set_t set, const char *key);
88 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_name_ext(xbt_set_t set,
91 XBT_PUBLIC(xbt_set_elm_t) xbt_set_get_by_id(xbt_set_t set, int id);
93 XBT_PUBLIC(unsigned long) xbt_set_length(const xbt_set_t set);
97 /** @defgroup XBT_set_curs Sets cursors
100 * \warning Don't add or remove entries to the cache while traversing
105 /** @brief Cursor type */
106 typedef struct xbt_set_cursor_ *xbt_set_cursor_t;
108 XBT_PUBLIC(void) xbt_set_cursor_first(xbt_set_t set,
109 xbt_set_cursor_t * cursor);
110 XBT_PUBLIC(void) xbt_set_cursor_step(xbt_set_cursor_t cursor);
111 XBT_PUBLIC(int) xbt_set_cursor_get_or_free(xbt_set_cursor_t * cursor,
112 xbt_set_elm_t * elm);
114 /** @brief Iterates over the whole set
117 #define xbt_set_foreach(set,cursor,elm) \
118 for ((cursor) = NULL, xbt_set_cursor_first((set),&(cursor)) ; \
119 xbt_set_cursor_get_or_free(&(cursor),(xbt_set_elm_t*)&(elm)); \
120 xbt_set_cursor_step(cursor) )
124 #endif /* _XBT_SET_H */