#include "gras_private.h"
-GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(set,GRAS);
+GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(set,gros,"data container consisting in dict+dynar");
/*####[ Type definition ]####################################################*/
struct gras_set_ {
*
* Creates a new set.
*/
-gras_error_t gras_set_new (gras_set_t **dst) {
- gras_set_t *res=(gras_set_t*)malloc(sizeof(gras_set_t));
+void gras_set_new (gras_set_t **dst) {
+ gras_set_t *res=gras_new(gras_set_t,1);
gras_error_t errcode;
- if (!res)
- RAISE_MALLOC;
-
- TRY(gras_dict_new (&(res->dict)));
- TRY(gras_dynar_new(&(res->dynar), sizeof(void*),NULL));
+ gras_dict_new (&(res->dict));
+ gras_dynar_new(&(res->dynar), sizeof(void*),NULL);
*dst=res;
- return no_error;
}
/**
if (*set) {
gras_dict_free ( &( (*set)->dict ) );
gras_dynar_free( (*set)->dynar );
- free(*set);
+ gras_free(*set);
*set=NULL;
}
}
* elm->name_len is used as is unless it's <= 0 (in which case it's recomputed);
* elm->ID is attributed automatically.
*/
-gras_error_t gras_set_add (gras_set_t *set,
- gras_set_elm_t *elm,
- void_f_pvoid_t *free_func) {
+void gras_set_add (gras_set_t *set,
+ gras_set_elm_t *elm,
+ void_f_pvoid_t *free_func) {
gras_error_t errcode;
gras_set_elm_t *found_in_dict;
elm->name_len = strlen(elm->name);
}
- errcode = gras_dict_retrieve_ext (set->dict,
+ errcode = gras_dict_get_ext (set->dict,
elm->name, elm->name_len,
- (void**) &found_in_dict);
+ (void**)&found_in_dict);
if (errcode == no_error) {
- elm->ID=found_in_dict->ID;
- DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
- TRY(gras_dict_insert_ext(set->dict, elm->name, elm->name_len, elm, free_func));
- TRY(gras_dynar_set(set->dynar, elm->ID, &elm));
- return no_error;
-
- } else if (errcode != mismatch_error) {
- return errcode; /* I expected mismatch_error */
+ if (elm == found_in_dict) {
+ DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
+ elm->name, elm->ID);
+ return;
+ } 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);
+ return;
+ }
+ } else {
+ gras_assert_error(mismatch_error);
}
elm->ID = gras_dynar_length( set->dynar );
- TRY(gras_dict_insert_ext(set->dict, elm->name, elm->name_len, elm, free_func));
- TRY(gras_dynar_set(set->dynar, elm->ID, &elm));
- DEBUG2("Insertion 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);
+ DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
- return no_error;
}
/**
* @name: Name of the searched cell
* @dst: where to put the found data into
*
- * Retrieve a data stored in the cell by providing its name.
+ * get a data stored in the cell by providing its name.
*/
gras_error_t gras_set_get_by_name (gras_set_t *set,
const char *name,
/* OUT */gras_set_elm_t **dst) {
-
- return gras_dict_retrieve_ext(set->dict, name, strlen(name), (void**) 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));
+ return errcode;
}
/**
* gras_set_get_by_name_ext:
* @name_len: length of the name, when strlen cannot be trusted
* @dst: where to put the found data into
*
- * Retrieve a data stored in the cell by providing its name (and the length
+ * 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).
*/
int name_len,
/* OUT */gras_set_elm_t **dst) {
- return gras_dict_retrieve_ext (set->dict, name, name_len, (void**)dst);
+ return gras_dict_get_ext (set->dict, name, name_len, (void**)dst);
}
/**
* gras_set_get_by_code:
* @set:
- * @name: Name of the searched cell
- * @name_len: length of the name, when strlen cannot be trusted
+ * @id: what you're looking for
* @dst: where to put the found data into
*
- * Retrieve 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).
+ * 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,
int id,
/* OUT */gras_set_elm_t **dst) {
- if (id < gras_dynar_length(set->dynar) &&
- id >= 0) {
- gras_dynar_get(set->dynar,id,dst);
-
- } else {
- DEBUG1("Cannot get ID %d: out of bound", id);
- return mismatch_error;
- }
+
+ /* Don't bother checking the bounds, the dynar does so */
+
+ gras_dynar_get(set->dynar,id,dst);
+ DEBUG3("Lookup type of id %d (of %lu): %s",
+ id, gras_dynar_length(set->dynar), (*dst)->name);
+
return no_error;
}
if (set != NULL) {
if (!*cursor) {
DEBUG0("Create the cursor on first use");
- *cursor = (gras_set_cursor_t*)malloc(sizeof(gras_set_cursor_t));
+ *cursor = gras_new(gras_set_cursor_t,1);
gras_assert0(*cursor,
"Malloc error during the creation of the cursor");
}
cursor=*curs;
if (! gras_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {
- free(cursor);
+ gras_free(cursor);
*curs=NULL;
return FALSE;
}