1 #ifndef _BABELTRACE_PRIO_HEAP_H
2 #define _BABELTRACE_PRIO_HEAP_H
5 * Static-sized priority heap containing pointers. Based on CLRS,
8 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
30 #include <babeltrace/babeltrace-internal.h>
33 size_t len
, alloc_len
;
35 int (*gt
)(void *a
, void *b
);
39 void check_heap(const struct ptr_heap
*heap
);
42 void check_heap(const struct ptr_heap
*heap
)
48 * bt_heap_maximum - return the largest element in the heap
49 * @heap: the heap to be operated on
51 * Returns the largest element in the heap, without performing any modification
52 * to the heap structure. Returns NULL if the heap is empty.
54 static inline void *bt_heap_maximum(const struct ptr_heap
*heap
)
57 return likely(heap
->len
) ? heap
->ptrs
[0] : NULL
;
61 * bt_heap_init - initialize the heap
62 * @heap: the heap to initialize
63 * @alloc_len: number of elements initially allocated
64 * @gt: function to compare the elements
66 * Returns -ENOMEM if out of memory.
68 extern int bt_heap_init(struct ptr_heap
*heap
,
70 int gt(void *a
, void *b
));
73 * bt_heap_free - free the heap
74 * @heap: the heap to free
76 extern void bt_heap_free(struct ptr_heap
*heap
);
79 * bt_heap_insert - insert an element into the heap
80 * @heap: the heap to be operated on
81 * @p: the element to add
83 * Insert an element into the heap.
85 * Returns -ENOMEM if out of memory.
87 extern int bt_heap_insert(struct ptr_heap
*heap
, void *p
);
90 * bt_heap_remove - remove the largest element from the heap
91 * @heap: the heap to be operated on
93 * Returns the largest element in the heap. It removes this element from the
94 * heap. Returns NULL if the heap is empty.
96 extern void *bt_heap_remove(struct ptr_heap
*heap
);
99 * bt_heap_cherrypick - remove a given element from the heap
100 * @heap: the heap to be operated on
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.
107 extern void *bt_heap_cherrypick(struct ptr_heap
*heap
, void *p
);
110 * bt_heap_replace_max - replace the the largest element from the heap
111 * @heap: the heap to be operated on
112 * @p: the pointer to be inserted as topmost element replacement
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
118 * This is the equivalent of calling bt_heap_remove() and then bt_heap_insert(), but
119 * it only rebalances the heap once. It never allocates memory.
121 extern void *bt_heap_replace_max(struct ptr_heap
*heap
, void *p
);
124 * bt_heap_copy - copy a heap
125 * @dst: the destination heap (must be allocated)
126 * @src: the source heap
128 * Returns -ENOMEM if out of memory.
130 extern int bt_heap_copy(struct ptr_heap
*dst
, struct ptr_heap
*src
);
132 #endif /* _BABELTRACE_PRIO_HEAP_H */
This page took 0.037982 seconds and 4 git commands to generate.