From 693db95cd52fd98879bc8c7ae9b42c94525463b9 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Mon, 28 Nov 2011 14:39:44 +0100 Subject: [PATCH 1/1] Define xbt_dict_new_homogeneous(). --- ChangeLog | 3 +++ include/xbt/dict.h | 3 ++- src/xbt/dict.c | 24 ++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 226f5c1efe..63e07eda6c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,9 @@ SimGrid (3.7) NOT RELEASED; urgency=low XBT: * Mallocators: allow value NULL for the reset function. + * Dicts: new function xbt_dict_new_homogeneous(void(*)(void*)) to + create homogeneous dictionaries, where all the elements share the + same free function. -- $date Da SimGrid team diff --git a/include/xbt/dict.h b/include/xbt/dict.h index cb5cde6ef4..5c6f6df546 100644 --- a/include/xbt/dict.h +++ b/include/xbt/dict.h @@ -1,6 +1,6 @@ /* xbt/dict.h -- api to a generic dictionary */ -/* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team. +/* Copyright (c) 2004-2011. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -45,6 +45,7 @@ SG_BEGIN_DECL() /** \brief Dictionary data type (opaque structure) */ typedef struct s_xbt_dict *xbt_dict_t; XBT_PUBLIC(xbt_dict_t) xbt_dict_new(void); +XBT_PUBLIC(xbt_dict_t) xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn); XBT_PUBLIC(void) xbt_dict_free(xbt_dict_t * dict); XBT_PUBLIC(unsigned int) xbt_dict_size(xbt_dict_t dict); diff --git a/src/xbt/dict.c b/src/xbt/dict.c index 2bbf177ef7..dd1b010d75 100644 --- a/src/xbt/dict.c +++ b/src/xbt/dict.c @@ -26,18 +26,38 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, * \see xbt_dict_free() * * Creates and initialize a new dictionary with a default hashtable size. + * The dictionary is heterogeneous: each element can have a different free + * function. */ xbt_dict_t xbt_dict_new(void) +{ + xbt_dict_t dict = xbt_dict_new_homogeneous(NULL); + dict->homogeneous = 0; + + return dict; +} + +/** + * \brief Constructor + * \param free_ctn function to call with (\a data as argument) when + * \a data is removed from the dictionary + * \return pointer to the destination + * \see xbt_dict_new(), xbt_dict_free() + * + * Creates and initialize a new dictionary with a default hashtable size. + * The dictionary is homogeneous: each element share the same free function. + */ +xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn) { xbt_dict_t dict; dict = xbt_new(s_xbt_dict_t, 1); - dict->free_f = NULL; + dict->free_f = free_ctn; dict->table_size = 127; dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1); dict->count = 0; dict->fill = 0; - dict->homogeneous = 0; + dict->homogeneous = 1; return dict; } -- 2.20.1