예제 #1
0
/**
 * @brief Set current data
 * @param cursor the cursor
 * @param data the new data
 * @param free_ctn the function to free the new data
 */
XBT_INLINE void xbt_dict_cursor_set_data(xbt_dict_cursor_t cursor,
                                         void *data,
                                         void_f_pvoid_t free_ctn)
{
  __cursor_not_null(cursor);
  xbt_dictelm_set_data(cursor->dict, cursor->current, data, free_ctn);
}
예제 #2
0
파일: dict.c 프로젝트: apargupta/simgrid
/**
 * \brief Add data to the dict (arbitrary key)
 * \param dict the container
 * \param key the key to set the new data
 * \param key_len the size of the \a key
 * \param data the data to add in the dict
 * \param free_ctn function to call with (\a data as argument) when
 *        \a data is removed from the dictionary
 *
 * Set the \a data in the structure under the \a key, which can be any kind
 * of data, as long as its length is provided in \a key_len.
 */
XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
                                 const char *key, int key_len,
                                 void *data, void_f_pvoid_t free_ctn)
{

    unsigned int hash_code = xbt_str_hash_ext(key, key_len);

    xbt_dictelm_t current, previous = NULL;
    xbt_assert(dict);

    XBT_CDEBUG(xbt_dict,
               "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
               dict->table_size, hash_code & dict->table_size);
    current = dict->table[hash_code & dict->table_size];
    while (current != NULL &&
            (hash_code != current->hash_code || key_len != current->key_len
             || memcmp(key, current->key, key_len))) {
        previous = current;
        current = current->next;
    }

    if (current == NULL) {
        /* this key doesn't exist yet */
        current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
        dict->count++;
        if (previous == NULL) {
            dict->table[hash_code & dict->table_size] = current;
            dict->fill++;
            if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
                xbt_dict_rehash(dict);
        } else {
            previous->next = current;
        }
    } else {
        XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
                   key_len, (char *) current->content,
                   key_len, (char *) data, key_len, (char *) key);
        /* there is already an element with the same key: overwrite it */
        xbt_dictelm_set_data(dict, current, data, free_ctn);
    }
}