mirror of
https://github.com/lxsang/ant-http
synced 2025-07-13 13:04:23 +02:00
allow ordered dictionary
This commit is contained in:
158
lib/dictionary.c
158
lib/dictionary.c
@ -31,119 +31,139 @@ dictionary_t dict()
|
||||
}
|
||||
dictionary_t dict_n(unsigned int size)
|
||||
{
|
||||
dictionary_t d = (dictionary_t) malloc(sizeof(struct __dict));
|
||||
if(!d) return NULL;
|
||||
d->map= (map_t)malloc(size*sizeof(chain_t));
|
||||
if(!d->map)
|
||||
dictionary_t d = (dictionary_t)malloc(sizeof(struct __dict));
|
||||
if (!d)
|
||||
return NULL;
|
||||
d->map = (map_t)malloc(size * sizeof(chain_t));
|
||||
if (!d->map)
|
||||
{
|
||||
free(d);
|
||||
return NULL;
|
||||
}
|
||||
d->cap = size;
|
||||
d->size = 0;
|
||||
for(unsigned int i=0; i< size;i++)
|
||||
for (unsigned int i = 0; i < size; i++)
|
||||
d->map[i] = NULL;
|
||||
return d;
|
||||
}
|
||||
chain_t dlookup(dictionary_t dic,const char* key)
|
||||
chain_t dlookup(dictionary_t dic, const char *key)
|
||||
{
|
||||
chain_t np;
|
||||
if(dic->map == NULL) return NULL;
|
||||
for (np = dic->map[hash(key,dic->cap)]; np != NULL; np = np->next)
|
||||
{
|
||||
if(!np || !np->key)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (strcmp(key, np->key) == 0)
|
||||
return np; /* found */
|
||||
}
|
||||
return NULL; /* not found */
|
||||
}
|
||||
chain_t __put_el_with_key(dictionary_t dic, const char* key)
|
||||
{
|
||||
chain_t np;
|
||||
unsigned hashval;
|
||||
if(dic->map == NULL) return NULL;
|
||||
if ((np = dlookup(dic,key)) == NULL) { /* not found */
|
||||
np = (chain_t) malloc(sizeof(*np));
|
||||
if (np == NULL || (np->key = strdup(key)) == NULL)
|
||||
return NULL;
|
||||
np->value = NULL;
|
||||
hashval = hash(key, dic->cap);
|
||||
np->next = dic->map[hashval];
|
||||
dic->map[hashval] = np;
|
||||
dic->size++;
|
||||
}
|
||||
// found
|
||||
return np;
|
||||
}
|
||||
chain_t dput(dictionary_t dic,const char* key, void* value)
|
||||
{
|
||||
chain_t np = __put_el_with_key(dic,key);
|
||||
if(np == NULL)
|
||||
if (dic->map == NULL)
|
||||
return NULL;
|
||||
for (np = dic->map[hash(key, dic->cap)]; np != NULL; np = np->next)
|
||||
{
|
||||
if(value) free(value);
|
||||
if (!np || !np->key)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (strcmp(key, np->key) == 0)
|
||||
return np; /* found */
|
||||
}
|
||||
return NULL; /* not found */
|
||||
}
|
||||
chain_t __put_el_with_key(dictionary_t dic, const char *key)
|
||||
{
|
||||
chain_t np, it;
|
||||
unsigned hashval;
|
||||
if (dic->map == NULL)
|
||||
return NULL;
|
||||
if ((np = dlookup(dic, key)) == NULL)
|
||||
{ /* not found */
|
||||
np = (chain_t)malloc(sizeof(*np));
|
||||
if (np == NULL || (np->key = strdup(key)) == NULL)
|
||||
return NULL;
|
||||
np->value = NULL;
|
||||
hashval = hash(key, dic->cap);
|
||||
it = dic->map[hashval];
|
||||
while (it && it->next != NULL)
|
||||
{
|
||||
it = it->next;
|
||||
}
|
||||
if (it == NULL)
|
||||
{
|
||||
np->next = dic->map[hashval];
|
||||
dic->map[hashval] = np;
|
||||
}
|
||||
else
|
||||
{
|
||||
it->next = np;
|
||||
np->next = NULL;
|
||||
}
|
||||
|
||||
dic->size++;
|
||||
}
|
||||
// found
|
||||
return np;
|
||||
}
|
||||
chain_t dput(dictionary_t dic, const char *key, void *value)
|
||||
{
|
||||
chain_t np = __put_el_with_key(dic, key);
|
||||
if (np == NULL)
|
||||
{
|
||||
if (value)
|
||||
free(value);
|
||||
return NULL;
|
||||
}
|
||||
if(np->value && value) free(np->value);
|
||||
if (np->value && value)
|
||||
free(np->value);
|
||||
np->value = value;
|
||||
return np;
|
||||
return np;
|
||||
}
|
||||
chain_t dremove(dictionary_t dic, const char* key)
|
||||
chain_t dremove(dictionary_t dic, const char *key)
|
||||
{
|
||||
if(dic->map == NULL) return 0;
|
||||
if (dic->map == NULL)
|
||||
return 0;
|
||||
int hashval = hash(key, dic->cap);
|
||||
chain_t np = dic->map[hashval];
|
||||
if(np!=NULL && strcmp(key,np->key)==0)
|
||||
if (np != NULL && strcmp(key, np->key) == 0)
|
||||
{
|
||||
dic->size--;
|
||||
dic->map[hashval] = np->next;
|
||||
np->next = NULL;
|
||||
return np;
|
||||
}
|
||||
for (np= dic->map[hashval]; np != NULL; np = np->next)
|
||||
if (np->next!=NULL&&strcmp(key, np->next->key) == 0)
|
||||
{
|
||||
for (np = dic->map[hashval]; np != NULL; np = np->next)
|
||||
if (np->next != NULL && strcmp(key, np->next->key) == 0)
|
||||
{
|
||||
chain_t ret = np->next;
|
||||
np->next = np->next->next; /* found */
|
||||
np->next = np->next->next; /* found */
|
||||
dic->size--;
|
||||
ret->next = NULL;
|
||||
return ret;
|
||||
}
|
||||
return NULL; /* not found */
|
||||
|
||||
return ret;
|
||||
}
|
||||
return NULL; /* not found */
|
||||
}
|
||||
void* dvalue(dictionary_t dic, const char* key)
|
||||
void *dvalue(dictionary_t dic, const char *key)
|
||||
{
|
||||
chain_t as = dlookup(dic,key);
|
||||
if(as == NULL) return NULL;
|
||||
chain_t as = dlookup(dic, key);
|
||||
if (as == NULL)
|
||||
return NULL;
|
||||
return as->value;
|
||||
}
|
||||
|
||||
void free_association(chain_t * asoc)
|
||||
void free_association(chain_t *asoc)
|
||||
{
|
||||
|
||||
while( (*asoc) != NULL )
|
||||
|
||||
while ((*asoc) != NULL)
|
||||
{
|
||||
chain_t a = *asoc;
|
||||
(* asoc) = (*asoc) ->next;
|
||||
|
||||
if(a->key)
|
||||
(*asoc) = (*asoc)->next;
|
||||
|
||||
if (a->key)
|
||||
{
|
||||
free(a->key);
|
||||
if(a->value) free(a->value);
|
||||
if (a->value)
|
||||
free(a->value);
|
||||
}
|
||||
free(a);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void freedict(dictionary_t dic){
|
||||
for(unsigned int i = 0; i < dic->cap; i++)
|
||||
void freedict(dictionary_t dic)
|
||||
{
|
||||
for (unsigned int i = 0; i < dic->cap; i++)
|
||||
free_association(&(dic->map[i]));
|
||||
free(dic->map);
|
||||
free(dic);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user