22#include <apr_thread_mutex.h>
28#define INITIAL_MAX 256
29#define NAPR_HEAP_PARENT(position) (((position) - 1) >> 1)
30#define NAPR_HEAP_LEFT(position) (((position) << 1) + 1)
31#define NAPR_HEAP_RIGHT(position) (((position) + 1) << 1)
36 apr_thread_mutex_t *mutex;
39 unsigned int count, max;
51 apr_pool_t *local_pool;
53 if (APR_SUCCESS == (apr_pool_create(&local_pool, pool))) {
55 heap->pool = local_pool;
56 heap->tree = (
void **) apr_pcalloc(local_pool,
sizeof(
void *) * INITIAL_MAX);
60 heap->max = INITIAL_MAX;
73 unsigned int ipos, ppos;
75 if (heap->max <= heap->count) {
81 for (new_max = 1; new_max <= heap->max; new_max *= 2);
83 tmp = apr_palloc(heap->pool, new_max *
sizeof(
void *));
85 memcpy(tmp, (heap->tree), (heap->count) *
sizeof(
void *));
86 memset((tmp + (heap->count)), 0, (new_max - (heap->count + 1)) *
sizeof(
void *));
100 heap->tree[heap->count] = datum;
103 ppos = NAPR_HEAP_PARENT(ipos);
105 while (ipos > 0 && (heap->cmp(heap->tree[ppos], heap->tree[ipos]) < 0)) {
109 tmp = heap->tree[ppos];
110 heap->tree[ppos] = heap->tree[ipos];
111 heap->tree[ipos] = tmp;
114 ppos = NAPR_HEAP_PARENT(ipos);
124 void *ret = NULL, *tmp;
125 unsigned int ipos, rpos, lpos, mpos;
127 if ((0 != heap->count) && (NULL != heap->tree)) {
131 heap->tree[0] = heap->tree[heap->count - 1];
132 heap->tree[heap->count - 1] = NULL;
137 lpos = NAPR_HEAP_LEFT(ipos);
138 rpos = NAPR_HEAP_RIGHT(ipos);
140 if (lpos < heap->count) {
141 if (heap->cmp(heap->tree[lpos], heap->tree[ipos]) > 0) {
147 if ((rpos < heap->count) && (heap->cmp(heap->tree[rpos], heap->tree[mpos])) > 0) {
159 tmp = heap->tree[mpos];
160 heap->tree[mpos] = heap->tree[ipos];
161 heap->tree[ipos] = tmp;
175 if ((n < heap->count) && (NULL != heap->tree))
176 return heap->tree[n];
194 if (APR_SUCCESS != apr_thread_mutex_create(&(heap->mutex), APR_THREAD_MUTEX_DEFAULT, pool))
208 if (1 == heap->mutex_set) {
209 if (APR_SUCCESS == (rc = apr_thread_mutex_lock(heap->mutex))) {
211 rc = apr_thread_mutex_unlock(heap->mutex);
UTIL debug output macros.
#define DEBUG_ERR(str, arg...)
Display error message at the level error.
void * napr_heap_extract(napr_heap_t *heap)
Removes and returns the element at the top of the heap (the min or max element).
napr_heap_t * napr_heap_make(apr_pool_t *pool, napr_heap_cmp_callback_fn_t *cmp)
Creates a new heap.
int napr_heap_insert(napr_heap_t *heap, void *datum)
Inserts an element into the heap, maintaining the heap property.
napr_heap_t * napr_heap_make_r(apr_pool_t *pool, napr_heap_cmp_callback_fn_t *cmp)
Creates a new thread-safe heap.
unsigned int napr_heap_size(const napr_heap_t *heap)
Gets the current number of elements in the heap.
void * napr_heap_get_nth(const napr_heap_t *heap, unsigned int n)
Gets the element at a specific index in the heap's internal array.
int napr_heap_insert_r(napr_heap_t *heap, void *datum)
Inserts an element into a thread-safe heap.
A generic binary heap implementation (min-heap or max-heap).
struct napr_heap_t napr_heap_t
Opaque heap structure.
int() napr_heap_cmp_callback_fn_t(const void *, const void *)
Callback function to compare two elements in the heap.