Optimize pq
This commit is contained in:
parent
6a511aef0a
commit
71623b674e
|
@ -52,14 +52,14 @@ static void swap(nghttp2_pq *pq, size_t i, size_t j) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bubble_up(nghttp2_pq *pq, size_t index) {
|
static void bubble_up(nghttp2_pq *pq, size_t index) {
|
||||||
if (index == 0) {
|
size_t parent;
|
||||||
|
while (index != 0) {
|
||||||
|
parent = (index - 1) / 2;
|
||||||
|
if (!pq->less(pq->q[index], pq->q[parent])) {
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
size_t parent = (index - 1) / 2;
|
|
||||||
if (pq->less(pq->q[index], pq->q[parent])) {
|
|
||||||
swap(pq, parent, index);
|
|
||||||
bubble_up(pq, parent);
|
|
||||||
}
|
}
|
||||||
|
swap(pq, parent, index);
|
||||||
|
index = parent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,8 +94,10 @@ nghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bubble_down(nghttp2_pq *pq, size_t index) {
|
static void bubble_down(nghttp2_pq *pq, size_t index) {
|
||||||
size_t i, j = index * 2 + 1;
|
size_t i, j, minindex;
|
||||||
size_t minindex = index;
|
for (;;) {
|
||||||
|
j = index * 2 + 1;
|
||||||
|
minindex = index;
|
||||||
for (i = 0; i < 2; ++i, ++j) {
|
for (i = 0; i < 2; ++i, ++j) {
|
||||||
if (j >= pq->length) {
|
if (j >= pq->length) {
|
||||||
break;
|
break;
|
||||||
|
@ -104,9 +106,11 @@ static void bubble_down(nghttp2_pq *pq, size_t index) {
|
||||||
minindex = j;
|
minindex = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (minindex != index) {
|
if (minindex == index) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
swap(pq, index, minindex);
|
swap(pq, index, minindex);
|
||||||
bubble_down(pq, minindex);
|
index = minindex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,6 +126,11 @@ void nghttp2_pq_pop(nghttp2_pq *pq) {
|
||||||
void nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item) {
|
void nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item) {
|
||||||
assert(pq->q[item->index] == item);
|
assert(pq->q[item->index] == item);
|
||||||
|
|
||||||
|
if (item->index == 0) {
|
||||||
|
nghttp2_pq_pop(pq);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (item->index == pq->length - 1) {
|
if (item->index == pq->length - 1) {
|
||||||
--pq->length;
|
--pq->length;
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue