hash链表

hash链表概述

hash链表是hash表和链表的结合,使用比较方便。

hash链表实现

本文的hash链表实现:hash头部用单链表、其他的hash节点用双向链表。实现主要取自Linux内核实现,本文做了移植。本文代码可从http://download.csdn.net/detail/it_pcode/6632905下载。

hash实现

#ifndef HLIST_H_
#define HLIST_H_

#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

/*通过父结构体type中的成员member的已知地址ptr,来寻找当前ptr地址所属的父结构体type的地址*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

/*内核预加载内容到RAM,在此不做实现*/
#define prefetch(x) (x)

/*
 * Double linked lists with a single pointer list head.
 * Mostly useful for hash tables where the two pointer list head is
 * too wasteful.
 * You lose the ability to access the tail in O(1).
 */

struct hlist_head {
	struct hlist_node *first;
};

struct hlist_node {
	struct hlist_node *next, **pprev;
};

#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
static inline void INIT_HLIST_NODE(struct hlist_node *h) {
	h->next = NULL;
	h->pprev = NULL;
}

static inline int hlist_unhashed(const struct hlist_node *h) {
	return !h->pprev;
}

static inline int hlist_empty(const struct hlist_head *h) {
	return !h->first;
}

static inline void __hlist_del(struct hlist_node *n) {
	struct hlist_node *next = n->next;
	struct hlist_node **pprev = n->pprev;
	*pprev = next;
	if (next)
		next->pprev = pprev;
}

static inline void hlist_del(struct hlist_node *n) {
	__hlist_del(n);
	n->next = NULL;
	n->pprev = NULL;
}

static inline void hlist_del_init(struct hlist_node *n) {
	if (!hlist_unhashed(n)) {
		__hlist_del(n);
		INIT_HLIST_NODE(n);
	}
}

static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) {
	struct hlist_node *first = h->first;
	n->next = first;
	if (first)
		first->pprev = &n->next;
	h->first = n;
	n->pprev = &h->first;
}

/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node *n,
		struct hlist_node *next) {
	n->pprev = next->pprev;
	n->next = next;
	next->pprev = &n->next;
	*(n->pprev) = n;
}

static inline void hlist_add_after(struct hlist_node *n,
		struct hlist_node *next) {
	next->next = n->next;
	n->next = next;
	next->pprev = &n->next;

	if (next->next)
		next->next->pprev = &next->next;
}

/*
 * Move a list from one list head to another. Fixup the pprev
 * reference of the first entry if it exists.
 */
static inline void hlist_move_list(struct hlist_head *old,
		struct hlist_head *new) {
	new->first = old->first;
	if (new->first)
		new->first->pprev = &new->first;
	old->first = NULL;
}

#define hlist_entry(ptr, type, member) container_of(ptr,type,member)

#define hlist_for_each(pos, head) \
	for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
	     pos = pos->next)

#define hlist_for_each_safe(pos, n, head) \
	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
	     pos = n)

/**
 * hlist_for_each_entry	- iterate over list of given type
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry(tpos, pos, head, member)			 \
	for (pos = (head)->first;					 \
	     pos && ({ prefetch(pos->next); 1;}) &&			 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

/**
 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_continue(tpos, pos, member)		 \
	for (pos = (pos)->next;						 \
	     pos && ({ prefetch(pos->next); 1;}) &&			 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

/**
 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_from(tpos, pos, member)			 \
	for (; pos && ({ prefetch(pos->next); 1;}) &&			 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

/**
 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @n:		another &struct hlist_node to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_safe(tpos, pos, n, head, member) 		 \
	for (pos = (head)->first;					 \
	     pos && ({ n = pos->next; 1; }) && 				 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = n)

#endif /* HLIST_H_ */

hash链表实例

首先实现了自己的hash list初始化、键存在与否判定以及key值查找等函数。然后是主测试程序。

#define MAX_LEN 20

struct hash_node {
	struct hlist_node hnode;
	int age;
};

struct hlist_head hashead[MAX_LEN];

void init_hlist() {
	int i = 0;
	for (i = 0; i < MAX_LEN; i++) {
		INIT_HLIST_HEAD(&hashead[i]);
	}
}

int key_exists(struct hlist_head *head, int key) {
	struct hash_node * node;
	struct hlist_node *hlistnode;
	hlist_for_each_entry(node, hlistnode, head,hnode) {
		if (node->age == key) {
			return 1;
		}
	}
	return 0;
}

struct hash_node * hlist_search(int age) {
	struct hash_node *node, *data;
	int i = 0;
	struct hlist_node *hlistnode;
	for (i = 0; i < MAX_LEN; i++) {
		hlist_for_each_entry(node, hlistnode, &hashead[i],hnode) {
			data = container_of(&node->hnode, struct hash_node, hnode);
			if (data->age == age) {
				return data;
			}
		}
	}
	return NULL;
}

void testhlist() {

	init_hlist();
	int i = 0;
	struct hash_node * node;
	struct hlist_node *hlistnode;

	srand(time(NULL));
	for (i = 0; i < 4 * MAX_LEN; i++) {
		node = malloc(sizeof(struct hash_node));
		INIT_HLIST_NODE(&node->hnode);
		node->age = rand() % (4 * MAX_LEN);
		if (key_exists(&hashead[node->age % MAX_LEN], node->age) == 0) {
			hlist_add_head(&node->hnode, &hashead[node->age % MAX_LEN]);
		}
	}
	for (i = 0; i < MAX_LEN; i++) {
		printf("head %d has member :", i);
		hlist_for_each_entry(node, hlistnode, &hashead[i],hnode) {
			printf("%d  ", node->age);
		}
		printf("\n");
	}

	for (i = 0; i < MAX_LEN; i++) {
		node = hlist_search(i);
		if (NULL != node) {
			printf("found %d\n", i);
			hlist_del(&node->hnode);
		}
	}
	printf("after clear \n");
	for (i = 0; i < MAX_LEN; i++) {
		printf("head %d has member :", i);
		hlist_for_each_entry(node, hlistnode, &hashead[i],hnode) {
			printf("%d  ", node->age);
		}
		printf("\n");
	}

}

在main函数里,调用主测试程序即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值