-
- unsigned int hash_code = xbt_lib_hash_ext(key, key_len);
-
- xbt_libelm_t current, previous = NULL;
- if(!lib) xbt_die("Lib is NULL!");
-
- XBT_DEBUG("ADD '%.*s' hash = '%d', size = '%d', & = '%d' to level : %d",
- key_len,
- key,
- hash_code,
- lib->table_size,
- hash_code & lib->table_size,
- level);
-
- current = lib->table[hash_code & lib->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_libelm_new(key, key_len, hash_code, data,level,lib->levels);
- lib->count++;
- if (previous == NULL) {
- lib->table[hash_code & lib->table_size] = current;
- lib->fill++;
- if ((lib->fill * 100) / (lib->table_size + 1) > MAX_FILL_PERCENT)
- xbt_lib_rehash(lib);
- } else {
- previous->next = current;
- }
- }
- else
- if( (&(current->content))[level] == NULL )/* this key exist but level is empty */
- {
- (&(current->content))[level] = data;
- }
- else
- {/* there is already an element with the same key: overwrite it */
- XBT_INFO("Replace %.*s by %.*s under key %.*s",
- key_len, (char *) (&(current->content))[level],
- key_len, (char *) data, key_len, (char *) key);
- if (current->content != NULL) {
- free((&(current->content))[level]);
- }
- (&(current->content))[level] = data;
- }
-}
-
-/**
- * Returns the hash code of a string.
- */
-static unsigned int xbt_lib_hash_ext(const char *str,
- int str_len)
-{
-
-
-#ifdef DJB2_HASH_FUNCTION
- /* fast implementation of djb2 algorithm */
- int c;
- register unsigned int hash = 5381;
-
- while (str_len--) {
- c = *str++;
- hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
- }
-# elif defined(FNV_HASH_FUNCTION)
- register unsigned int hash = 0x811c9dc5;
- unsigned char *bp = (unsigned char *) str; /* start of buffer */
- unsigned char *be = bp + str_len; /* beyond end of buffer */
-
- while (bp < be) {
- /* multiply by the 32 bit FNV magic prime mod 2^32 */
- hash +=
- (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
- (hash << 24);
-
- /* xor the bottom with the current octet */
- hash ^= (unsigned int) *bp++;