e660b0cdc9125ccbc9e99bf83267a9e3540b37b1
4 * Static-sized priority heap containing pointers. Based on CLRS,
7 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
20 #include <linux/slab.h>
21 #include <linux/prio_heap.h>
24 * TODO implement heap_init, heap_free, heap_insert.
27 static void heapify(struct ptr_heap
*heap
, int pos
)
29 void **ptrs
= heap
->ptrs
;
33 int left
= 2 * pos
+ 1;
34 int right
= 2 * pos
+ 2;
36 if (left
< heap
->size
&& heap
->gt(ptrs
[left
], p
))
38 if (right
< heap
->size
&& heap
->gt(ptrs
[right
], ptrs
[largest
]))
42 /* Push p down the heap one level and bump one up */
43 ptrs
[pos
] = ptrs
[largest
];
49 void *heap_replace_max(struct ptr_heap
*heap
, void *p
)
52 void **ptrs
= heap
->ptrs
;
60 /* Replace the current max and heapify */
67 void *heap_remove(struct ptr_heap
*heap
)
69 void **ptrs
= heap
->ptrs
;
79 /* Shrink, replace the current max by previous last entry and heapify */
80 return heap_replace_max(heap
, ptrs
[--heap
->size
]);
83 void *heap_cherrypick(struct ptr_heap
*heap
, void *p
)
85 void **ptrs
= heap
->ptrs
;
86 size_t pos
, size
= heap
->size
;
88 for (pos
= 0; pos
< size
; pos
++)
93 if (heap
->size
== 1) {
98 * Replace p with previous last entry and heapify.
100 ptrs
[pos
] = ptrs
[--heap
->size
];
This page took 0.031217 seconds and 3 git commands to generate.