更新数据结构与哈希函数
parent
33fcc94004
commit
23715c44bf
|
|
@ -5,10 +5,50 @@ int hash_func1(const char *key);
|
|||
int hash_func2(const char *key);
|
||||
|
||||
int conf_value_insert(CONF_ARG *arg)
|
||||
{
|
||||
}
|
||||
|
||||
CONF_VALUE *conf_value_get(CONF *conf,const char *key)
|
||||
{}
|
||||
|
||||
CONF_ARG *conf_value_get(CONF *conf,const char *key)
|
||||
CONF_VALUE **conf_value_get_all(CONF *conf)
|
||||
{}
|
||||
|
||||
CONF_ARG *conf_value_get_all(CONF *conf)
|
||||
{}
|
||||
/*BKDR 哈希算法*/
|
||||
int hash_func1(const char *key)
|
||||
{
|
||||
unsigned int seed=131;
|
||||
unsigned int hash=0;
|
||||
|
||||
while(*key)
|
||||
{
|
||||
hash=hash*seed+*key;
|
||||
|
||||
++key;
|
||||
}
|
||||
|
||||
return hash&0x7FFFFFFF;
|
||||
}
|
||||
|
||||
/*AP 哈希算法*/
|
||||
int hash_func2(const char *key)
|
||||
{
|
||||
unsigned int hash=0;
|
||||
int i;
|
||||
|
||||
for(i=0;key[i];++i)
|
||||
{
|
||||
if((i & 1) == 0)
|
||||
{
|
||||
hash^=((hash << 7)^*key^(hash >> 3));
|
||||
++key;
|
||||
}
|
||||
else
|
||||
{
|
||||
hash^=(~((hash << 11)^*key^(hash >> 5)));
|
||||
++key;
|
||||
}
|
||||
}
|
||||
|
||||
return hash&0x7FFFFFFF;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,39 +6,47 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*值链表数据结构*/
|
||||
typedef struct node
|
||||
{
|
||||
char *key
|
||||
char *value;
|
||||
struct node *next;
|
||||
}CONF_VALUE;
|
||||
|
||||
/* 键/值对数据结构
|
||||
* hash为使用的hash函数:0为第一个哈希算法 1:为第二个哈希算法
|
||||
* len为当前结点个数
|
||||
* next为冲突时的链表
|
||||
*/
|
||||
typedef struct arg
|
||||
{
|
||||
int hash;
|
||||
int len;
|
||||
CONF_VALUE *value;
|
||||
|
||||
struct arg *next;
|
||||
}CONF_ARG;
|
||||
|
||||
/* conf-c的数据结构
|
||||
* fp为指向配置文件的FILE指针
|
||||
* len为键/值对个数
|
||||
* conf为存储键/值的hash函数指针
|
||||
* hash_data为存储键/值的数据结构
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
FILE *fp;
|
||||
int len;
|
||||
conf_hash *conf;
|
||||
CONF_ARG *hash_data;
|
||||
}CONF;
|
||||
|
||||
/*值链表数据结构*/
|
||||
typedef struct node
|
||||
{
|
||||
char *value;
|
||||
struct node *next;
|
||||
}CONF_VALUE;
|
||||
|
||||
//键/值对数据结构
|
||||
typedef struct
|
||||
{
|
||||
char *key;
|
||||
CONF_VALUE *value;
|
||||
}CONF_ARG;
|
||||
|
||||
//插入数据,key为键,value为值
|
||||
int conf_value_insert(CONF_ARG *arg);
|
||||
|
||||
//根据一个键查找数据
|
||||
CONF_ARG conf_value_get(CONF *conf,const char *key);
|
||||
CONF_VALUE *conf_value_get(CONF *conf,const char *key);
|
||||
|
||||
//得到所有键值对
|
||||
CONF_ARG conf_value_get_all(CONF *conf);
|
||||
CONF_VALUE **conf_value_get_all(CONF *conf);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Reference in New Issue