Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2017-10-29 07:25:13 +0300
committerCampbell Barton <ideasman42@gmail.com>2017-10-29 07:47:06 +0300
commit34257329263c3af108736b8d1047d48091e82d92 (patch)
tree29307361dce6c7bfced0b7b0887278df3c00b568 /source/blender/blenlib/intern/BLI_heap.c
parent336885bebaa8c7b60041b139f02a29da475cf3ea (diff)
BLI_heap: minor changes to the API
Recent addition of 'reinsert' didn't match logic for ghash API. Rename to BLI_heap_node_value_update, also add BLI_heap_insert_or_update since it's a common operation.
Diffstat (limited to 'source/blender/blenlib/intern/BLI_heap.c')
-rw-r--r--source/blender/blenlib/intern/BLI_heap.c46
1 files changed, 36 insertions, 10 deletions
diff --git a/source/blender/blenlib/intern/BLI_heap.c b/source/blender/blenlib/intern/BLI_heap.c
index d794332b5df..d6e8721faa7 100644
--- a/source/blender/blenlib/intern/BLI_heap.c
+++ b/source/blender/blenlib/intern/BLI_heap.c
@@ -275,6 +275,20 @@ HeapNode *BLI_heap_insert(Heap *heap, float value, void *ptr)
return node;
}
+/**
+ * Convenience function since this is a common pattern.
+ */
+void BLI_heap_insert_or_update(Heap *heap, HeapNode **node_p, float value, void *ptr)
+{
+ if (*node_p == NULL) {
+ *node_p = BLI_heap_insert(heap, value, ptr);
+ }
+ else {
+ BLI_heap_node_value_update_ptr(heap, *node_p, value, ptr);
+ }
+}
+
+
bool BLI_heap_is_empty(Heap *heap)
{
return (heap->size == 0);
@@ -306,16 +320,6 @@ void *BLI_heap_popmin(Heap *heap)
return ptr;
}
-void BLI_heap_reinsert(Heap *heap, HeapNode *node, float value)
-{
- if (value == node->value) {
- return;
- }
- node->value = value;
- heap_up(heap, node->index);
- heap_down(heap, node->index);
-}
-
void BLI_heap_remove(Heap *heap, HeapNode *node)
{
uint i = node->index;
@@ -332,6 +336,28 @@ void BLI_heap_remove(Heap *heap, HeapNode *node)
BLI_heap_popmin(heap);
}
+/**
+ * Can be used to avoid #BLI_heap_remove, #BLI_heap_insert calls,
+ * balancing the tree still has a performance cost,
+ * but is often much less than remove/insert, difference is most noticable with large heaps.
+ */
+void BLI_heap_node_value_update(Heap *heap, HeapNode *node, float value)
+{
+ if (value == node->value) {
+ return;
+ }
+ node->value = value;
+ /* Can be called in either order, makes no difference. */
+ heap_up(heap, node->index);
+ heap_down(heap, node->index);
+}
+
+void BLI_heap_node_value_update_ptr(Heap *heap, HeapNode *node, float value, void *ptr)
+{
+ node->ptr = ptr;
+ BLI_heap_node_value_update(heap, node, value);
+}
+
float BLI_heap_node_value(HeapNode *node)
{
return node->value;