struct lhash_node_st {
void *data;
struct lhash_node_st *next;
unsigned long hash;
};
本结构是一个单链表。其中,data用于存放数据地址,next为下一个数据地址,hash为数据哈希计算值。
struct lhash_st {
OPENSSL_LH_NODE **b; //b指针数组用于存放所有的数据,数组中的每一个值为数据链表的头指针
OPENSSL_LH_COMPFUNC comp;//存放数据比较函数地址
OPENSSL_LH_HASHFUNC hash;//hash用于存放计算哈希值函数的地址
unsigned int num_nodes;//链表个数
unsigned int num_alloc_nodes;//为b分配空间的大小
unsigned int p;
unsigned int pmax;
unsigned long up_load; /* load times 256 */
unsigned long down_load; /* load times 256 */
unsigned long num_items;
unsigned long num_expands;
unsigned long num_expand_reallocs;
unsigned long num_contracts;
unsigned long num_contract_reallocs;
TSAN_QUALIFIER unsigned long num_hash_calls;
TSAN_QUALIFIER unsigned long num_comp_calls;
unsigned long num_insert;
unsigned long num_replace;
unsigned long num_delete;
unsigned long num_no_delete;
TSAN_QUALIFIER unsigned long num_retrieve;
TSAN_QUALIFIER unsigned long num_retrieve_miss;
TSAN_QUALIFIER unsigned long num_hash_comps;
int error;
};
b[0]->lhash_node1->lhash_node2->…
b[1]->lhash_node11->lhash_node21->…
…
b[num_nodes - 1]->lhash_node1n->lhash_node2n->…
…
b[num_alloc_nodes]
每个都是一个单向链表,存在多个数据
num_nodes为链表个数 即b中存放的单向链表个数
哈希表就是通过上面的结构体存在于内存中。
实例:
#include <string.h>
// #include <openssl/lhash.h>
#include "/usr/include/openssl/opensslconf.h"
//#include <openssl/opensslconf.h>
//#include "/usr/include/openssl/lhash.h"
#include <openssl/lhash.h>
#include "stdio.h"
typedef struct Student_st
{
char name[20];
int age;
char otherInfo[200];
}Student;
static int Student_cmp(const void *a, const void *b)
{
char *namea=((Student *)a)->name;
char *nameb=((Student *)b)->name;
return strcmp(namea,nameb);
}
static void PrintValue(Student *a)
{
printf("name :%s\n",a->name);
printf("age :%d\n",a->age);
printf("otherInfo : %s\n",a->otherInfo);
}
static void PrintValue_arg(Student *a,void *b)
{
int flag=0;
flag=*(int *)b;
printf("用户输入参数为:%d\n",flag);
printf("name :%s\n",a->name);
printf("age :%d\n",a->age);
printf("otherInfo : %s\n",a->otherInfo);
}
int main()
{
int flag=11;
OPENSSL_LHASH *h;
Student s1={"zcp1",28,"hu bei"},
s2={"forxy2",28,"no info"},
s3={"skp3",24,"student"},
s4={"zhao4_zcp",28,"zcp's name"},
*s5;
void *data;
//生成哈希表
//h=lh_new(NULL,Student_cmp);
h = OPENSSL_LH_new(NULL, Student_cmp);
if(h==NULL)
{
printf("err.\n");
return -1;
}
//往哈希表中添加数据
data=&s1;
//lh_insert(h,data);
OPENSSL_LH_insert(h, data);
data=&s2;
//lh_insert(h,data);
OPENSSL_LH_insert(h, data);
data=&s3;
//lh_insert(h,data);
OPENSSL_LH_insert(h, data);
data=&s4;
//lh_insert(h,data);
OPENSSL_LH_insert(h, data);
//处理哈希表中所有数据,func为外部提供的回调函数,arg为传递给func函数的参数。本函数遍历所有存储在哈希表中的数据,每个数据被func处理。
//lh_doall(h,PrintValue); //并没有按照插入的顺序
OPENSSL_LH_doall(h,(OPENSSL_LH_DOALL_FUNC)PrintValue);
//此参数类似于lh_doall 函数,func为外部提供的回调函数,arg为传递给func函数的参数。本函数遍历所有存储在哈希表中的数据,每个数据被func处理。
//lh_doall_arg(h,PrintValue_arg,(void *)(&flag));
OPENSSL_LH_doall_arg(h,(OPENSSL_LH_DOALL_FUNCARG)PrintValue_arg,(void *)(&flag));
//data=lh_retrieve(h,(const void*)"skp3");// -->Student_cmp peitao
OPENSSL_LH_retrieve(h,(const void*)"skp3");
if(data==NULL)
{
printf("can not look up skp!\n");
lh_free(h);
return -1;
}
s5=data;
printf("student name : %s\n",s5->name);
printf("sutdent age : %d\n",s5->age);
printf("student otherinfo : %s\n",s5->otherInfo);
//lh_free(h);
OPENSSL_LH_free(h);
getchar();
return 0;
}