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