Simple data structures / objects in plain C Snapshot
Functions
heap.c File Reference
#include <heap.h>
#include <stdlib.h>
#include <memory.h>

Go to the source code of this file.

Functions

int HEAP_push (HEAP *heap, void *element, size_t elmsize)
 insert a new element into the heap
int HEAP_pop (HEAP *heap)
 remove the top element from the heap
static int check_recursive (HEAP *heap, size_t pos)
int HEAP_check (HEAP *heap)
 check consistency of heap object.
static void visit_sorted (HEAP *heap, size_t pos, HEAP_VISITOR eval, void *context)
void HEAP_foreach_sorted (HEAP *heap, HEAP_VISITOR eval, void *context)

Function Documentation

static int check_recursive ( HEAP heap,
size_t  pos 
) [static]

Definition at line 129 of file heap.c.

{
        size_t elmsize = heap->elmsize;
        size_t pos_left;
        size_t pos_right;

        pos_left = (2 * pos);
        if (pos_left <= heap->elmcount) {

                if (heap->compare_func( heap->buffer + (pos - 1) * elmsize,  
                                                            heap->buffer + (pos_left - 1) * elmsize, 
                                                                elmsize) > 0) {
                        return 0;
                }
                if (!check_recursive( heap, pos_left)) {
                        return 0;
                }
        }


        pos_right =   (2 * pos + 1) * elmsize;
        if (pos_right <= heap->elmcount) {

                if (heap->compare_func( heap->buffer + (pos - 1) * elmsize,  
                                                            heap->buffer + (pos_right - 1) * elmsize, 
                                                            elmsize) > 0) {
                        return 0;
                }
                if (!check_recursive( heap, pos_right)) {
                        return 0;
                }
        }
        return 1;
}
static void visit_sorted ( HEAP heap,
size_t  pos,
HEAP_VISITOR  eval,
void *  context 
) [static]

Definition at line 178 of file heap.c.

{
        size_t elmsize = heap->elmsize;
        size_t pos_left;
        size_t pos_right;

        if (heap->elmcount > heap->elmmaxcount) {
                return;
        }

        pos_left = (2 * pos);
        if (pos_left <= heap->elmcount) {

                check_recursive( heap, pos_left );
        }

        pos_right =   (2 * pos + 1) * elmsize;
        if (pos_right <= heap->elmcount) {

                check_recursive( heap, pos_right );
        }

        eval( pos, heap->buffer + (pos - 1) * elmsize, elmsize, context );
}