static bool dict_has_key_internal(Dict dict, const char * key, ListItr * pitr) { struct gds_kvpair needle = { .key = (char *) key }; const size_t hash = djb2hash(key) % dict->num_buckets; List list = dict->buckets[hash]; ListItr itr; if ( (itr = list_find_itr(list, (void *) &needle)) ) { if ( pitr ) { *pitr = itr; } return true; } else { if ( pitr ) { *pitr = NULL; } return false; } } static size_t djb2hash(const char * str) { size_t hash = 5381; int c; while ( (c = *str++) ) { hash = ((hash << 5) + hash) + c; } return hash; }
bool dict_insert(Dict dict, const char * key, ...) { ListItr itr; if ( dict_has_key_internal(dict, key, &itr) ) { struct gds_kvpair * pair; list_get_value_itr(itr, &pair); if ( dict->free_on_destroy ) { /* Free existing item if necessary */ gdt_free(&pair->value); } va_list ap; va_start(ap, key); gdt_set_value(&pair->value, dict->type, NULL, ap); va_end(ap); } else { va_list ap; va_start(ap, key); struct gds_kvpair * new_pair = gds_kvpair_create(key, dict->type, ap); va_end(ap); if ( !new_pair ) { return false; } const size_t hash = djb2hash(key) % dict->num_buckets; if ( !list_append(dict->buckets[hash], (void *) new_pair) ) { gds_kvpair_destroy(new_pair, dict->free_on_destroy); return false; } } return true; }
Str::LocaleString::LocaleString(const char *s) : m_id(static_cast<INT32>(djb2hash(s))) {}
UINT32 PrefsHashFunctions::Hash(const void *k) { return static_cast<UINT32>(djb2hash(*static_cast<const uni_char * const *>(k))); }
/* static */ UINT32 OpGenericString8HashTable::HashString(const char* str, unsigned str_length, BOOL case_sensitive) { return static_cast<UINT32>(case_sensitive ? djb2hash(str, str_length) : djb2hash_nocase(str, str_length)); }
/* static */ UINT32 OpGenericStringHashTable::HashString(const uni_char* str, BOOL case_sensitive) { return static_cast<UINT32>(case_sensitive ? djb2hash(str) : djb2hash_nocase(str)); }