2 * SPDX-License-Identifier: MIT
4 * Copyright 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Static-sized priority heap containing pointers. Based on CLRS,
10 #ifndef _BABELTRACE_PRIO_HEAP_H
11 #define _BABELTRACE_PRIO_HEAP_H
14 #include "common/macros.h"
17 size_t len
, alloc_len
;
19 int (*gt
)(void *a
, void *b
);
23 void check_heap(const struct ptr_heap
*heap
);
26 void check_heap(const struct ptr_heap
*heap
)
32 * bt_heap_maximum - return the largest element in the heap
33 * @heap: the heap to be operated on
35 * Returns the largest element in the heap, without performing any modification
36 * to the heap structure. Returns NULL if the heap is empty.
38 static inline void *bt_heap_maximum(const struct ptr_heap
*heap
)
41 return G_LIKELY(heap
->len
) ? heap
->ptrs
[0] : NULL
;
45 * bt_heap_init - initialize the heap
46 * @heap: the heap to initialize
47 * @alloc_len: number of elements initially allocated
48 * @gt: function to compare the elements
50 * Returns -ENOMEM if out of memory.
53 extern int bt_heap_init(struct ptr_heap
*heap
,
55 int gt(void *a
, void *b
));
58 * bt_heap_free - free the heap
59 * @heap: the heap to free
62 extern void bt_heap_free(struct ptr_heap
*heap
);
65 * bt_heap_insert - insert an element into the heap
66 * @heap: the heap to be operated on
67 * @p: the element to add
69 * Insert an element into the heap.
71 * Returns -ENOMEM if out of memory.
74 extern int bt_heap_insert(struct ptr_heap
*heap
, void *p
);
77 * bt_heap_remove - remove the largest element from the heap
78 * @heap: the heap to be operated on
80 * Returns the largest element in the heap. It removes this element from the
81 * heap. Returns NULL if the heap is empty.
84 extern void *bt_heap_remove(struct ptr_heap
*heap
);
87 * bt_heap_cherrypick - remove a given element from the heap
88 * @heap: the heap to be operated on
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.
96 extern void *bt_heap_cherrypick(struct ptr_heap
*heap
, void *p
);
99 * bt_heap_replace_max - replace the the largest element from the heap
100 * @heap: the heap to be operated on
101 * @p: the pointer to be inserted as topmost element replacement
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
107 * This is the equivalent of calling bt_heap_remove() and then bt_heap_insert(), but
108 * it only rebalances the heap once. It never allocates memory.
111 extern void *bt_heap_replace_max(struct ptr_heap
*heap
, void *p
);
114 * bt_heap_copy - copy a heap
115 * @dst: the destination heap (must be allocated)
116 * @src: the source heap
118 * Returns -ENOMEM if out of memory.
121 extern int bt_heap_copy(struct ptr_heap
*dst
, struct ptr_heap
*src
);
123 #endif /* _BABELTRACE_PRIO_HEAP_H */
This page took 0.0374409999999999 seconds and 4 git commands to generate.