From b1bbc924a7d548f207e4b5f79f6bfea4ce33bf49 Mon Sep 17 00:00:00 2001 From: Pedro Gonnet <pedro.gonnet@durham.ac.uk> Date: Wed, 13 Nov 2013 21:25:07 +0000 Subject: [PATCH] check heap in both directions. --- src/queue.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/queue.c b/src/queue.c index 30d120d..1961b11 100644 --- a/src/queue.c +++ b/src/queue.c @@ -89,20 +89,33 @@ int queue_get ( struct queue *q , struct qsched *s , int insist ) { /* Fix the heap. */ w = tasks[ inds[k] ].weight; - while ( 1 ) { - if ( ( j = 2*k + 1 ) >= count ) - break; - if ( j+1 < count && tasks[ inds[j] ].weight < tasks[ inds[j+1] ].weight ) - j = j + 1; - if ( tasks[ inds[j] ].weight > w ) { - temp = inds[j]; - inds[j] = inds[k]; - inds[k] = temp; - k = j; + if ( k > 0 && w > tasks[ inds[k/2] ].weight ) + while ( k > 0 ) { + j = k / 2; + if ( w > tasks[ inds[j] ].weight ) { + temp = inds[j]; + inds[j] = inds[k]; + inds[k] = temp; + k = j; + } + else + break; + } + else + while ( 1 ) { + if ( ( j = 2*k + 1 ) >= count ) + break; + if ( j+1 < count && tasks[ inds[j] ].weight < tasks[ inds[j+1] ].weight ) + j = j + 1; + if ( tasks[ inds[j] ].weight > w ) { + temp = inds[j]; + inds[j] = inds[k]; + inds[k] = temp; + k = j; + } + else + break; } - else - break; - } } /* did we get a task? */ -- GitLab