lib: make symbols in prio-head hidden
[babeltrace.git] / src / lib / prio-heap / prio-heap.h
CommitLineData
1eb0c69c
MD
1#ifndef _BABELTRACE_PRIO_HEAP_H
2#define _BABELTRACE_PRIO_HEAP_H
3
4/*
1eb0c69c
MD
5 * Static-sized priority heap containing pointers. Based on CLRS,
6 * chapter 6.
7 *
8 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
c462e188
MD
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
1eb0c69c
MD
27 */
28
29#include <unistd.h>
91d81473 30#include "common/macros.h"
1eb0c69c
MD
31
32struct ptr_heap {
33 size_t len, alloc_len;
34 void **ptrs;
35 int (*gt)(void *a, void *b);
36};
37
eacd552c
MD
38#ifdef DEBUG_HEAP
39void check_heap(const struct ptr_heap *heap);
40#else
41static inline
42void check_heap(const struct ptr_heap *heap)
43{
44}
45#endif
46
1eb0c69c 47/**
dd06413f 48 * bt_heap_maximum - return the largest element in the heap
1eb0c69c
MD
49 * @heap: the heap to be operated on
50 *
51 * Returns the largest element in the heap, without performing any modification
52 * to the heap structure. Returns NULL if the heap is empty.
53 */
dd06413f 54static inline void *bt_heap_maximum(const struct ptr_heap *heap)
1eb0c69c 55{
eacd552c 56 check_heap(heap);
91d81473 57 return G_LIKELY(heap->len) ? heap->ptrs[0] : NULL;
1eb0c69c
MD
58}
59
60/**
dd06413f 61 * bt_heap_init - initialize the heap
1eb0c69c
MD
62 * @heap: the heap to initialize
63 * @alloc_len: number of elements initially allocated
64 * @gt: function to compare the elements
65 *
66 * Returns -ENOMEM if out of memory.
67 */
ec21af4a 68BT_HIDDEN
dd06413f 69extern int bt_heap_init(struct ptr_heap *heap,
1eb0c69c
MD
70 size_t alloc_len,
71 int gt(void *a, void *b));
72
73/**
dd06413f 74 * bt_heap_free - free the heap
1eb0c69c
MD
75 * @heap: the heap to free
76 */
ec21af4a 77BT_HIDDEN
dd06413f 78extern void bt_heap_free(struct ptr_heap *heap);
1eb0c69c
MD
79
80/**
dd06413f 81 * bt_heap_insert - insert an element into the heap
1eb0c69c
MD
82 * @heap: the heap to be operated on
83 * @p: the element to add
84 *
85 * Insert an element into the heap.
86 *
87 * Returns -ENOMEM if out of memory.
88 */
ec21af4a 89BT_HIDDEN
dd06413f 90extern int bt_heap_insert(struct ptr_heap *heap, void *p);
1eb0c69c
MD
91
92/**
dd06413f 93 * bt_heap_remove - remove the largest element from the heap
1eb0c69c
MD
94 * @heap: the heap to be operated on
95 *
96 * Returns the largest element in the heap. It removes this element from the
97 * heap. Returns NULL if the heap is empty.
98 */
ec21af4a 99BT_HIDDEN
dd06413f 100extern void *bt_heap_remove(struct ptr_heap *heap);
1eb0c69c
MD
101
102/**
dd06413f 103 * bt_heap_cherrypick - remove a given element from the heap
1eb0c69c
MD
104 * @heap: the heap to be operated on
105 * @p: the element
106 *
107 * Remove the given element from the heap. Return the element if present, else
108 * return NULL. This algorithm has a complexity of O(n), which is higher than
109 * O(log(n)) provided by the rest of this API.
110 */
ec21af4a 111BT_HIDDEN
dd06413f 112extern void *bt_heap_cherrypick(struct ptr_heap *heap, void *p);
1eb0c69c
MD
113
114/**
dd06413f 115 * bt_heap_replace_max - replace the the largest element from the heap
1eb0c69c
MD
116 * @heap: the heap to be operated on
117 * @p: the pointer to be inserted as topmost element replacement
118 *
119 * Returns the largest element in the heap. It removes this element from the
120 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
121 * the heap is empty.
122 *
dd06413f 123 * This is the equivalent of calling bt_heap_remove() and then bt_heap_insert(), but
1eb0c69c
MD
124 * it only rebalances the heap once. It never allocates memory.
125 */
ec21af4a 126BT_HIDDEN
dd06413f 127extern void *bt_heap_replace_max(struct ptr_heap *heap, void *p);
1eb0c69c 128
23a151f0 129/**
dd06413f 130 * bt_heap_copy - copy a heap
23a151f0
JD
131 * @dst: the destination heap (must be allocated)
132 * @src: the source heap
133 *
134 * Returns -ENOMEM if out of memory.
135 */
ec21af4a 136BT_HIDDEN
dd06413f 137extern int bt_heap_copy(struct ptr_heap *dst, struct ptr_heap *src);
23a151f0 138
1eb0c69c 139#endif /* _BABELTRACE_PRIO_HEAP_H */
This page took 0.085331 seconds and 4 git commands to generate.