39 static inline long int random(
void)
55 #define MIN_CAPACITY 16 76 char *tmp = malloc(
sizeof(
char)*heap->
el_sz);
89 void **pp = (
void**) heap->
data;
103 if (el_sz ==
sizeof(
void*))
124 if (heap->
alloc >= capacity)
127 int newcap = heap->
alloc;
129 while (newcap < capacity) {
138 heap->
values = realloc(heap->
values, newcap *
sizeof(
float));
140 heap->
alloc = newcap;
146 assert (isfinite(v) &&
"zmaxheap_add: Trying to add non-finite number to heap. NaN's prohibited, could allow INF with testing");
149 int idx = heap->
size;
158 int parent = (idx - 1) / 2;
161 if (heap->
values[parent] >= v)
165 heap->
swap(heap, idx, parent);
172 assert(heap != NULL);
174 assert(heap->
el_sz ==
sizeof(
void*));
176 for (
int idx = 0; idx < heap->
size; idx++) {
180 debug_print(
"Warning: zmaxheap_vmap item %d is NULL\n", idx);
191 if (idx >= heap->
size)
204 if (idx == heap->
size)
216 float parent_score = heap->
values[idx];
219 while (parent < heap->
size) {
221 int left = 2*parent + 1;
222 int right = left + 1;
226 float left_score = (left < heap->
size) ? heap->
values[left] : -INFINITY;
227 float right_score = (right < heap->size) ? heap->
values[right] : -INFINITY;
232 if (parent_score >= left_score && parent_score >= right_score)
236 if (left_score >= right_score) {
237 assert(left < heap->size);
238 heap->
swap(heap, parent, left);
242 assert(right < heap->size);
243 heap->
swap(heap, parent, right);
274 if (it->
in != it->
out) {
294 if (it->
in != it->
out) {
311 int left = 2*parent + 1;
312 int right = 2*parent + 2;
314 int betterchild = parent;
321 if (betterchild != parent) {
322 heap->
swap(heap, parent, betterchild);
327 #if 0 //won't compile if defined but not used 331 for (
int parent = 0; parent < heap->
size; parent++) {
332 int left = 2*parent + 1;
333 int right = 2*parent + 2;
335 if (left < heap->
size) {
339 if (right < heap->size) {
348 if (it->
in == it->
out)
356 for (
int i = heap->
size/2 - 1; i >= 0; i--)
364 int32_t *vals = calloc(
sizeof(int32_t), cap);
371 for (
int iter = 0; iter < 5000000; iter++) {
372 assert(sz == heap->
size);
374 if ((random() & 1) == 0 && sz < cap) {
376 int32_t v = (int32_t) (random() / 1000);
387 int maxv = -1, maxi = -1;
389 for (
int i = 0; i < sz; i++) {
390 if (vals[i] > maxv) {
404 assert(outv == outfv);
405 assert(maxv == outv);
408 vals[maxi] = vals[sz - 1];
416 if (maxsz > 0 && sz == 0)
420 printf(
"max size: %d, zcount %d\n", maxsz, zcnt);
void zmaxheap_iterator_init(zmaxheap_t *heap, zmaxheap_iterator_t *it)
void zmaxheap_iterator_remove(zmaxheap_iterator_t *it)
void zmaxheap_destroy(zmaxheap_t *heap)
#define debug_print(fmt,...)
void zmaxheap_vmap(zmaxheap_t *heap, void(*f)())
int zmaxheap_remove_index(zmaxheap_t *heap, int idx, void *p, float *v)
void(* swap)(zmaxheap_t *heap, int a, int b)
static void swap_default(zmaxheap_t *heap, int a, int b)
int zmaxheap_size(zmaxheap_t *heap)
zmaxheap_t * zmaxheap_create(size_t el_sz)
static void swap_pointer(zmaxheap_t *heap, int a, int b)
void zmaxheap_iterator_finish(zmaxheap_iterator_t *it)
int zmaxheap_iterator_next(zmaxheap_iterator_t *it, void *p, float *v)
static void maxheapify(zmaxheap_t *heap, int parent)
void zmaxheap_add(zmaxheap_t *heap, void *p, float v)
int zmaxheap_iterator_next_volatile(zmaxheap_iterator_t *it, void *p, float *v)
void zmaxheap_ensure_capacity(zmaxheap_t *heap, int capacity)
int zmaxheap_remove_max(zmaxheap_t *heap, void *p, float *v)