您的位置:首页 > 其它

一个哈希表Hash Table的例子,用于IPv4协议的IP地址查找

2018-03-13 09:38 501 查看
头文件

typedef struct ip4_addr_key
{
unsigned int                ifindex;
unsigned int                addr;
unsigned short              vr;
}ip4AddrKey;

typedef unsigned int (*hash_obj)(const ip4AddrKey *obj);
typedef unsigned int (*hash_key)(const ip4AddrKey *key);
typedef unsigned int (*hash_cmp)(const ip4AddrKey* key1,const ip4AddrKey *key2);

typedef struct myHashTbl
{
hash_obj objFunc;
hash_key keyFunc;
hash_cmp cmpFunc;
unsigned int size;
unsigned int elem;
void **table;
}myHashTbl;

void *element_get(myHashTbl *head, const void *key);

myHashTbl *createHash(int size);
int addElem(ip4AddrKey *obj);
unsigned int mykey_update(void *key_param, int key_len, unsigned initval);

C文件
#include<stdio.h>
#include<stdlib.h>
#include "hash2.h"

unsigned int array[11];
unsigned int Key[11];

unsigned int Index[11];
myHashTbl tbl1;

myHashTbl *pHashTbl;

ip4AddrKey addrList[]=
{
{
.ifindex=0,
.addr=0xc0a80909,
.vr=0
},
{1,0xd0a80303, 0},
{0,0xc0a80102, 0},
{1,0xc0a90201, 0},
};

unsigned int myhash_key(ip4AddrKey *key)
{
return mykey_update(&key->addr, 4,key->ifindex + (unsigned int)(key->vr << 16));

}

unsigned int myhash_obj(ip4AddrKey *obj)
{
return myhash_key(obj);
}

unsigned int myhash_cmp(const ip4AddrKey* key1,const ip4AddrKey *key2)
{
if((key1->addr==key2->addr) && (key1->ifindex==key2->ifindex))
return 1;
else
return 0;
}

myHashTbl *createHash(int size)
{
myHashTbl *pTbl;
pTbl=(myHashTbl *)calloc(1,sizeof(myHashTbl *));

pTbl->cmpFunc=myhash_cmp;
pTbl->keyFunc=myhash_key;
pTbl->objFunc=myhash_obj;
pTbl->size=11;
pTbl->elem=0;
//pTbl->table=calloc(pTbl->size,sizeof(*pTbl->table));
pTbl->table=array;
return pTbl;
}

int addElem(ip4AddrKey *obj)
{
unsigned hash_index;
hash_index = pHashTbl->keyFunc(obj) % pHashTbl->size;
printf("hash_index=%d\n",hash_index);
while (pHashTbl->table[hash_index % head->size] != 0)
{
if (head->table[hash_index % head->size] == obj)
return DUPLICATE;
hash_index++;
}
pHashTbl->table[hash_index] = obj;
pHashTbl->elem++;
return 0;
}

unsigned int mykey_update(void *key_param, int key_len, unsigned initval)
{
unsigned a, b;
unsigned len;
const unsigned char *key = key_param;
static int keyNo=0;

/* Set up the internal state */
len = (unsigned) key_len;
a = initval;
b = len;

while (len >= 4)
{
a += (unsigned) (key[0] + (key[1] << 8) + (key[2] << 16) + (key[3] << 24));
b +=  (a << 3);
a += ~(b << 11);
b ^=  (a >> 16);
key += 4; len -= 4;
}

/* All the case statements fall through */
switch (len)
{
case 3: a += (unsigned)(key[2] << 16);
case 2: b ^= (unsigned)(key[1] << 8);
case 1: a += key[0];
default: break;
}

a ^=  (b >> 6);
b += ~(a << 15);
a ^=  (b >> 10);
Key[keyNo++]=a+b;
return a + b;
}

int main(void)
{
int *paddr;
int *addr1=&addrList[2];
pHashTbl=createHash(29);
addElem(&addrList[0]);
addElem(&addrList[1]);
addElem(&addrList[2]);
addElem(&addrList[3]);

paddr=element_get(pHashTbl,&addrList[2]);

return 0;
}

void *element_get(myHashTbl *head, const void *key)
{
unsigned int hash_index;
void    *obj;

hash_index = head->keyFunc(key) % head->size;
while ((obj = head->table[hash_index]) != NULL)
{
if (head->cmpFunc(obj, key))
return obj;
hash_index = (hash_index + 1) % head->size;
}
return NULL;
}


这个hash 表的array存储的都是指向ip4AddrKey的地址,也就是实现了

addr《-》address的映射。使用mingw-gcc+eclipse编译运行

In another strategy, called open addressing, all entry records are stored in the bucket array itself. When a ne



“`

`

w entry has to be inserted, the buckets are examined, starting with the hashed-to slot and proceeding in some probe sequence, until an unoccupied slot is found. When searching for an entry, the buckets are scanned in the same sequence, until either the target record is found, or an unused array slot is found, which indicates that there is no such key in the table.[16] The name “open addressing” refers to the fact that the location (“address”) of the item is not determined by its hash value. (This method is also called closed hashing; it should not be confused with “open hashing” or “closed addressing” that usually mean separate chaining.)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐