更新动态散列表
parent
24f3a5d244
commit
11b94b6e7a
|
|
@ -9,13 +9,29 @@ unsigned int conf_hash_func2(const char *key);
|
|||
int conf_hash_search(CONF_ARG *arg,char *key,CONF_VALUE **value);
|
||||
//插入数据
|
||||
void conf_hash_insert(CONF_ARG *arg,CONF_VALUE *value);
|
||||
//复制哈希表
|
||||
void _conf_hash_copy(CONF_ARG *arg,CONF_ARG *new,int len);
|
||||
//检测数据是否在表中
|
||||
int conf_hash_in(CONF_ARG *arg,char *key,int len);
|
||||
|
||||
void conf_value_insert(CONF_ARG *arg,CONF_VALUE *value,int len)
|
||||
int conf_value_insert(CONF_ARG *arg,CONF_VALUE *value,int len)
|
||||
{
|
||||
/* hash为最终的hash值
|
||||
* hash2为临时hash值 */
|
||||
int hash,hash2;
|
||||
|
||||
if(conf_hash_in(arg,value->key,len))
|
||||
{
|
||||
free(value->key);
|
||||
while(*value->value)
|
||||
{
|
||||
free(*value->value);
|
||||
++value->value;
|
||||
}
|
||||
free(value);
|
||||
|
||||
return 0;
|
||||
}
|
||||
//默认使用第一个哈希函数
|
||||
hash=hash2=conf_hash_func1(value->key)%len;
|
||||
|
||||
|
|
@ -35,9 +51,53 @@ void conf_value_insert(CONF_ARG *arg,CONF_VALUE *value,int len)
|
|||
arg[hash]=arg[hash].next;*/
|
||||
conf_hash_insert(&arg[hash],value);
|
||||
|
||||
return 1;
|
||||
//arg[hash].value=value;
|
||||
}
|
||||
|
||||
//复制哈希表
|
||||
void conf_hash_copy(CONF_ARG *new,int new_len,CONF_ARG *old,int old_len)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0;i < old_len;++i)
|
||||
{
|
||||
if(old[i].len)
|
||||
_conf_hash_copy(&old[i],new,new_len);
|
||||
}
|
||||
}
|
||||
|
||||
//动态更新哈希表
|
||||
int conf_hash_update(CONF *conf)
|
||||
{
|
||||
CONF_ARG *new;
|
||||
int len;
|
||||
|
||||
len=conf->size+conf->size*HASH_SP;
|
||||
new=malloc(sizeof(CONF_ARG)*len);
|
||||
if(new == NULL) return CONF_NO_MEM;
|
||||
conf_hash_zero(new,len);
|
||||
conf_hash_copy(new,len,conf->hash_data,conf->size);
|
||||
conf->size=len;
|
||||
free(conf->hash_data);
|
||||
conf->hash_data=new;
|
||||
|
||||
return CONF_OK;
|
||||
}
|
||||
|
||||
//置空哈希表
|
||||
void conf_hash_zero(CONF_ARG *arg,int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0;i < len;++i)
|
||||
{
|
||||
arg[i].len=0;
|
||||
arg[i].value=NULL;
|
||||
arg[i].next=NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*BKDR 哈希算法*/
|
||||
unsigned int conf_hash_func1(const char *key)
|
||||
{
|
||||
|
|
@ -119,3 +179,35 @@ void conf_hash_insert(CONF_ARG *arg,CONF_VALUE *value)
|
|||
arg->next=temp;
|
||||
}
|
||||
}
|
||||
|
||||
void _conf_hash_copy(CONF_ARG *arg,CONF_ARG *new,int len)
|
||||
{
|
||||
while(arg != NULL)
|
||||
{
|
||||
conf_value_insert(new,arg->value,len);
|
||||
|
||||
arg=arg->next;
|
||||
}
|
||||
}
|
||||
|
||||
int conf_hash_in(CONF_ARG *arg,char *key,int len)
|
||||
{
|
||||
CONF_VALUE *value;
|
||||
int hash,hash2;
|
||||
|
||||
hash=hash2=conf_hash_func1(key)%len;
|
||||
if(arg[hash].len == 0)
|
||||
{
|
||||
hash2=conf_hash_func2(key)%len;
|
||||
if(arg[hash2].len == 0)
|
||||
return 0;
|
||||
else if(conf_hash_search(&arg[hash2],key,&value))
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
else if(conf_hash_search(&arg[hash],key,&value))
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue