2015-11-24 17:58:32 +01:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2015 LE Xuan Sang xsang.le@gmail.com
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
2020-08-25 16:40:24 +02:00
|
|
|
#include <string.h>
|
2019-12-15 12:10:06 +01:00
|
|
|
#include "utils.h"
|
2020-08-25 16:40:24 +02:00
|
|
|
#include "dictionary.h"
|
2015-10-22 11:39:11 +02:00
|
|
|
|
2019-12-15 12:10:06 +01:00
|
|
|
dictionary_t dict()
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
return dict_n(DHASHSIZE);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
free(d);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
d->cap = size;
|
|
|
|
d->size = 0;
|
|
|
|
for(unsigned int i=0; i< size;i++)
|
|
|
|
d->map[i] = NULL;
|
2015-10-22 11:39:11 +02:00
|
|
|
return d;
|
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t dlookup(dictionary_t dic,const char* key)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t np;
|
|
|
|
if(dic->map == NULL) return NULL;
|
|
|
|
for (np = dic->map[hash(key,dic->cap)]; np != NULL; np = np->next)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
if(!np || !np->key)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (strcmp(key, np->key) == 0)
|
|
|
|
return np; /* found */
|
|
|
|
}
|
|
|
|
return NULL; /* not found */
|
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t __put_el_with_key(dictionary_t dic, const char* key)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t np;
|
2015-10-22 11:39:11 +02:00
|
|
|
unsigned hashval;
|
2019-12-15 12:10:06 +01:00
|
|
|
if(dic->map == NULL) return NULL;
|
2015-10-22 11:39:11 +02:00
|
|
|
if ((np = dlookup(dic,key)) == NULL) { /* not found */
|
2019-12-15 12:10:06 +01:00
|
|
|
np = (chain_t) malloc(sizeof(*np));
|
2015-10-22 11:39:11 +02:00
|
|
|
if (np == NULL || (np->key = strdup(key)) == NULL)
|
|
|
|
return NULL;
|
2019-12-15 12:10:06 +01:00
|
|
|
np->value = NULL;
|
|
|
|
hashval = hash(key, dic->cap);
|
|
|
|
np->next = dic->map[hashval];
|
|
|
|
dic->map[hashval] = np;
|
|
|
|
dic->size++;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
// found
|
2015-10-22 11:39:11 +02:00
|
|
|
return np;
|
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t dput(dictionary_t dic,const char* key, void* value)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t np = __put_el_with_key(dic,key);
|
2018-10-05 19:01:39 +02:00
|
|
|
if(np == NULL)
|
|
|
|
{
|
|
|
|
if(value) free(value);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(np->value && value) free(np->value);
|
2015-11-24 17:58:32 +01:00
|
|
|
np->value = value;
|
2015-10-22 11:39:11 +02:00
|
|
|
return np;
|
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t dremove(dictionary_t dic, const char* key)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
if(dic->map == NULL) return 0;
|
|
|
|
int hashval = hash(key, dic->cap);
|
|
|
|
chain_t np = dic->map[hashval];
|
2015-10-22 11:39:11 +02:00
|
|
|
if(np!=NULL && strcmp(key,np->key)==0)
|
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
dic->size--;
|
|
|
|
dic->map[hashval] = np->next;
|
|
|
|
np->next = NULL;
|
|
|
|
return np;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
for (np= dic->map[hashval]; np != NULL; np = np->next)
|
2015-10-22 11:39:11 +02:00
|
|
|
if (np->next!=NULL&&strcmp(key, np->next->key) == 0)
|
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t ret = np->next;
|
2015-10-22 11:39:11 +02:00
|
|
|
np->next = np->next->next; /* found */
|
2019-12-15 12:10:06 +01:00
|
|
|
dic->size--;
|
|
|
|
ret->next = NULL;
|
|
|
|
return ret;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
return NULL; /* not found */
|
2015-10-22 11:39:11 +02:00
|
|
|
|
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
void* dvalue(dictionary_t dic, const char* key)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t as = dlookup(dic,key);
|
2015-10-22 11:39:11 +02:00
|
|
|
if(as == NULL) return NULL;
|
2015-11-24 17:58:32 +01:00
|
|
|
return as->value;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
2019-12-15 12:10:06 +01:00
|
|
|
void free_association(chain_t * asoc)
|
2018-03-02 19:04:00 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
while( (*asoc) != NULL )
|
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t a = *asoc;
|
2018-03-02 19:04:00 +01:00
|
|
|
(* asoc) = (*asoc) ->next;
|
|
|
|
|
|
|
|
if(a->key)
|
|
|
|
{
|
|
|
|
free(a->key);
|
|
|
|
if(a->value) free(a->value);
|
|
|
|
}
|
|
|
|
free(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-15 12:10:06 +01:00
|
|
|
void freedict(dictionary_t dic){
|
|
|
|
for(unsigned int i = 0; i < dic->cap; i++)
|
|
|
|
free_association(&(dic->map[i]));
|
|
|
|
free(dic->map);
|
2018-03-02 19:04:00 +01:00
|
|
|
free(dic);
|
|
|
|
}
|
|
|
|
|
2015-10-22 11:39:11 +02:00
|
|
|
|