From 12900371925594a27b91457d6db67020eb4df4e5 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 26 Apr 2015 23:36:07 +0200 Subject: [PATCH] new function: remove a given element of the xbt_heap --- include/xbt/heap.h | 1 + src/xbt/heap.c | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/xbt/heap.h b/include/xbt/heap.h index e0ddfb8dd3..dda24d486c 100644 --- a/include/xbt/heap.h +++ b/include/xbt/heap.h @@ -27,6 +27,7 @@ XBT_PUBLIC(int) xbt_heap_size(xbt_heap_t H); XBT_PUBLIC(void) xbt_heap_push(xbt_heap_t H, void *content, double key); XBT_PUBLIC(void *) xbt_heap_pop(xbt_heap_t H); +XBT_PUBLIC(void) xbt_heap_rm_elm(xbt_heap_t H, void *content, double key); XBT_PUBLIC(double) xbt_heap_maxkey(xbt_heap_t H); XBT_PUBLIC(void *) xbt_heap_maxcontent(xbt_heap_t H); diff --git a/src/xbt/heap.c b/src/xbt/heap.c index 34b12cc593..f2f75bab0f 100644 --- a/src/xbt/heap.c +++ b/src/xbt/heap.c @@ -108,7 +108,6 @@ void xbt_heap_push(xbt_heap_t H, void *content, double key) return; } - /** * @brief Extracts from the heap and returns the element with the smallest key. * \param H the heap we're working on @@ -169,6 +168,19 @@ void *xbt_heap_remove(xbt_heap_t H, int i) return xbt_heap_pop(H); } +/** @brief Remove an arbitrary element from the heap + * @param H the heap we're working on + * @param content the object you want to add to the heap + * @param key the key associated to this object + */ +void xbt_heap_rm_elm(xbt_heap_t H, void *content, double key) { + int i=0; + while (i < H->count && (KEY(H, i) != key || CONTENT(H, i) != content)) + i++; + if (i == H->count) + return; + xbt_heap_remove(H,i); +} /** * @brief Updates an element of the heap with a new value. -- 2.20.1