一个哈希表Hash Table的例子,用于IPv4协议的IP地址查找
2018-03-13 09:38
501 查看
头文件
这个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.)
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.)
相关文章推荐
- 《大话数据结构》 查找 以及一个简单的哈希表例子
- 3010.脚本作业―l201.8.0编写一个脚本用于检测IP地址(递进版8)
- Hash Table 哈希表 C++ 例子
- 全国计算机等级二级C++第四章习题编程题6 :编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL.
- php中用于检测一个地理IP地址是否可用的代码
- 3011.脚本作业―l201.9.0编写一个脚本用于检测IP地址(递进版9)
- 一个比较笨的全文搜索的例子(分析结构用)-模糊查找
- Hash Table 哈希表 C++ 例子
- 3012.脚本作业―l201.10.0编写一个脚本用于检测IP地址(递进版10)
- 一个非常重要的查找算法--哈希表查找(SearchHash)
- 编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL.
- 一个用于网络传输,转换数据类型到byte的例子
- Dom4j的一个小例子,用于解析xml文件的元素获取方式(转)
- C++ 哈希表 (hashtable) 用于保存简单的数据,及数据查找,数据删除
- hash table相关--引入哈希桶的概念来实现一个哈希表
- 编写一个用于在字符串中查找某字符的函数。
- 二分法(折半法)查找数据的一个简单例子
- 发布一个用于和GridView或DataGrid表格相关联的查找搜索服务器控件
- Lea指令计算地址(用于四则混合运算),附上一个函数调用例子及其反汇编代码,很清楚
- C++字符串处理的一个例子:1.查找一个字符在字符串中第n次出现的位置。2分割字符串