From 6538f7ff7c62de807adca85e5db49d7cccab67d4 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 8 Apr 2016 22:04:14 +0100 Subject: [PATCH] [Feature] Allow removal from the heap --- src/libutil/heap.c | 21 ++++++++++++++++++++- src/libutil/heap.h | 9 +++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/libutil/heap.c b/src/libutil/heap.c index ba1b44793..9bcfd4f1d 100644 --- a/src/libutil/heap.c +++ b/src/libutil/heap.c @@ -138,7 +138,6 @@ rspamd_min_heap_update_elt (struct rspamd_min_heap *heap, g_assert (heap != NULL); g_assert (elt->idx > 0 && elt->idx <= heap->ar->len); - oldpri = elt->pri; elt->pri = npri; @@ -152,6 +151,26 @@ rspamd_min_heap_update_elt (struct rspamd_min_heap *heap, } } +void +rspamd_min_heap_remove_elt (struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt) +{ + struct rspamd_min_heap_elt *first; + + g_assert (heap != NULL); + g_assert (elt->idx > 0 && elt->idx <= heap->ar->len); + + first = g_ptr_array_index (heap->ar, 0); + + if (elt != first) { + elt->pri = first->pri - 1; + rspamd_min_heap_swim (heap, elt); + } + + /* Now the desired element is on the top of queue */ + (void)rspamd_min_heap_pop (heap); +} + void rspamd_min_heap_destroy (struct rspamd_min_heap *heap) { diff --git a/src/libutil/heap.h b/src/libutil/heap.h index 58a9a0c88..20e9209ce 100644 --- a/src/libutil/heap.h +++ b/src/libutil/heap.h @@ -62,6 +62,15 @@ struct rspamd_min_heap_elt* rspamd_min_heap_pop (struct rspamd_min_heap *heap); void rspamd_min_heap_update_elt (struct rspamd_min_heap *heap, struct rspamd_min_heap_elt *elt, guint npri); + +/** + * Removes element from the heap + * @param heap + * @param elt + */ +void rspamd_min_heap_remove_elt (struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt); + /** * Destroys heap (elements are not destroyed themselves) * @param heap -- 2.39.5