-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathheap.h
43 lines (32 loc) · 1.01 KB
/
heap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef HEAP_H
#define HEAP_H
#include <stdbool.h>
#include <stddef.h>
/// Type used to store a heap.
typedef struct heap {
void ** entries;
void ** entries_end;
size_t index_offset;
/// @c compare should return >0 if first arg is greater than second, and <=0
/// otherwise. Thus either a strcmp or a '>' like predicate can be used.
int (*compare) (const void *, const void *);
} heap_t;
/// Initialise a new heap.
void heap_init (heap_t * heap, size_t offset,
int (*compare) (const void *, const void *));
/// Destroy a heap.
void heap_destroy (heap_t * heap);
/// Insert an item.
void heap_insert (heap_t * heap, void * item);
/// Remove an item.
void heap_remove (heap_t * heap, void * item);
/// Return least item from a heap.
void * heap_front (heap_t * heap);
/// Return least item from a heap, after removing it.
void * heap_pop (heap_t * heap);
/// Is a heap empty?
static inline bool heap_empty (heap_t * heap)
{
return heap->entries == heap->entries_end;
}
#endif