tap-driver.sh: flush stdout after each test result
[babeltrace.git] / include / babeltrace2 / prio-heap-internal.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>
3fadfbc0 30#include <babeltrace2/babeltrace-internal.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);
96e1cf26 57 return 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 */
dd06413f 68extern int bt_heap_init(struct ptr_heap *heap,
1eb0c69c
MD
69 size_t alloc_len,
70 int gt(void *a, void *b));
71
72/**
dd06413f 73 * bt_heap_free - free the heap
1eb0c69c
MD
74 * @heap: the heap to free
75 */
dd06413f 76extern void bt_heap_free(struct ptr_heap *heap);
1eb0c69c
MD
77
78/**
dd06413f 79 * bt_heap_insert - insert an element into the heap
1eb0c69c
MD
80 * @heap: the heap to be operated on
81 * @p: the element to add
82 *
83 * Insert an element into the heap.
84 *
85 * Returns -ENOMEM if out of memory.
86 */
dd06413f 87extern int bt_heap_insert(struct ptr_heap *heap, void *p);
1eb0c69c
MD
88
89/**
dd06413f 90 * bt_heap_remove - remove the largest element from the heap
1eb0c69c
MD
91 * @heap: the heap to be operated on
92 *
93 * Returns the largest element in the heap. It removes this element from the
94 * heap. Returns NULL if the heap is empty.
95 */
dd06413f 96extern void *bt_heap_remove(struct ptr_heap *heap);
1eb0c69c
MD
97
98/**
dd06413f 99 * bt_heap_cherrypick - remove a given element from the heap
1eb0c69c
MD
100 * @heap: the heap to be operated on
101 * @p: the element
102 *
103 * Remove the given element from the heap. Return the element if present, else
104 * return NULL. This algorithm has a complexity of O(n), which is higher than
105 * O(log(n)) provided by the rest of this API.
106 */
dd06413f 107extern void *bt_heap_cherrypick(struct ptr_heap *heap, void *p);
1eb0c69c
MD
108
109/**
dd06413f 110 * bt_heap_replace_max - replace the the largest element from the heap
1eb0c69c
MD
111 * @heap: the heap to be operated on
112 * @p: the pointer to be inserted as topmost element replacement
113 *
114 * Returns the largest element in the heap. It removes this element from the
115 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
116 * the heap is empty.
117 *
dd06413f 118 * This is the equivalent of calling bt_heap_remove() and then bt_heap_insert(), but
1eb0c69c
MD
119 * it only rebalances the heap once. It never allocates memory.
120 */
dd06413f 121extern void *bt_heap_replace_max(struct ptr_heap *heap, void *p);
1eb0c69c 122
23a151f0 123/**
dd06413f 124 * bt_heap_copy - copy a heap
23a151f0
JD
125 * @dst: the destination heap (must be allocated)
126 * @src: the source heap
127 *
128 * Returns -ENOMEM if out of memory.
129 */
dd06413f 130extern int bt_heap_copy(struct ptr_heap *dst, struct ptr_heap *src);
23a151f0 131
1eb0c69c 132#endif /* _BABELTRACE_PRIO_HEAP_H */
This page took 0.058201 seconds and 4 git commands to generate.