Add heap debug option, fix heap
[babeltrace.git] / include / babeltrace / prio_heap.h
CommitLineData
1eb0c69c
MD
1#ifndef _BABELTRACE_PRIO_HEAP_H
2#define _BABELTRACE_PRIO_HEAP_H
3
4/*
5 * prio_heap.h
6 *
7 * Static-sized priority heap containing pointers. Based on CLRS,
8 * chapter 6.
9 *
10 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 */
22
23#include <unistd.h>
24
25struct ptr_heap {
26 size_t len, alloc_len;
27 void **ptrs;
28 int (*gt)(void *a, void *b);
29};
30
eacd552c
MD
31#ifdef DEBUG_HEAP
32void check_heap(const struct ptr_heap *heap);
33#else
34static inline
35void check_heap(const struct ptr_heap *heap)
36{
37}
38#endif
39
1eb0c69c
MD
40/**
41 * heap_maximum - return the largest element in the heap
42 * @heap: the heap to be operated on
43 *
44 * Returns the largest element in the heap, without performing any modification
45 * to the heap structure. Returns NULL if the heap is empty.
46 */
47static inline void *heap_maximum(const struct ptr_heap *heap)
48{
eacd552c 49 check_heap(heap);
1eb0c69c
MD
50 return heap->len ? heap->ptrs[0] : NULL;
51}
52
53/**
54 * heap_init - initialize the heap
55 * @heap: the heap to initialize
56 * @alloc_len: number of elements initially allocated
57 * @gt: function to compare the elements
58 *
59 * Returns -ENOMEM if out of memory.
60 */
61extern int heap_init(struct ptr_heap *heap,
62 size_t alloc_len,
63 int gt(void *a, void *b));
64
65/**
66 * heap_free - free the heap
67 * @heap: the heap to free
68 */
69extern void heap_free(struct ptr_heap *heap);
70
71/**
72 * heap_insert - insert an element into the heap
73 * @heap: the heap to be operated on
74 * @p: the element to add
75 *
76 * Insert an element into the heap.
77 *
78 * Returns -ENOMEM if out of memory.
79 */
80extern int heap_insert(struct ptr_heap *heap, void *p);
81
82/**
83 * heap_remove - remove the largest element from the heap
84 * @heap: the heap to be operated on
85 *
86 * Returns the largest element in the heap. It removes this element from the
87 * heap. Returns NULL if the heap is empty.
88 */
89extern void *heap_remove(struct ptr_heap *heap);
90
91/**
92 * heap_cherrypick - remove a given element from the heap
93 * @heap: the heap to be operated on
94 * @p: the element
95 *
96 * Remove the given element from the heap. Return the element if present, else
97 * return NULL. This algorithm has a complexity of O(n), which is higher than
98 * O(log(n)) provided by the rest of this API.
99 */
100extern void *heap_cherrypick(struct ptr_heap *heap, void *p);
101
102/**
103 * heap_replace_max - replace the the largest element from the heap
104 * @heap: the heap to be operated on
105 * @p: the pointer to be inserted as topmost element replacement
106 *
107 * Returns the largest element in the heap. It removes this element from the
108 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
109 * the heap is empty.
110 *
111 * This is the equivalent of calling heap_remove() and then heap_insert(), but
112 * it only rebalances the heap once. It never allocates memory.
113 */
114extern void *heap_replace_max(struct ptr_heap *heap, void *p);
115
116#endif /* _BABELTRACE_PRIO_HEAP_H */
This page took 0.026262 seconds and 4 git commands to generate.