1 #ifndef _BABELTRACE_PRIO_HEAP_H
2 #define _BABELTRACE_PRIO_HEAP_H
7 * Static-sized priority heap containing pointers. Based on CLRS,
10 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
24 #include <babeltrace/babeltrace-internal.h>
27 size_t len
, alloc_len
;
29 int (*gt
)(void *a
, void *b
);
33 void check_heap(const struct ptr_heap
*heap
);
36 void check_heap(const struct ptr_heap
*heap
)
42 * heap_maximum - return the largest element in the heap
43 * @heap: the heap to be operated on
45 * Returns the largest element in the heap, without performing any modification
46 * to the heap structure. Returns NULL if the heap is empty.
48 static inline void *heap_maximum(const struct ptr_heap
*heap
)
51 return likely(heap
->len
) ? heap
->ptrs
[0] : NULL
;
55 * heap_init - initialize the heap
56 * @heap: the heap to initialize
57 * @alloc_len: number of elements initially allocated
58 * @gt: function to compare the elements
60 * Returns -ENOMEM if out of memory.
62 extern int heap_init(struct ptr_heap
*heap
,
64 int gt(void *a
, void *b
));
67 * heap_free - free the heap
68 * @heap: the heap to free
70 extern void heap_free(struct ptr_heap
*heap
);
73 * heap_insert - insert an element into the heap
74 * @heap: the heap to be operated on
75 * @p: the element to add
77 * Insert an element into the heap.
79 * Returns -ENOMEM if out of memory.
81 extern int heap_insert(struct ptr_heap
*heap
, void *p
);
84 * heap_remove - remove the largest element from the heap
85 * @heap: the heap to be operated on
87 * Returns the largest element in the heap. It removes this element from the
88 * heap. Returns NULL if the heap is empty.
90 extern void *heap_remove(struct ptr_heap
*heap
);
93 * heap_cherrypick - remove a given element from the heap
94 * @heap: the heap to be operated on
97 * Remove the given element from the heap. Return the element if present, else
98 * return NULL. This algorithm has a complexity of O(n), which is higher than
99 * O(log(n)) provided by the rest of this API.
101 extern void *heap_cherrypick(struct ptr_heap
*heap
, void *p
);
104 * heap_replace_max - replace the the largest element from the heap
105 * @heap: the heap to be operated on
106 * @p: the pointer to be inserted as topmost element replacement
108 * Returns the largest element in the heap. It removes this element from the
109 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
112 * This is the equivalent of calling heap_remove() and then heap_insert(), but
113 * it only rebalances the heap once. It never allocates memory.
115 extern void *heap_replace_max(struct ptr_heap
*heap
, void *p
);
118 * heap_copy - copy a heap
119 * @dst: the destination heap (must be allocated)
120 * @src: the source heap
122 * Returns -ENOMEM if out of memory.
124 extern int heap_copy(struct ptr_heap
*dst
, struct ptr_heap
*src
);
126 #endif /* _BABELTRACE_PRIO_HEAP_H */
This page took 0.031921 seconds and 4 git commands to generate.