提交 71a50956 编写于 作者: A antirez

dict.c: added two lower level methods for directly manipulating hash entries....

dict.c: added two lower level methods for directly manipulating hash entries. This is useful in order to set 64 bit integers as values directly inside the hash entry (in order to save memory), without casting, and even in 32 bit builds.
上级 66d8818c
...@@ -258,6 +258,30 @@ static void _dictRehashStep(dict *d) { ...@@ -258,6 +258,30 @@ static void _dictRehashStep(dict *d) {
/* Add an element to the target hash table */ /* Add an element to the target hash table */
int dictAdd(dict *d, void *key, void *val) int dictAdd(dict *d, void *key, void *val)
{
dictEntry *entry = dictAddRaw(d,key);
if (!entry) return DICT_ERR;
dictSetHashVal(d, entry, val);
return DICT_OK;
}
/* Low level add. This function adds the entry but instead of setting
* a value returns the dictEntry structure to the user, that will make
* sure to fill the value field as he wishes.
*
* This function is also directly expoed to user API to be called
* mainly in order to store non-pointers inside the hash value, example:
*
* entry = dictAddRaw(dict,mykey);
* if (entry != NULL) dictSetValSignedInteger(entry,1000);
*
* Return values:
*
* If key already exists NULL is returned.
* If key was added, the hash entry is returned to be manipulated by the caller.
*/
dictEntry *dictAddRaw(dict *d, void *key)
{ {
int index; int index;
dictEntry *entry; dictEntry *entry;
...@@ -268,7 +292,7 @@ int dictAdd(dict *d, void *key, void *val) ...@@ -268,7 +292,7 @@ int dictAdd(dict *d, void *key, void *val)
/* Get the index of the new element, or -1 if /* Get the index of the new element, or -1 if
* the element already exists. */ * the element already exists. */
if ((index = _dictKeyIndex(d, key)) == -1) if ((index = _dictKeyIndex(d, key)) == -1)
return DICT_ERR; return NULL;
/* Allocate the memory and store the new entry */ /* Allocate the memory and store the new entry */
ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0]; ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];
...@@ -279,8 +303,7 @@ int dictAdd(dict *d, void *key, void *val) ...@@ -279,8 +303,7 @@ int dictAdd(dict *d, void *key, void *val)
/* Set the hash entry fields. */ /* Set the hash entry fields. */
dictSetHashKey(d, entry, key); dictSetHashKey(d, entry, key);
dictSetHashVal(d, entry, val); return entry;
return DICT_OK;
} }
/* Add an element, discarding the old if the key already exists. /* Add an element, discarding the old if the key already exists.
...@@ -308,6 +331,18 @@ int dictReplace(dict *d, void *key, void *val) ...@@ -308,6 +331,18 @@ int dictReplace(dict *d, void *key, void *val)
return 0; return 0;
} }
/* dictReplaceRaw() is simply a version of dictAddRaw() that always
* returns the hash entry of the specified key, even if the key already
* exists and can't be added (in that case the entry of the already
* existing key is returned.)
*
* See dictAddRaw() for more information. */
dictEntry *dictReplaceRaw(dict *d, void *key) {
dictEntry *entry = dictFind(d,key);
return entry ? entry : dictAddRaw(d,key);
}
/* Search and remove an element */ /* Search and remove an element */
static int dictGenericDelete(dict *d, const void *key, int nofree) static int dictGenericDelete(dict *d, const void *key, int nofree)
{ {
......
...@@ -133,7 +133,9 @@ typedef struct dictIterator { ...@@ -133,7 +133,9 @@ typedef struct dictIterator {
dict *dictCreate(dictType *type, void *privDataPtr); dict *dictCreate(dictType *type, void *privDataPtr);
int dictExpand(dict *d, unsigned long size); int dictExpand(dict *d, unsigned long size);
int dictAdd(dict *d, void *key, void *val); int dictAdd(dict *d, void *key, void *val);
dictEntry *dictAddRaw(dict *d, void *key);
int dictReplace(dict *d, void *key, void *val); int dictReplace(dict *d, void *key, void *val);
dictEntry *dictReplaceRaw(dict *d, void *key);
int dictDelete(dict *d, const void *key); int dictDelete(dict *d, const void *key);
int dictDeleteNoFree(dict *d, const void *key); int dictDeleteNoFree(dict *d, const void *key);
void dictRelease(dict *d); void dictRelease(dict *d);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册