X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/3d0d626e19a7b79320e7d922c9c1dcf122076cea..13c904d6998134e6eb726d1b695d9ce0c8c2e949:/src/xbt/set.c diff --git a/src/xbt/set.c b/src/xbt/set.c index 974837e57e..e97eed9b99 100644 --- a/src/xbt/set.c +++ b/src/xbt/set.c @@ -2,78 +2,72 @@ /* set - data container consisting in dict+dynar */ -/* Authors: Martin Quinson */ -/* Copyright (C) 2004 the GRAS posse. */ +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it - under the terms of the license (GNU LGPL) which comes with this package. */ + * under the terms of the license (GNU LGPL) which comes with this package. */ -#include "gras_private.h" +#include "xbt/misc.h" +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "xbt/error.h" +#include "xbt/dynar.h" +#include "xbt/dict.h" -GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(set,gros,"data container consisting in dict+dynar"); +#include "xbt/set.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(set,xbt, + "set: data container consisting in dict+dynar"); /*####[ Type definition ]####################################################*/ -struct gras_set_ { - gras_dict_t *dict; /* data stored by name */ - gras_dynar_t *dynar; /* data stored by ID */ -}; +typedef struct xbt_set_ { + xbt_dict_t dict; /* data stored by name */ + xbt_dynar_t dynar; /* data stored by ID */ +} s_xbt_set_t; /*####[ Memory ]############################################################*/ -/** - * gras_set_new: - * @dst: where to - * - * Creates a new set. - */ -void gras_set_new (gras_set_t **dst) { - gras_set_t *res=gras_new(gras_set_t,1); - gras_error_t errcode; +/** @brief Constructor */ +xbt_set_t xbt_set_new (void) { + xbt_set_t res=xbt_new(s_xbt_set_t,1); - gras_dict_new (&(res->dict)); - gras_dynar_new(&(res->dynar), sizeof(void*),NULL); + res->dict=xbt_dict_new (); + res->dynar=xbt_dynar_new(sizeof(void*),NULL); - *dst=res; + return res; } -/** - * gras_set_free: - * @set: - * - * Frees a set. - */ -void gras_set_free(gras_set_t **set) { +/** @brief Destructor */ +void xbt_set_free(xbt_set_t *set) { if (*set) { - gras_dict_free ( &( (*set)->dict ) ); - gras_dynar_free( (*set)->dynar ); - gras_free(*set); - *set=NULL; + xbt_dict_free ( &( (*set)->dict ) ); + xbt_dynar_free( &( (*set)->dynar ) ); + free(*set); + *set = NULL; } } -/** - * gras_set_add: - * @set: set to populate - * @elm: element to add. - * @free_ctn: How to add the data +/** @brief Add an element to a set. * - * Add an element to a set. + * \param set set to populate + * \param elm element to add. + * \param free_func How to add the data * * elm->name must be set; - * elm->name_len is used as is unless it's <= 0 (in which case it's recomputed); + * if elm->name_len <= 0, it is recomputed. If >0, it's used as is; * elm->ID is attributed automatically. */ -void gras_set_add (gras_set_t *set, - gras_set_elm_t *elm, +void xbt_set_add (xbt_set_t set, + xbt_set_elm_t elm, void_f_pvoid_t *free_func) { - gras_error_t errcode; - gras_set_elm_t *found_in_dict; + xbt_error_t errcode; + xbt_set_elm_t found_in_dict; if (elm->name_len <= 0) { elm->name_len = strlen(elm->name); } - errcode = gras_dict_get_ext (set->dict, + errcode = xbt_dict_get_ext (set->dict, elm->name, elm->name_len, (void**)&found_in_dict); if (errcode == no_error) { @@ -84,74 +78,71 @@ void gras_set_add (gras_set_t *set, } else { elm->ID=found_in_dict->ID; DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID); - gras_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func); - gras_dynar_set(set->dynar, elm->ID, &elm); + xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func); + xbt_dynar_set(set->dynar, elm->ID, &elm); return; } } else { - gras_assert_error(mismatch_error); + xbt_assert_error(mismatch_error); } - elm->ID = gras_dynar_length( set->dynar ); - gras_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func); - gras_dynar_set(set->dynar, elm->ID, &elm); + elm->ID = xbt_dynar_length( set->dynar ); + xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func); + xbt_dynar_set(set->dynar, elm->ID, &elm); DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID); } -/** - * gras_set_get_by_name: - * @set: - * @name: Name of the searched cell - * @dst: where to put the found data into - * - * get a data stored in the cell by providing its name. +/** @brief Retrive data by providing its name. + * + * \param set + * \param name Name of the searched cell + * \param dst where to put the found data into */ -gras_error_t gras_set_get_by_name (gras_set_t *set, +xbt_error_t xbt_set_get_by_name (xbt_set_t set, const char *name, - /* OUT */gras_set_elm_t **dst) { - gras_error_t errcode; - errcode = gras_dict_get_ext(set->dict, name, strlen(name), (void**) dst); - DEBUG2("Lookup key %s: %s",name,gras_error_name(errcode)); + /* OUT */xbt_set_elm_t *dst) { + xbt_error_t errcode; + errcode = xbt_dict_get_ext(set->dict, name, strlen(name), (void**) dst); + DEBUG2("Lookup key %s: %s",name,xbt_error_name(errcode)); return errcode; } -/** - * gras_set_get_by_name_ext: - * @set: - * @name: Name of the searched cell - * @name_len: length of the name, when strlen cannot be trusted - * @dst: where to put the found data into + +/** @brief Retrive data by providing its name and the length of the name * - * get a data stored in the cell by providing its name (and the length - * of the name, when strlen cannot be trusted because you don't use a char* - * as name, you weird guy). + * \param set + * \param name Name of the searched cell + * \param name_len length of the name, when strlen cannot be trusted + * \param dst where to put the found data into + * + * This is useful when strlen cannot be trusted because you don't use a char* + * as name, you weirdo. */ -gras_error_t gras_set_get_by_name_ext(gras_set_t *set, +xbt_error_t xbt_set_get_by_name_ext(xbt_set_t set, const char *name, int name_len, - /* OUT */gras_set_elm_t **dst) { + /* OUT */xbt_set_elm_t *dst) { - return gras_dict_get_ext (set->dict, name, name_len, (void**)dst); + return xbt_dict_get_ext (set->dict, name, name_len, (void**)dst); } -/** - * gras_set_get_by_code: - * @set: - * @id: what you're looking for - * @dst: where to put the found data into +/** @brief Retrive data by providing its ID + * + * \param set + * \param id what you're looking for + * \param dst where to put the found data into * - * get a data stored in the cell by providing its id. * @warning, if the ID does not exists, you're getting into trouble */ -gras_error_t gras_set_get_by_id (gras_set_t *set, +xbt_error_t xbt_set_get_by_id (xbt_set_t set, int id, - /* OUT */gras_set_elm_t **dst) { + /* OUT */xbt_set_elm_t *dst) { /* Don't bother checking the bounds, the dynar does so */ - gras_dynar_get(set->dynar,id,dst); + *dst = xbt_dynar_get_as(set->dynar,id,xbt_set_elm_t); DEBUG3("Lookup type of id %d (of %lu): %s", - id, gras_dynar_length(set->dynar), (*dst)->name); + id, xbt_dynar_length(set->dynar), (*dst)->name); return no_error; } @@ -159,63 +150,49 @@ gras_error_t gras_set_get_by_id (gras_set_t *set, /*** *** Cursors ***/ -struct gras_set_cursor_ { - gras_set_t *set; +typedef struct xbt_set_cursor_ { + xbt_set_t set; int val; -}; +} s_xbt_set_cursor_t; -/** - * gras_set_cursor_first: - * @set: on what to let the cursor iterate - * @cursor: dest address - * - * Create the cursor if it does not exists. Rewind it in any case. - */ -void gras_set_cursor_first (gras_set_t *set, - gras_set_cursor_t **cursor) { +/** @brief Create the cursor if it does not exists, rewind it in any case. */ +void xbt_set_cursor_first (xbt_set_t set, + xbt_set_cursor_t *cursor) { if (set != NULL) { if (!*cursor) { DEBUG0("Create the cursor on first use"); - *cursor = gras_new(gras_set_cursor_t,1); - gras_assert0(*cursor, + *cursor = xbt_new(s_xbt_set_cursor_t,1); + xbt_assert0(*cursor, "Malloc error during the creation of the cursor"); } (*cursor)->set = set; - gras_dynar_cursor_first(set->dynar, &( (*cursor)->val) ); + xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) ); } else { *cursor = NULL; } } -/** - * gras_set_cursor_step: - * @cursor: the cursor - * - * Move to the next element. - */ -void gras_set_cursor_step (gras_set_cursor_t *cursor) { - gras_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) ); +/** @brief Move to the next element. */ +void xbt_set_cursor_step (xbt_set_cursor_t cursor) { + xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) ); } -/** - * gras_set_cursor_get_or_free: - * @cursor: the cursor - * @Returns: true if it's ok, false if there is no more data - * - * Get current data +/** @brief Get current data + * + * \return true if it's ok, false if there is no more data */ -int gras_set_cursor_get_or_free (gras_set_cursor_t **curs, - gras_set_elm_t **elm) { - gras_set_cursor_t *cursor; +int xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs, + xbt_set_elm_t *elm) { + xbt_set_cursor_t cursor; if (!curs || !(*curs)) return FALSE; cursor=*curs; - if (! gras_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) { - gras_free(cursor); + if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) { + free(cursor); *curs=NULL; return FALSE; }