2 * Static-sized priority heap containing pointers. Based on CLRS,
5 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include "common/macros.h"
27 #include "common/assert.h"
32 #include "prio-heap.h"
35 void check_heap(const struct ptr_heap
*heap
)
42 for (i
= 1; i
< heap
->len
; i
++)
43 BT_ASSERT_DBG(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
48 size_t parent(size_t i
)
60 size_t right(size_t i
)
66 * Copy of heap->ptrs pointer is invalid after heap_grow.
69 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
73 if (G_LIKELY(heap
->alloc_len
>= new_len
))
76 heap
->alloc_len
= bt_max_t(size_t, new_len
, heap
->alloc_len
<< 1);
77 new_ptrs
= calloc(heap
->alloc_len
, sizeof(void *));
78 if (G_UNLIKELY(!new_ptrs
))
80 if (G_LIKELY(heap
->ptrs
))
81 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
83 heap
->ptrs
= new_ptrs
;
88 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
92 ret
= heap_grow(heap
, new_len
);
99 int bt_heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
100 int gt(void *a
, void *b
))
107 * Minimum size allocated is 1 entry to ensure memory allocation
108 * never fails within bt_heap_replace_max.
110 return heap_grow(heap
, bt_max_t(size_t, 1, alloc_len
));
113 void bt_heap_free(struct ptr_heap
*heap
)
118 static void heapify(struct ptr_heap
*heap
, size_t i
)
120 void **ptrs
= heap
->ptrs
;
121 size_t l
, r
, largest
;
128 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
132 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
134 if (G_UNLIKELY(largest
== i
))
137 ptrs
[i
] = ptrs
[largest
];
144 void *bt_heap_replace_max(struct ptr_heap
*heap
, void *p
)
148 if (G_UNLIKELY(!heap
->len
)) {
149 (void) heap_set_len(heap
, 1);
155 /* Replace the current max and heapify */
162 int bt_heap_insert(struct ptr_heap
*heap
, void *p
)
168 ret
= heap_set_len(heap
, heap
->len
+ 1);
173 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
174 /* Move parent down until we find the right spot */
175 ptrs
[pos
] = ptrs
[parent(pos
)];
183 void *bt_heap_remove(struct ptr_heap
*heap
)
189 (void) heap_set_len(heap
, 0);
190 return heap
->ptrs
[0];
192 /* Shrink, replace the current max by previous last entry and heapify */
193 heap_set_len(heap
, heap
->len
- 1);
194 /* len changed. previous last entry is at heap->len */
195 return bt_heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
198 void *bt_heap_cherrypick(struct ptr_heap
*heap
, void *p
)
200 size_t pos
, len
= heap
->len
;
202 for (pos
= 0; pos
< len
; pos
++)
203 if (G_UNLIKELY(heap
->ptrs
[pos
] == p
))
207 if (G_UNLIKELY(heap
->len
== 1)) {
208 (void) heap_set_len(heap
, 0);
210 return heap
->ptrs
[0];
212 /* Replace p with previous last entry and heapify. */
213 heap_set_len(heap
, heap
->len
- 1);
214 /* len changed. previous last entry is at heap->len */
215 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
220 int bt_heap_copy(struct ptr_heap
*dst
, struct ptr_heap
*src
)
224 ret
= bt_heap_init(dst
, src
->alloc_len
, src
->gt
);
228 ret
= heap_set_len(dst
, src
->len
);
232 memcpy(dst
->ptrs
, src
->ptrs
, src
->len
* sizeof(void *));
This page took 0.034327 seconds and 4 git commands to generate.