BLI_heap: add an API function to directly read the top node value.
authorAlexander Gavrilov <angavrilov@gmail.com>
Sun, 4 Nov 2018 10:27:10 +0000 (13:27 +0300)
committerAlexander Gavrilov <angavrilov@gmail.com>
Sun, 4 Nov 2018 10:29:17 +0000 (13:29 +0300)
It is very commonly needed in loop conditions to check if
the items in the heap are good enough to continue.

source/blender/blenkernel/intern/colorband.c
source/blender/blenlib/BLI_heap.h
source/blender/blenlib/intern/BLI_heap.c
source/blender/bmesh/tools/bmesh_decimate_collapse.c
source/blender/editors/mesh/editmesh_tools.c
tests/gtests/blenlib/BLI_heap_test.cc

index f72d4df725ad8183051ee496483d284eeae81f4b..38631c76009fe9f96afc7f8a27dd536c761867c5 100644 (file)
@@ -208,7 +208,7 @@ static void colorband_init_from_table_rgba_resample(
        }
 
        while ((carr_len > 1 && !BLI_heap_is_empty(heap)) &&
-              ((carr_len >= MAXCOLORBAND) || (BLI_heap_node_value(BLI_heap_top(heap)) <= eps_2x)))
+              ((carr_len >= MAXCOLORBAND) || (BLI_heap_top_value(heap) <= eps_2x)))
        {
                c = BLI_heap_pop_min(heap);
                struct ColorResampleElem *c_next = c->next, *c_prev = c->prev;
index 771b9dabe4deacbbacde559f3acb72ffb17d10fb..35c8df3075c20c8e4765753fb36d6f0ea28998b5 100644 (file)
@@ -43,6 +43,7 @@ void            BLI_heap_remove(Heap *heap, HeapNode *node) ATTR_NONNULL(1, 2);
 bool            BLI_heap_is_empty(const Heap *heap) ATTR_NONNULL(1);
 unsigned int    BLI_heap_len(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
 HeapNode       *BLI_heap_top(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
+float           BLI_heap_top_value(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
 void           *BLI_heap_pop_min(Heap *heap) ATTR_NONNULL(1);
 void            BLI_heap_node_value_update(Heap *heap, HeapNode *node, float value) ATTR_NONNULL(1, 2);
 void            BLI_heap_node_value_update_ptr(Heap *heap, HeapNode *node, float value, void *ptr) ATTR_NONNULL(1, 2);
index 5658c1fd1037aa5546ae90f22be4e35c89d6bf65..17a15f9326688fcaccfcf198f5483586045200a7 100644 (file)
@@ -317,6 +317,17 @@ HeapNode *BLI_heap_top(const Heap *heap)
        return heap->tree[0];
 }
 
+/**
+ * Return the value of top node of the heap.
+ * This is the node with the lowest value.
+ */
+float BLI_heap_top_value(const Heap *heap)
+{
+       BLI_assert(heap->size != 0);
+
+       return heap->tree[0]->value;
+}
+
 /**
  * Pop the top node off the heap and return it's pointer.
  */
index c1b2bc2625b1edaf45033a9a48962406f547db2b..fa427b3b9ebc1028fa083e052e7477b5b0993c0b 100644 (file)
@@ -1354,7 +1354,7 @@ void BM_mesh_decimate_collapse(
                /* simple non-mirror case */
                while ((bm->totface > face_tot_target) &&
                       (BLI_heap_is_empty(eheap) == false) &&
-                      (BLI_heap_node_value(BLI_heap_top(eheap)) != COST_INVALID))
+                      (BLI_heap_top_value(eheap) != COST_INVALID))
                {
                        // const float value = BLI_heap_node_value(BLI_heap_top(eheap));
                        BMEdge *e = BLI_heap_pop_min(eheap);
@@ -1379,7 +1379,7 @@ void BM_mesh_decimate_collapse(
        else {
                while ((bm->totface > face_tot_target) &&
                       (BLI_heap_is_empty(eheap) == false) &&
-                      (BLI_heap_node_value(BLI_heap_top(eheap)) != COST_INVALID))
+                      (BLI_heap_top_value(eheap) != COST_INVALID))
                {
                        /**
                         * \note
index c11aefb7e14dcff8bb8eee56bcd2270db8dafe5d..b16195023c9671de23b893c254fb1d17cd2c8c6b 100644 (file)
@@ -7868,10 +7868,10 @@ static int edbm_average_normals_exec(bContext *C, wmOperator *op)
 
                                        BLI_SMALLSTACK_DECLARE(loops, BMLoop *);
                                        float wnor[3], avg_normal[3] = { 0.0f }, count = 0;
-                                       float val = BLI_heap_node_value(BLI_heap_top(loop_weight));
+                                       float val = BLI_heap_top_value(loop_weight);
 
                                        while (!BLI_heap_is_empty(loop_weight)) {
-                                               const float cur_val = BLI_heap_node_value(BLI_heap_top(loop_weight));
+                                               const float cur_val = BLI_heap_top_value(loop_weight);
                                                if (!compare_ff(val, cur_val, threshold)) {
                                                        count++;
                                                        val = cur_val;
index 69566d8dca649420dcf565a10d020591ca17bba5..26f3aa19b9f831577e316fb142a50c6c01078ff9 100644 (file)
@@ -176,6 +176,7 @@ static void random_heap_reinsert_helper(
        for (int out_test = 0; out_test < items_total; out_test++) {
                HeapNode *node_top = BLI_heap_top(heap);
                float out = BLI_heap_node_value(node_top);
+               EXPECT_EQ(out, BLI_heap_top_value(heap));
                EXPECT_EQ((float)out_test, out);
                BLI_heap_pop_min(heap);
        }