libevent中堆的实现(很标准的堆实现)

// 所有宏定义已经展开


typedef struct min_heap
{
	struct event** p;		// 预留的空间
	unsigned n, a;	// n : 元素个数 	a : 预留的空间的长度
} min_heap_t;


void min_heap_ctor_(min_heap_t* s) {		// 初始化堆
	s->p = 0;
	s->n = 0;
	s->a = 0;
}
void min_heap_dtor_(min_heap_t* s) {		// 堆的析构函数
	if (s->p) event_mm_free_(s->p);
}

void min_heap_elem_init_(struct event* e) {
	e->ev_timeout_pos.min_heap_idx = -1;
}
int min_heap_empty_(min_heap_t* s) {	// 判断堆是不是空的
	return 0u == s->n;
}
unsigned min_heap_size_(min_heap_t* s) {	// 返回堆中元素的个数
	return s->n;
}
struct event* min_heap_top_(min_heap_t* s) {	// 得到堆中存储的event
	return s->n ? *s->p : 0;
}


// 往堆中添加一个元素
int min_heap_push_(min_heap_t* s, struct event* e) {
	// 如果当前堆中的元素大于4294967295U, 获取为堆中预留空间失败,都返回-1
	if (s->n == (4294967295U) || min_heap_reserve_(s, s->n + 1))
		return -1;
	// s->n++. 说明元素数量加一
	// 默认是把新插入的元素放到p数组的最后一个元素,然后不断的上浮
	min_heap_shift_up_(s, s->n++, e);
	return 0;
}


// 取出堆中的第一个元素,也就是时间值最小的那个event,当前堆没有了头,就需要新找一个头
struct event* min_heap_pop_(min_heap_t* s) {
	if (s->n) {
		struct event* e = *s->p;
		// 取走头部元素之后,思路上是把尾部的元素放到头部,然后对这个元素下沉,但是这里没有真正的
		// 放在头部而是逻辑上的放
		min_heap_shift_down_(s, 0u, s->p[--s->n]);
		e->ev_timeout_pos.min_heap_idx = -1;
		return e;
	}
	return 0;
}
// 判断event是不是当前堆中最小的元素
int min_heap_elt_is_top_(const struct event *e) {
	return e->ev_timeout_pos.min_heap_idx == 0;
}

// 从堆中删除一个元素
int min_heap_erase_(min_heap_t* s, struct event* e) {
	if (-1 != e->ev_timeout_pos.min_heap_idx) {
		struct event *last = s->p[--s->n];
		unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;
		if (e->ev_timeout_pos.min_heap_idx > 0 && ((((&(s->p[parent])->ev_timeout)->tv_sec == (&(last)->ev_timeout)->tv_sec) ? ((&(s->p[parent])->ev_timeout)->tv_usec > (&(last)->ev_timeout)->tv_usec) : ((&(s->p[parent])->ev_timeout)->tv_sec > (&(last)->ev_timeout)->tv_sec))))
			min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, last);
		else
			min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, last);
		e->ev_timeout_pos.min_heap_idx = -1;
		return 0;
	}
	return -1;
}
int min_heap_adjust_(min_heap_t *s, struct event *e) {
	if (-1 == e->ev_timeout_pos.min_heap_idx) {
		return min_heap_push_(s, e);
	} else {
		// 找到父节点
		unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;

		if (e->ev_timeout_pos.min_heap_idx > 0 && ((((&(s->p[parent])->ev_timeout)->tv_sec == (&(e)->ev_timeout)->tv_sec) ? ((&(s->p[parent])->ev_timeout)->tv_usec > (&(e)->ev_timeout)->tv_usec) : ((&(s->p[parent])->ev_timeout)->tv_sec > (&(e)->ev_timeout)->tv_sec))))
			min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, e);
		else
			min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, e);
		return 0;
	}
}

// 给堆预留空间,最小是n
int min_heap_reserve_(min_heap_t* s, unsigned n) {
	if (s->a < n) {
		struct event** p;
		unsigned a = s->a ? s->a * 2 : 8;	// 如果原来有空间就扩展2倍
		if (a < n)	// 扩展后如果还小于n,就变成n
			a = n;
		if (!(p = (struct event**)event_mm_realloc_((s->p), (a * sizeof *p))))
			return -1;
		s->p = p;
		s->a = a;
	}
	return 0;
}

// 从hole_index处上移event e
void min_heap_shift_up_unconditional_(
	min_heap_t* s, unsigned hole_index, struct event* e) {
	unsigned parent = (hole_index - 1) / 2;
	do {
		(s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
		hole_index = parent;
		parent = (hole_index - 1) / 2;
	} while (hole_index && ((((&(s->p[parent])->ev_timeout)->tv_sec == (&(e)->ev_timeout)->tv_sec) ? ((&(s->p[parent])->ev_timeout)->tv_usec > (&(e)->ev_timeout)->tv_usec) : ((&(s->p[parent])->ev_timeout)->tv_sec > (&(e)->ev_timeout)->tv_sec))));
	// 这里说明hole_index的父节点的值已经小于了hole_index,就没必要继续上浮了
	(s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}

// 堆中的上浮操作
void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e) {
	unsigned parent = (hole_index - 1) / 2;		// 找到父节点
	while (hole_index && ((((&(s->p[parent])->ev_timeout)->tv_sec == (&(e)->ev_timeout)->tv_sec) ? ((&(s->p[parent])->ev_timeout)->tv_usec > (&(e)->ev_timeout)->tv_usec) : ((&(s->p[parent])->ev_timeout)->tv_sec > (&(e)->ev_timeout)->tv_sec)))) {
		// 如果要插入的节点比父节点小那就上浮
		(s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
		hole_index = parent;
		parent = (hole_index - 1) / 2;
	}
	// 找到最终的位置,hole_index是p数组的下标
	(s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}
// 堆的下沉操作
void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e) {
	unsigned min_child = 2 * (hole_index + 1);
	while (min_child <= s->n) {
		min_child -= min_child == s->n || ((((&(s->p[min_child])->ev_timeout)->tv_sec == (&(s->p[min_child - 1])->ev_timeout)->tv_sec) ? ((&(s->p[min_child])->ev_timeout)->tv_usec > (&(s->p[min_child - 1])->ev_timeout)->tv_usec) : ((&(s->p[min_child])->ev_timeout)->tv_sec > (&(s->p[min_child - 1])->ev_timeout)->tv_sec)));
		if (!(((((&(e)->ev_timeout)->tv_sec == (&(s->p[min_child])->ev_timeout)->tv_sec) ? ((&(e)->ev_timeout)->tv_usec > (&(s->p[min_child])->ev_timeout)->tv_usec) : ((&(e)->ev_timeout)->tv_sec > (&(s->p[min_child])->ev_timeout)->tv_sec)))))
			break;
		(s->p[hole_index] = s->p[min_child])->ev_timeout_pos.min_heap_idx = hole_index;
		hole_index = min_child;
		min_child = 2 * (hole_index + 1);
	}
	(s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值