Implement missing prio heap functions (MIT-licensed)
[lttng-modules.git] / lib / prio_heap / prio_heap.h
1 #ifndef _LTTNG_PRIO_HEAP_H
2 #define _LTTNG_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.
21 */
22
23 #include <linux/gfp.h>
24
25 struct ptr_heap {
26 size_t size, max;
27 void **ptrs;
28 int (*gt)(void *a, void *b);
29 };
30
31 /**
32 * heap_maximum - return the largest element in the heap
33 * @heap: the heap to be operated on
34 *
35 * Returns the largest element in the heap, without performing any modification
36 * to the heap structure. Returns NULL if the heap is empty.
37 */
38 static inline void *heap_maximum(const struct ptr_heap *heap)
39 {
40 return heap->size ? heap->ptrs[0] : NULL;
41 }
42
43 /**
44 * heap_init - initialize the heap
45 * @heap: the heap to initialize
46 * @size: maximum number of elements
47 * @gfp: allocation flags
48 * @gt: function to compare the elements
49 *
50 * Returns -ENOMEM if out of memory.
51 */
52 extern int heap_init(struct ptr_heap *heap, size_t size,
53 gfp_t gfpmask, int gt(void *a, void *b));
54
55 /**
56 * heap_free - free the heap
57 * @heap: the heap to free
58 */
59 extern void heap_free(struct ptr_heap *heap);
60
61 /**
62 * heap_insert - insert an element into the heap
63 * @heap: the heap to be operated on
64 *
65 * Insert an element into the heap. If the heap is full, return the
66 * largest element between those previously present in the heap and the
67 * element being added, else return NULL.
68 */
69 extern void *heap_insert(struct ptr_heap *heap, void *p);
70
71 /**
72 * heap_remove - remove the largest element from the heap
73 * @heap: the heap to be operated on
74 *
75 * Returns the largest element in the heap. It removes this element from the
76 * heap. Returns NULL if the heap is empty.
77 */
78 extern void *heap_remove(struct ptr_heap *heap);
79
80 /**
81 * heap_cherrypick - remove a given element from the heap
82 * @heap: the heap to be operated on
83 * @p: the element
84 *
85 * Remove the given element from the heap. Return the element if present, else
86 * return NULL. This algorithm has a complexity of O(n), which is higher than
87 * O(log(n)) provided by the rest of this API.
88 */
89 extern void *heap_cherrypick(struct ptr_heap *heap, void *p);
90
91 /**
92 * heap_replace_max - replace the the largest element from the heap
93 * @heap: the heap to be operated on
94 * @p: the pointer to be inserted as topmost element replacement
95 *
96 * Returns the largest element in the heap. It removes this element from the
97 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
98 * the heap is empty.
99 *
100 * This is the equivalent of calling heap_remove() and then heap_insert(), but
101 * it only rebalances the heap once.
102 */
103 extern void *heap_replace_max(struct ptr_heap *heap, void *p);
104
105 #endif /* _LTTNG_PRIO_HEAP_H */
This page took 0.03077 seconds and 4 git commands to generate.