Visibility hidden by default
[babeltrace.git] / src / lib / prio-heap / prio-heap.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Static-sized priority heap containing pointers. Based on CLRS,
7 * chapter 6.
8 */
9
10 #ifndef _BABELTRACE_PRIO_HEAP_H
11 #define _BABELTRACE_PRIO_HEAP_H
12
13 #include <unistd.h>
14 #include "common/macros.h"
15
16 struct ptr_heap {
17 size_t len, alloc_len;
18 void **ptrs;
19 int (*gt)(void *a, void *b);
20 };
21
22 #ifdef DEBUG_HEAP
23 void check_heap(const struct ptr_heap *heap);
24 #else
25 static inline
26 void check_heap(const struct ptr_heap *heap)
27 {
28 }
29 #endif
30
31 /**
32 * bt_heap_maximum - return the largest element in the heap
33 * @heap: the heap to be operated on
34 *
35 * Returns the largest element in the heap, without performing any modification
36 * to the heap structure. Returns NULL if the heap is empty.
37 */
38 static inline void *bt_heap_maximum(const struct ptr_heap *heap)
39 {
40 check_heap(heap);
41 return G_LIKELY(heap->len) ? heap->ptrs[0] : NULL;
42 }
43
44 /**
45 * bt_heap_init - initialize the heap
46 * @heap: the heap to initialize
47 * @alloc_len: number of elements initially allocated
48 * @gt: function to compare the elements
49 *
50 * Returns -ENOMEM if out of memory.
51 */
52 extern int bt_heap_init(struct ptr_heap *heap,
53 size_t alloc_len,
54 int gt(void *a, void *b));
55
56 /**
57 * bt_heap_free - free the heap
58 * @heap: the heap to free
59 */
60 extern void bt_heap_free(struct ptr_heap *heap);
61
62 /**
63 * bt_heap_insert - insert an element into the heap
64 * @heap: the heap to be operated on
65 * @p: the element to add
66 *
67 * Insert an element into the heap.
68 *
69 * Returns -ENOMEM if out of memory.
70 */
71 extern int bt_heap_insert(struct ptr_heap *heap, void *p);
72
73 /**
74 * bt_heap_remove - remove the largest element from the heap
75 * @heap: the heap to be operated on
76 *
77 * Returns the largest element in the heap. It removes this element from the
78 * heap. Returns NULL if the heap is empty.
79 */
80 extern void *bt_heap_remove(struct ptr_heap *heap);
81
82 /**
83 * bt_heap_cherrypick - remove a given element from the heap
84 * @heap: the heap to be operated on
85 * @p: the element
86 *
87 * Remove the given element from the heap. Return the element if present, else
88 * return NULL. This algorithm has a complexity of O(n), which is higher than
89 * O(log(n)) provided by the rest of this API.
90 */
91 extern void *bt_heap_cherrypick(struct ptr_heap *heap, void *p);
92
93 /**
94 * bt_heap_replace_max - replace the the largest element from the heap
95 * @heap: the heap to be operated on
96 * @p: the pointer to be inserted as topmost element replacement
97 *
98 * Returns the largest element in the heap. It removes this element from the
99 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
100 * the heap is empty.
101 *
102 * This is the equivalent of calling bt_heap_remove() and then bt_heap_insert(), but
103 * it only rebalances the heap once. It never allocates memory.
104 */
105 extern void *bt_heap_replace_max(struct ptr_heap *heap, void *p);
106
107 /**
108 * bt_heap_copy - copy a heap
109 * @dst: the destination heap (must be allocated)
110 * @src: the source heap
111 *
112 * Returns -ENOMEM if out of memory.
113 */
114 extern int bt_heap_copy(struct ptr_heap *dst, struct ptr_heap *src);
115
116 #endif /* _BABELTRACE_PRIO_HEAP_H */
This page took 0.031739 seconds and 4 git commands to generate.