libevent2源码 min_heap
来源:互联网 发布:怎么添加打印机端口 编辑:程序博客网 时间:2024/06/06 02:51
位于 minheap-internal.h , 是一个堆的实现
主要代码为:
堆的定义:
typedef struct min_heap{//堆各节点空间struct event** p;//n为节点数量,a为容量unsigned n, a;} min_heap_t;
插入新节点
int min_heap_push(min_heap_t* s, struct event* e){//申请空间if (min_heap_reserve(s, s->n + 1))return -1;//移动到正确位置min_heap_shift_up_(s, s->n++, e);return 0;}
void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e){unsigned parent = (hole_index - 1) / 2;//父节点大于hole_index处节点while (hole_index && min_heap_elem_greater(s->p[parent], e)){//父节点移到hole_index处(s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;//继续向上级节点寻找hole_index = parent;parent = (hole_index - 1) / 2;}//放入e(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 || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);//父节点已经最小则无需继续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 = 2 * (hole_index + 1);}(s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;}
0 0
- libevent2源码 min_heap
- libevent2.0源码学习一:前言
- libevent2.0源码学习二:通用数据结构
- libevent2.0源码学习一:前言
- libevent2.0源码学习二:通用数据结构
- 学习笔记--libevent min_heap
- libevent的min_heap剖析
- libevent2.0源码学习三:对网络模型的(EPOLL)的封装
- libevent2.0源码学习三:对网络模型的(EPOLL)的封装
- libevent2.0 使用
- libevent2.0.1.7
- ubuntu安装libevent2.0.20
- ubuntu安装libevent2.0.21
- Ubuntu安装libevent2.0.20
- muduo 与 libevent2 吞吐量对比
- libevent2 简单服务器客户端实现
- 堆的实现,提供 min_heap and max_heap功能
- 通向码农的道路(开源 libevent min_heap 实现)
- GitHub 优秀的 Android 开源项目
- HDU 5011 Game 【Nim博弈】
- codeforces 476c Dreamoon and Sums
- opengl教程
- CodeForces 486C Palindrome Transformation
- libevent2源码 min_heap
- Uva 644-Immediate Decodability(串)
- 备份基本原理
- C++ STL删除元素的一些案例
- 【树套树】【bzoj 3236】: [Ahoi2013]作业
- Send a Table
- Android: 读取短信
- Anti-alias的前世今生
- 【计算几何】 POJ 1556 The Doors