You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libublsettings/source/libublsettings-hash.c

88 lines
3.3 KiB

#include "libublsettings.h"
yon_hash *yon_hash_new(int size, unsigned int(*hash_func)(const char *)){
yon_hash *hash = malloc(sizeof(yon_hash));
memset(hash,0,sizeof(yon_hash));
hash->data = malloc(sizeof(yon_hash_element)*size);
hash->size = size;
hash->hash_func = hash_func;
return hash;
}
yon_hash_element *yon_hash_element_new(const char *key, const char *data){
yon_hash_element *element = malloc(sizeof(yon_hash_element));
memset(element,0,sizeof(yon_hash_element));
element->key = yon_char_new(key);
element->data = yon_char_new(data);
return element;
}
int yon_hash_insert(yon_hash *target, const char *key, void *data){
if (target&&target->hash_func&&!yon_char_is_empty(key)&&data){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
yon_hash_element *element = yon_hash_element_new(key,data);
yon_hash_element *last = (yon_hash_element *)yon_dictionary_get_last((dictionary*)target->data[hash]);
last->next=(struct yon_hash_element*)element;
element->prev=(struct yon_hash_element*)last;
element->first=(struct yon_hash_element*)last->first;
return 1;
} else {
target->data = malloc(sizeof(yon_hash_element*));
target->data[0] = yon_hash_element_new(key,data);
return 1;
}
}
return 0;
}
int yon_hash_remove(yon_hash *target, const char *key){
if (target&&target->hash_func&&!yon_char_is_empty(key)){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
dictionary *dict = yon_dictionary_get((dictionary**)&target->data[hash],(char*)key);
target->data[hash]=(yon_hash_element*)yon_dictionary_rip(dict);
}
}
return 0;
}
void *yon_hash_lookup(yon_hash *target, const char *key){
if (target&&target->size>0&&!yon_char_is_empty(key)){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
dictionary *dict = yon_dictionary_get((dictionary**)&target->data[hash],(char*)key);
return yon_dictionary_get_data(dict,void*);
}
}
return NULL;
}
int yon_hash_contains(yon_hash *target, const char *key){
if (target&&!yon_char_is_empty(key)){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
if (yon_dictionary_get((dictionary**)&target->data[hash],key)) return 1;
}
}
return 0;
}
int yon_hash_add(yon_hash *target,const char *key){
if (target&&target->size>0&&!yon_char_is_empty(key)){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
yon_hash_element *element = yon_hash_element_new(key,NULL);
yon_hash_element *last = (yon_hash_element *)yon_dictionary_get_last((dictionary*)target->data[hash]);
last->next=(struct yon_hash_element*)element;
element->prev=(struct yon_hash_element*)last;
element->first=(struct yon_hash_element*)last->first;
return 1;
} else {
target->data = malloc(sizeof(yon_hash_element*));
target->data[0] = yon_hash_element_new(key,NULL);
return 1;
}
}
}