text stringlengths 0 357 |
|---|
} |
/* Attempt to locate an element of the hash table pH with a key |
* that matches pKey,nKey. Return the data for this element if it is |
* found, or NULL if there is no match. |
*/ |
void* dc_hash_find(const dc_hash_t *pH, const void *pKey, int nKey) |
{ |
int h; /* A hash on key */ |
dc_hashelem_t *elem; /* The element that matches key */ |
int (*xHash)(const void*,int); /* The hash function */ |
if (pH==0 || pH->ht==0) return 0; |
xHash = hashFunction(pH->keyClass); |
assert( xHash!=0); |
h = (*xHash)(pKey,nKey); |
assert( (pH->htsize & (pH->htsize-1))==0); |
elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1)); |
return elem ? elem->data : 0; |
} |
/* Insert an element into the hash table pH. The key is pKey,nKey |
* and the data is "data". |
* |
* If no element exists with a matching key, then a new |
* element is created. A copy of the key is made if the copyKey |
* flag is set. NULL is returned. |
* |
* If another element already exists with the same key, then the |
* new data replaces the old data and the old data is returned. |
* The key is not copied in this instance. If a malloc fails, then |
* the new data is returned and the hash table is unchanged. |
* |
* If the "data" parameter to this function is NULL, then the |
* element corresponding to "key" is removed from the hash table. |
*/ |
void* dc_hash_insert(dc_hash_t *pH, const void *pKey, int nKey, void *data) |
{ |
int hraw; /* Raw hash value of the key */ |
int h; /* the hash of the key modulo hash table size */ |
dc_hashelem_t *elem; /* Used to loop thru the element list */ |
dc_hashelem_t *new_elem; /* New element added to the pH */ |
int (*xHash)(const void*,int); /* The hash function */ |
assert( pH!=0); |
xHash = hashFunction(pH->keyClass); |
assert( xHash!=0); |
hraw = (*xHash)(pKey, nKey); |
assert( (pH->htsize & (pH->htsize-1))==0); |
h = hraw & (pH->htsize-1); |
elem = findElementGivenHash(pH,pKey,nKey,h); |
if (elem) |
{ |
void *old_data = elem->data; |
if (data==0) |
{ |
removeElementGivenHash(pH,elem,h); |
} |
else |
{ |
elem->data = data; |
} |
return old_data; |
} |
if (data==0) return 0; |
new_elem = (dc_hashelem_t*)sjhashMalloc( sizeof(dc_hashelem_t)); |
if (new_elem==0) return data; |
if (pH->copyKey && pKey!=0) |
{ |
new_elem->pKey = sjhashMallocRaw( nKey); |
if (new_elem->pKey==0) |
{ |
sjhashFree(new_elem); |
return data; |
} |
memcpy((void*)new_elem->pKey, pKey, nKey); |
} |
else |
{ |
new_elem->pKey = (void*)pKey; |
} |
new_elem->nKey = nKey; |
pH->count++; |
if (pH->htsize==0) |
{ |
rehash(pH,8); |
if (pH->htsize==0) |
{ |
pH->count = 0; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.