libevent中min_heap分析
typedef struct min_heap
{
struct event** p;
unsigned n, a;
} min_heap_t; static inline void min_heap_ctor_(min_heap_t* s);
static inline void min_heap_dtor_(min_heap_t* s);
static inline void min_heap_elem_init_(struct event* e);
static inline int min_heap_elt_is_top_(const struct event *e);
static inline int min_heap_empty_(min_heap_t* s);
static inline unsigned min_heap_size_(min_heap_t* s);
static inline struct event* min_heap_top_(min_heap_t* s);
static inline int min_heap_reserve_(min_heap_t* s, unsigned n);
static inline int min_heap_push_(min_heap_t* s, struct event* e);
static inline struct event* min_heap_pop_(min_heap_t* s);
static inline int min_heap_adjust_(min_heap_t *s, struct event* e);
static inline int min_heap_erase_(min_heap_t* s, struct event* e);
static inline void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e);
static inline void min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e);
static inline void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e); #define min_heap_elem_greater(a, b) \
(evutil_timercmp(&(a)->ev_timeout, &(b)->ev_timeout, >)) void min_heap_ctor_(min_heap_t* s) { s->p = ; s->n = ; s->a = ; }
void min_heap_dtor_(min_heap_t* s) { if (s->p) mm_free(s->p); }
void min_heap_elem_init_(struct event* e) { e->ev_timeout_pos.min_heap_idx = -; }
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) { return s->n ? *s->p : ; } 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 ;
} 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 ;
} int min_heap_elt_is_top_(const struct event *e)
{
return e->ev_timeout_pos.min_heap_idx == ;
} 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];
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_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 = -;
return ;
}
return -;
} int min_heap_adjust_(min_heap_t *s, struct event *e)
{
if (- == e->ev_timeout_pos.min_heap_idx) {
return min_heap_push_(s, e);
} else {
unsigned parent = (e->ev_timeout_pos.min_heap_idx - ) / ;
/* The position of e has changed; we shift it up or down
* as needed. We can't need to do both. */
if (e->ev_timeout_pos.min_heap_idx > && min_heap_elem_greater(s->p[parent], e))
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 ;
}
return -;
} int min_heap_reserve_(min_heap_t* s, unsigned n)
{
if (s->a < n)
{
struct event** p;
unsigned a = s->a ? s->a * : ;
if (a < n)
a = 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_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e)
{
unsigned parent = (hole_index - ) / ;
do
{
//如果父节点>value[hole_index],父节点下沉
(s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
//hole_index替换为parent
hole_index = parent;
parent = (hole_index - ) / ;
} while (hole_index && min_heap_elem_greater(s->p[parent], e));
(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 - ) / ;
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;
} 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;
}
暂时记录下,明天继续看,figthing!
实现的很清晰,不得不佩服作者清晰的思维呀。菜鸟学习ing!
libevent中min_heap分析的更多相关文章
- libevent中数据缓冲区buffer分析
很多时候为了应对数据IO的"慢"或者其他原因都需要使用数据缓冲区.对于数据缓冲,我们不陌生,但是对于如何实现这个缓冲区,相信很多时候大家都没有考虑过.今天就通过分析libevent ...
- 【转】libevent源码分析
libevent源码分析 转自:http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html 这两天没事,看了一下Memcached和l ...
- libevent 网络IO分析
libevent 网络IO分析 Table of Contents 1. 简介 2. 简单使用与入门 2.1. 定时器-timeout 超时回调 2.2. 信号事件 2.3. 读取 socket 3. ...
- libevent源码分析
这两天没事,看了一下Memcached和libevent的源码,做个小总结. 1.入门 1.1.概述Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络 ...
- libevent源码分析二--timeout事件响应
libevent不仅支持io事件,同时还支持timeout事件与signal事件,这篇文件将分析libevent是如何组织timeout事件以及如何响应timeout事件. 1. min_heap ...
- libevent 源码分析
1,前言 Libevent是一个轻量级的开源高性能网络库,使用者众多,研究者更甚,相关文章也不少.写这一系列文章的用意在于,一则分享心得:二则对libevent代码和设计思想做系统的.更深层次的分析, ...
- libevent源码分析:bufferevent
struct bufferevent定义在文件bufferevent_struct.h中. /** Shared implementation of a bufferevent. This type ...
- Libevent源码分析(一):最小堆
Libevent中的timeout事件是使用最小堆来管理维护的.代码位于<minheap-internal.h>. 看函数命名和代码风格应该是一个C++程序员,函数名都挺好懂的,只是下面这 ...
- Libevent源码分析 (1) hello-world
Libevent源码分析 (1) hello-world ⑨月份接触了久闻大名的libevent,当时想读读源码,可是由于事情比较多一直没有时间,现在手头的东西基本告一段落了,我准备读读libeven ...
随机推荐
- Logistic Regression--逻辑回归算法汇总**
http://www.cnblogs.com/biyeymyhjob/archive/2012/07/18/2595410.html 转自别处 有很多与此类似的文章 也不知道谁是原创 因原文由少于错 ...
- VS web.config/app.conifg配置文件自定义类型使用智能感知功能
大家使用VS编辑web.config或app.config时可以使用智能感知功能的,像下面这样很是方便 当然如果是我们自定义的类型也是可以使用智能感知的,因为智能感知的内容是来自你或其他公司(MS)提 ...
- 整数对A满足二叉查找树,B满足最大堆
1 题目 给出一组整数对 { (a[0], b[0]), (a[1], b[1]) ... (a[n-1], b[n-1]) },全部 a 值和 b 值分别不反复(随意 i != j 满足 a[i] ...
- DISQLite3在XE4中的安装
时隔这么久,因为工作中需要将一些图片序列文件进行分析,然后将结果进行分组统计,而分组统计用SQL语法很容易实现,但是要求程序运行的环境中安装有庞大的数据库系统,经过网上的寻找,终于发现了SQLite. ...
- 基于VM10+Win7安装Mac OSX10.11 El Capitan
前言 此文写给那些像我一样的屌丝程序员(呵呵,我现在从事的是最底层的工作了,但是不想放弃我的梦想) 说明 基于VM10+Win7安装Mac OSX10.11 El Capitan 工具 VMware- ...
- insta经典滤镜下载
好不容易找到的Insta的经典滤镜源码,贴出来帮大家学习. // // IFImageFilter.m // InstaFilters // // Created by Di Wu on 2/28/1 ...
- vi入门到精通
VI是在Linux命令行下常用的文本编辑工具,在服务配置管理过程中经常用到:vi的常见的使用指南,互联网上随处可见,但仅能满足初学者对文档编辑的需求.这里就我自己在使用过程中通常用到的一些技巧操作方法 ...
- PHPUnit单元测试
单元测试 PHPUnit <?php /** * 定义一个用来被测试的类RemoteConnect * @author json * */ class RemoteConnect{ public ...
- Spring开发 - 通过实现ApplicationContextAware在Servlet中调用注解的Service
用过Spring MVC的人都知道,我们如何在Controller中注入Service,可以使用@Resource注解的方法. 有时候,实际在项目的过程中,我们需要在某个Servlet中使用Servi ...
- How to check WWN and Multipathing on Windows Server
There are many ways to find the World Wide Name (WWN) of fibre channel HBA connected to windows serv ...