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 #include "common/macros.h"
11 #include "common/assert.h"
16 #include "common/prio-heap.h"
19 void check_heap(const struct ptr_heap
*heap
)
26 for (i
= 1; i
< heap
->len
; i
++)
27 BT_ASSERT_DBG(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
32 size_t parent(size_t i
)
44 size_t right(size_t i
)
50 * Copy of heap->ptrs pointer is invalid after heap_grow.
53 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
57 if (G_LIKELY(heap
->alloc_len
>= new_len
))
60 heap
->alloc_len
= bt_max_t(size_t, new_len
, heap
->alloc_len
<< 1);
61 new_ptrs
= calloc(heap
->alloc_len
, sizeof(void *));
62 if (G_UNLIKELY(!new_ptrs
))
64 if (G_LIKELY(heap
->ptrs
))
65 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
67 heap
->ptrs
= new_ptrs
;
72 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
76 ret
= heap_grow(heap
, new_len
);
83 int bt_heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
84 int gt(void *a
, void *b
))
91 * Minimum size allocated is 1 entry to ensure memory allocation
92 * never fails within bt_heap_replace_max.
94 return heap_grow(heap
, bt_max_t(size_t, 1, alloc_len
));
97 void bt_heap_free(struct ptr_heap
*heap
)
102 static void heapify(struct ptr_heap
*heap
, size_t i
)
104 void **ptrs
= heap
->ptrs
;
105 size_t l
, r
, largest
;
112 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
116 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
118 if (G_UNLIKELY(largest
== i
))
121 ptrs
[i
] = ptrs
[largest
];
128 void *bt_heap_replace_max(struct ptr_heap
*heap
, void *p
)
132 if (G_UNLIKELY(!heap
->len
)) {
133 (void) heap_set_len(heap
, 1);
139 /* Replace the current max and heapify */
146 int bt_heap_insert(struct ptr_heap
*heap
, void *p
)
152 ret
= heap_set_len(heap
, heap
->len
+ 1);
157 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
158 /* Move parent down until we find the right spot */
159 ptrs
[pos
] = ptrs
[parent(pos
)];
167 void *bt_heap_remove(struct ptr_heap
*heap
)
173 (void) heap_set_len(heap
, 0);
174 return heap
->ptrs
[0];
176 /* Shrink, replace the current max by previous last entry and heapify */
177 heap_set_len(heap
, heap
->len
- 1);
178 /* len changed. previous last entry is at heap->len */
179 return bt_heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
182 void *bt_heap_cherrypick(struct ptr_heap
*heap
, void *p
)
184 size_t pos
, len
= heap
->len
;
186 for (pos
= 0; pos
< len
; pos
++)
187 if (G_UNLIKELY(heap
->ptrs
[pos
] == p
))
191 if (G_UNLIKELY(heap
->len
== 1)) {
192 (void) heap_set_len(heap
, 0);
194 return heap
->ptrs
[0];
196 /* Replace p with previous last entry and heapify. */
197 heap_set_len(heap
, heap
->len
- 1);
198 /* len changed. previous last entry is at heap->len */
199 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
204 int bt_heap_copy(struct ptr_heap
*dst
, struct ptr_heap
*src
)
208 ret
= bt_heap_init(dst
, src
->alloc_len
, src
->gt
);
212 ret
= heap_set_len(dst
, src
->len
);
216 memcpy(dst
->ptrs
, src
->ptrs
, src
->len
* sizeof(void *));
This page took 0.036387 seconds and 4 git commands to generate.