ir: add bt_ctf_trace_get_stream_class_by_id()
[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.
c462e188
MD
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * SOFTWARE.
1eb0c69c
MD
29 */
30
31#include <unistd.h>
96e1cf26 32#include <babeltrace/babeltrace-internal.h>
1eb0c69c
MD
33
34struct ptr_heap {
35 size_t len, alloc_len;
36 void **ptrs;
37 int (*gt)(void *a, void *b);
38};
39
eacd552c
MD
40#ifdef DEBUG_HEAP
41void check_heap(const struct ptr_heap *heap);
42#else
43static inline
44void check_heap(const struct ptr_heap *heap)
45{
46}
47#endif
48
1eb0c69c 49/**
dd06413f 50 * bt_heap_maximum - return the largest element in the heap
1eb0c69c
MD
51 * @heap: the heap to be operated on
52 *
53 * Returns the largest element in the heap, without performing any modification
54 * to the heap structure. Returns NULL if the heap is empty.
55 */
dd06413f 56static inline void *bt_heap_maximum(const struct ptr_heap *heap)
1eb0c69c 57{
eacd552c 58 check_heap(heap);
96e1cf26 59 return likely(heap->len) ? heap->ptrs[0] : NULL;
1eb0c69c
MD
60}
61
62/**
dd06413f 63 * bt_heap_init - initialize the heap
1eb0c69c
MD
64 * @heap: the heap to initialize
65 * @alloc_len: number of elements initially allocated
66 * @gt: function to compare the elements
67 *
68 * Returns -ENOMEM if out of memory.
69 */
dd06413f 70extern int bt_heap_init(struct ptr_heap *heap,
1eb0c69c
MD
71 size_t alloc_len,
72 int gt(void *a, void *b));
73
74/**
dd06413f 75 * bt_heap_free - free the heap
1eb0c69c
MD
76 * @heap: the heap to free
77 */
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 */
dd06413f 89extern int bt_heap_insert(struct ptr_heap *heap, void *p);
1eb0c69c
MD
90
91/**
dd06413f 92 * bt_heap_remove - remove the largest element from the heap
1eb0c69c
MD
93 * @heap: the heap to be operated on
94 *
95 * Returns the largest element in the heap. It removes this element from the
96 * heap. Returns NULL if the heap is empty.
97 */
dd06413f 98extern void *bt_heap_remove(struct ptr_heap *heap);
1eb0c69c
MD
99
100/**
dd06413f 101 * bt_heap_cherrypick - remove a given element from the heap
1eb0c69c
MD
102 * @heap: the heap to be operated on
103 * @p: the element
104 *
105 * Remove the given element from the heap. Return the element if present, else
106 * return NULL. This algorithm has a complexity of O(n), which is higher than
107 * O(log(n)) provided by the rest of this API.
108 */
dd06413f 109extern void *bt_heap_cherrypick(struct ptr_heap *heap, void *p);
1eb0c69c
MD
110
111/**
dd06413f 112 * bt_heap_replace_max - replace the the largest element from the heap
1eb0c69c
MD
113 * @heap: the heap to be operated on
114 * @p: the pointer to be inserted as topmost element replacement
115 *
116 * Returns the largest element in the heap. It removes this element from the
117 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
118 * the heap is empty.
119 *
dd06413f 120 * This is the equivalent of calling bt_heap_remove() and then bt_heap_insert(), but
1eb0c69c
MD
121 * it only rebalances the heap once. It never allocates memory.
122 */
dd06413f 123extern void *bt_heap_replace_max(struct ptr_heap *heap, void *p);
1eb0c69c 124
23a151f0 125/**
dd06413f 126 * bt_heap_copy - copy a heap
23a151f0
JD
127 * @dst: the destination heap (must be allocated)
128 * @src: the source heap
129 *
130 * Returns -ENOMEM if out of memory.
131 */
dd06413f 132extern int bt_heap_copy(struct ptr_heap *dst, struct ptr_heap *src);
23a151f0 133
1eb0c69c 134#endif /* _BABELTRACE_PRIO_HEAP_H */
This page took 0.036549 seconds and 4 git commands to generate.