// 所有宏定義已經展開
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;
}