1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MIN_HEAP_H
3 #define _LINUX_MIN_HEAP_H
6 #include <linux/string.h>
7 #include <linux/types.h>
10 * struct min_heap - Data structure to hold a min-heap.
11 * @data: Start of array holding the heap elements.
12 * @nr: Number of elements currently in the heap.
13 * @size: Maximum number of elements that can be held in current storage.
22 * struct min_heap_callbacks - Data/functions to customise the min_heap.
23 * @elem_size: The nr of each element in bytes.
24 * @less: Partial order function for this heap.
25 * @swp: Swap elements function.
27 struct min_heap_callbacks {
29 bool (*less)(const void *lhs, const void *rhs);
30 void (*swp)(void *lhs, void *rhs);
33 /* Sift the element at pos down the heap. */
34 static __always_inline
35 void min_heapify(struct min_heap *heap, int pos,
36 const struct min_heap_callbacks *func)
39 void *data = heap->data;
40 void *root = data + pos * func->elem_size;
43 /* Find the sift-down path all the way to the leaves. */
45 if (i * 2 + 2 >= heap->nr)
47 left = data + (i * 2 + 1) * func->elem_size;
48 right = data + (i * 2 + 2) * func->elem_size;
49 i = func->less(left, right) ? i * 2 + 1 : i * 2 + 2;
52 /* Special case for the last leaf with no sibling. */
53 if (i * 2 + 2 == heap->nr)
56 /* Backtrack to the correct location. */
57 while (i != pos && func->less(root, data + i * func->elem_size))
60 /* Shift the element into its correct place. */
64 func->swp(data + i * func->elem_size, data + j * func->elem_size);
68 /* Floyd's approach to heapification that is O(nr). */
69 static __always_inline
70 void min_heapify_all(struct min_heap *heap,
71 const struct min_heap_callbacks *func)
75 for (i = heap->nr / 2 - 1; i >= 0; i--)
76 min_heapify(heap, i, func);
79 /* Remove minimum element from the heap, O(log2(nr)). */
80 static __always_inline
81 void min_heap_pop(struct min_heap *heap,
82 const struct min_heap_callbacks *func)
84 void *data = heap->data;
86 if (WARN_ONCE(heap->nr <= 0, "Popping an empty heap"))
89 /* Place last element at the root (position 0) and then sift down. */
91 memcpy(data, data + (heap->nr * func->elem_size), func->elem_size);
92 min_heapify(heap, 0, func);
96 * Remove the minimum element and then push the given element. The
97 * implementation performs 1 sift (O(log2(nr))) and is therefore more
98 * efficient than a pop followed by a push that does 2.
100 static __always_inline
101 void min_heap_pop_push(struct min_heap *heap,
103 const struct min_heap_callbacks *func)
105 memcpy(heap->data, element, func->elem_size);
106 min_heapify(heap, 0, func);
109 /* Push an element on to the heap, O(log2(nr)). */
110 static __always_inline
111 void min_heap_push(struct min_heap *heap, const void *element,
112 const struct min_heap_callbacks *func)
114 void *data = heap->data;
115 void *child, *parent;
118 if (WARN_ONCE(heap->nr >= heap->size, "Pushing on a full heap"))
121 /* Place at the end of data. */
123 memcpy(data + (pos * func->elem_size), element, func->elem_size);
126 /* Sift child at pos up. */
127 for (; pos > 0; pos = (pos - 1) / 2) {
128 child = data + (pos * func->elem_size);
129 parent = data + ((pos - 1) / 2) * func->elem_size;
130 if (func->less(parent, child))
132 func->swp(parent, child);
136 #endif /* _LINUX_MIN_HEAP_H */