📄 hashtab.c
字号:
#include "global.h"
int hash_char(char * key)
{
int temp = 0;
int i = 0;
while (key[i] != '\0') {
temp = ((temp << SHIFT) + key[i]) % SIZE;
++i;
}
return temp;
}
int hash_int(int connfd)
{
return (connfd*2+3)%SIZE;
}
/*initialize the two hash tables*/
void init_hash_tab()
{
int i;
for(i = 0; i < SIZE; i++) {
hash_table[i] = NULL;
addr_table[i] = NULL;
}
}
/*insert a record into the hash_table table*/
bucket_list hash_insert(char * name)
{
int h = hash_char(name);
bucket_list l = hash_table[h];
l = (bucket_list) malloc(sizeof(struct bucket_list_rec));
if(l == NULL) {
log_err("malloc fails!\n");
}
l->name = name;
l->connfd = -1;
l->next = hash_table[h];
hash_table[h] = l;
return l;
}
/*insert a record into the addr_table table*/
addr_list addr_insert(int connfd)
{
int h = hash_int(connfd);
addr_list a = addr_table[h];
a = (addr_list)malloc(sizeof(struct addr_list_rec));
if(a == NULL) {
log_err("malloc fails!\n");
}
a->name = NULL;
a->connfd = connfd;
a->next = addr_table[h];
addr_table[h] = a;
return a;
}
/*lookup a record from the hash_table table*/
bucket_list hash_lookup(char * name)
{
int h = hash_char(name);
bucket_list l = hash_table[h];
while((l != NULL) && (strcmp(name,l->name) != 0))
l = l->next;
if(l == NULL) return (bucket_list)0;
else return l;
}
/*lookup a record from the addr_table table*/
addr_list addr_lookup(int connfd)
{
int h = hash_int(connfd);
addr_list a = addr_table[h];
while((a != NULL) && (connfd != a->connfd))
a = a->next;
if(a == NULL) return (addr_list)0;
else return a;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -