2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2012-02-17 18:59:41 +00:00
|
|
|
#pragma once
|
2006-02-08 18:06:35 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bli
|
|
|
|
|
* \brief A min-heap / priority queue ADT
|
2011-02-18 13:58:08 +00:00
|
|
|
*/
|
|
|
|
|
|
2020-05-08 18:16:39 +02:00
|
|
|
#include "BLI_math.h"
|
|
|
|
|
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-02-08 18:06:35 +00:00
|
|
|
struct Heap;
|
|
|
|
|
struct HeapNode;
|
|
|
|
|
typedef struct Heap Heap;
|
|
|
|
|
typedef struct HeapNode HeapNode;
|
|
|
|
|
|
2012-05-12 20:39:39 +00:00
|
|
|
typedef void (*HeapFreeFP)(void *ptr);
|
2006-02-08 18:06:35 +00:00
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
|
* Creates a new heap. Removed nodes are recycled, so memory usage will not shrink.
|
|
|
|
|
*
|
|
|
|
|
* \note Use when the size of the heap is known in advance.
|
|
|
|
|
*/
|
2022-03-30 17:26:42 +11:00
|
|
|
Heap *BLI_heap_new_ex(unsigned int reserve_num) ATTR_WARN_UNUSED_RESULT;
|
2014-07-17 18:56:13 +10:00
|
|
|
Heap *BLI_heap_new(void) ATTR_WARN_UNUSED_RESULT;
|
2014-12-09 00:32:20 +01:00
|
|
|
void BLI_heap_clear(Heap *heap, HeapFreeFP ptrfreefp) ATTR_NONNULL(1);
|
2014-07-17 18:56:13 +10:00
|
|
|
void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp) ATTR_NONNULL(1);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
|
* Insert heap node with a value (often a 'cost') and pointer into the heap,
|
|
|
|
|
* duplicate values are allowed.
|
|
|
|
|
*/
|
2014-07-17 18:56:13 +10:00
|
|
|
HeapNode *BLI_heap_insert(Heap *heap, float value, void *ptr) ATTR_NONNULL(1);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
|
* Convenience function since this is a common pattern.
|
|
|
|
|
*/
|
2017-10-29 15:25:13 +11:00
|
|
|
void BLI_heap_insert_or_update(Heap *heap, HeapNode **node_p, float value, void *ptr)
|
|
|
|
|
ATTR_NONNULL(1, 2);
|
2014-07-17 18:56:13 +10:00
|
|
|
void BLI_heap_remove(Heap *heap, HeapNode *node) ATTR_NONNULL(1, 2);
|
2017-10-29 15:42:54 +11:00
|
|
|
bool BLI_heap_is_empty(const Heap *heap) ATTR_NONNULL(1);
|
2018-02-15 23:36:11 +11:00
|
|
|
unsigned int BLI_heap_len(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
|
* Return the top node of the heap.
|
|
|
|
|
* This is the node with the lowest value.
|
|
|
|
|
*/
|
2017-10-29 15:42:54 +11:00
|
|
|
HeapNode *BLI_heap_top(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
|
* Return the value of top node of the heap.
|
|
|
|
|
* This is the node with the lowest value.
|
|
|
|
|
*/
|
2018-11-04 13:27:10 +03:00
|
|
|
float BLI_heap_top_value(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
|
* Pop the top node off the heap and return its pointer.
|
|
|
|
|
*/
|
2018-02-15 23:36:11 +11:00
|
|
|
void *BLI_heap_pop_min(Heap *heap) ATTR_NONNULL(1);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
|
* 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 noticeable with large heaps.
|
|
|
|
|
*/
|
2017-10-29 15:25:13 +11:00
|
|
|
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);
|
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
|
* Return the value or pointer of a heap node.
|
|
|
|
|
*/
|
2017-10-29 15:42:54 +11:00
|
|
|
float BLI_heap_node_value(const HeapNode *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
|
|
|
|
|
void *BLI_heap_node_ptr(const HeapNode *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
|
* Only for checking internal errors (gtest).
|
|
|
|
|
*/
|
2017-10-29 18:23:33 +11:00
|
|
|
bool BLI_heap_is_valid(const Heap *heap);
|
2006-02-08 18:06:35 +00:00
|
|
|
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|