From fc24ffbfb3384286efa20728fe054c884d487fb0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 9 Jul 2023 11:05:13 -0600 Subject: [PATCH] [priority-queue] Minor simplify Tail-recursion definitely was being optimized by compiler, but I prefer writing it this way. --- src/hb-priority-queue.hh | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/hb-priority-queue.hh b/src/hb-priority-queue.hh index bf1b282d3..133c4c57e 100644 --- a/src/hb-priority-queue.hh +++ b/src/hb-priority-queue.hh @@ -108,6 +108,7 @@ struct hb_priority_queue_t void bubble_down (unsigned index) { + repeat: assert (index < heap.length); unsigned left = left_child (index); @@ -123,19 +124,20 @@ struct hb_priority_queue_t && (!has_right || heap.arrayZ[index].first <= heap.arrayZ[right].first)) return; + unsigned child; if (!has_right || heap.arrayZ[left].first < heap.arrayZ[right].first) - { - swap (index, left); - bubble_down (left); - return; - } + child = left; + else + child = right; - swap (index, right); - bubble_down (right); + swap (index, child); + index = child; + goto repeat; } void bubble_up (unsigned index) { + repeat: assert (index < heap.length); if (index == 0) return; @@ -145,7 +147,8 @@ struct hb_priority_queue_t return; swap (index, parent_index); - bubble_up (parent_index); + index = parent_index; + goto repeat; } void swap (unsigned a, unsigned b)