Libevent源码分析(一):最小堆
Libevent中的timeout事件是使用最小堆来管理维护的.代码位于<minheap-internal.h>.
看函数命名和代码风格应该是一个C++程序员,函数名都挺好懂的,只是下面这个结构体变量命名比较坑....
typedef struct min_heap
{
struct event** p;
unsigned n, a;//n队列元素的多少,a代表队列空间的大小.
} min_heap_t;
注释是我加的,这命名,n啊a啊的,鬼知道啥意思....必须吐槽一下.
先来说说什么是最小堆:
1.堆是一个二叉树
2.最小堆:父节点的值总是小于等于子节点
如下图:
上图圆圈旁边的数字代表其在数组中的下标.堆一般是用数组来存储的,也就是说实际存储结构是连续的,只是逻辑上是一棵树的结构.这样做的好处是很容易找到堆顶的元素,对Libevent来说,很容易就可以找到距当前时间最近的timeout事件.
现在想想看我们要插入一个元素,我们要怎么移动数组中元素的位置,使其逻辑上仍然是一个小堆?结合下图很容易看出来:
1.假设我们要插入的元素为6大于其父节点的值2.则把元素放在数组相应的index上,插入完成.
2.假设我们要插入的为2小于其父节点的值3.则交换该节点与其父节点的值.对于下图来说,交换完毕后插入就算完成了.那要是交换完后发现index=2的元素还小于其父节点index=0的呢?就又得在一次交换,如此循环,直到达到根节点或是其不大于父节点.
到了这里我们看看libevent里的实现代码就很清楚了
int min_heap_push(min_heap_t* s, struct event* e)
{
if (min_heap_reserve(s, s->n + ))
return -;
min_heap_shift_up_(s, s->n++, e);
return ;
} //分配队列大小.n代表队列元素个数多少.
int min_heap_reserve(min_heap_t* s, unsigned n)
{
if (s->a < n) //队列大小不足元素个数,重新分配空间.
{
struct event** p;
unsigned a = s->a ? s->a * : ; //初始分配8个指针大小空间,否则原空间大小翻倍.
if (a < n)
a = n; //翻倍后空间依旧不足,则分配n.
if (!(p = (struct event**)mm_realloc(s->p, a * sizeof *p))) //重新分配内存
return -;
s->p = p; //重新赋值队列地址及大小.
s->a = a; //
}
return ;
} void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
{
unsigned parent = (hole_index - ) / ;
while (hole_index && min_heap_elem_greater(s->p[parent], e)) //比父节点小或是到达根节点.则交换位置.循环.
{
(s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
hole_index = parent;
parent = (hole_index - ) / ;
}
(s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}
实际上作者写了一个比较通用的函数min_heap_shift_up(),与之相对应的还有min_heap_shift_down()
void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
{
unsigned min_child = * (hole_index + );
while (min_child <= s->n)
{
//找出较小子节点
min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - ]);
//比子节点小正常.不需要再交换位置,跳出循环.
if (!(min_heap_elem_greater(e, s->p[min_child])))
break;
//比子节点大,要交换位置
(s->p[hole_index] = s->p[min_child])->ev_timeout_pos.min_heap_idx = hole_index;
hole_index = min_child;
min_child = * (hole_index + );
}
(s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}
这里的hole_index是我们要填入某个值的下标,e是要填入的值.还是画图比较好理解:
当这个值(下标为hole_Index=1)比其父节点1(index = 0)小时,要向上移动调整.
当这个值(下标为hole_Index=1)比其最小子节点6(index = 3)还大时,要向下移动调整.
Libevent里是这么使用他们的:
int min_heap_erase(min_heap_t* s, struct event* e)
{
if (- != e->ev_timeout_pos.min_heap_idx)
{
struct event *last = s->p[--s->n];//把最后一个值作为要填入hole_index的值
unsigned parent = (e->ev_timeout_pos.min_heap_idx - ) / ;
/* we replace e with the last element in the heap. We might need to
shift it upward if it is less than its parent, or downward if it is
greater than one or both its children. Since the children are known
to be less than the parent, it can't need to shift both up and
down. */
if (e->ev_timeout_pos.min_heap_idx > && min_heap_elem_greater(s->p[parent], last))
min_heap_shift_up_(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 = -;
return ;
}
return -;
}
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 = -;
return e;
}
return ;
}
最后总结一下,由于堆这种结构在逻辑上的这种二叉树的关系,其插入也好,删除也好,就是一个与父节点或是子节点比较然后调整位置,这一过程循环往复直到达到边界条件的过程.记住这一点,就不难写出代码了.
二叉树节点i:父节点为(i-1)/2.子节点为2i+1,2(i+1)。
Libevent源码分析(一):最小堆的更多相关文章
- 【转】libevent源码分析
libevent源码分析 转自:http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html 这两天没事,看了一下Memcached和l ...
- Libevent源码分析 (1) hello-world
Libevent源码分析 (1) hello-world ⑨月份接触了久闻大名的libevent,当时想读读源码,可是由于事情比较多一直没有时间,现在手头的东西基本告一段落了,我准备读读libeven ...
- libevent源码分析
这两天没事,看了一下Memcached和libevent的源码,做个小总结. 1.入门 1.1.概述Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络 ...
- libevent源码分析二--timeout事件响应
libevent不仅支持io事件,同时还支持timeout事件与signal事件,这篇文件将分析libevent是如何组织timeout事件以及如何响应timeout事件. 1. min_heap ...
- Libevent源码分析系列【转】
转自:https://www.cnblogs.com/zxiner/p/6919021.html 1.使用libevent库 源码那么多,该怎么分析从哪分析呢?一个好的方法就是先用起来,会用了 ...
- Libevent源码分析系列
1.使用libevent库 源码那么多,该怎么分析从哪分析呢?一个好的方法就是先用起来,会用了,然后去看底层相应的源码,这样比较有条理,自上向下掌握.下面用libevent库写个程序,每隔1秒 ...
- Libevent源码分析—event_base_dispatch()
我们知道libevent是一个Reactor模式的事件驱动的网络库. 到目前为止,我们已经看了核心的event和event_base结构体的源码,看了初始化这两个结构体的源码,看了注册event的 ...
- Libevent源码分析—event_init()
下面开始看初始化event_base结构的相关函数.相关源码位于event.c event_init() 首先调用event_init()初始化event_base结构体 struct event_b ...
- Libevent源码分析—event, event_base
event和event_base是libevent的两个核心结构体,分别是反应堆模式中的Event和Reactor.源码分别位于event.h和event-internal.h中 1.event: s ...
随机推荐
- JavaScript Web Application summary
Widget/ HTML DOM (CORE) (local dom) DOM, BOM, Event(Framework, UI, Widget) function(closure) DATA (c ...
- java签名证书
import java.io.FileInputStream; import java.security.KeyStore; import java.security.PrivateKey; impo ...
- perl 正则前导字符
uat-prx02:/root# cat a1.pl my $str="123"; if ($str =~/(abc)*/){print "111111111\n&quo ...
- 【二分】XMU 1587 中位数
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1587 题目大意: 求两个长度为n(n<=109)的有序序列合并后的中位数.序列中 ...
- HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash
Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...
- 1000 A+B [ACM刷题]
这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程. github传送门:https://github.com/haoyuanliu/Online_Judge/tree/mas ...
- lesson9:分布式定时任务
在实际的开发过程中,我们一定会遇到服务自有的定时任务,又因为现在的服务都是分布式的,但是对于定时任务,很多的业务场景下,还是只希望单台服务来执行,网上有很多分布式定时任务的框架,各位如感兴趣,可以自行 ...
- JavaScript新手学习笔记1——数组
今天,我复习了一下JavaScript的数组相关的知识,总结一下数组的API: 总共有11个API:按照学习的先后顺序来吧,分别是: ① toString() 语法:arr.toString(); ...
- Genymotion模拟器一滑动页面就跳到搜索003
今天郁闷的要死,好不容易让Appium关联起Genymotion了,但是一滑动屏幕就跳转到搜索003界面,当时还以为是Appium的Bug或者Genymotion本身出问题了. 结果网上搜了一段时间( ...
- iOS UITextField 设置内边距
[self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingTop"]; [self.your ...