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 <babeltrace2/prio-heap-internal.h>
27 #include <babeltrace2/babeltrace-internal.h>
28 #include <babeltrace2/assert-internal.h>
34 void check_heap(const struct ptr_heap
*heap
)
41 for (i
= 1; i
< heap
->len
; i
++)
42 BT_ASSERT(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
47 size_t parent(size_t i
)
59 size_t right(size_t i
)
65 * Copy of heap->ptrs pointer is invalid after heap_grow.
68 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
72 if (likely(heap
->alloc_len
>= new_len
))
75 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
76 new_ptrs
= calloc(heap
->alloc_len
, sizeof(void *));
77 if (unlikely(!new_ptrs
))
79 if (likely(heap
->ptrs
))
80 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
82 heap
->ptrs
= new_ptrs
;
87 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
91 ret
= heap_grow(heap
, new_len
);
98 int bt_heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
99 int gt(void *a
, void *b
))
106 * Minimum size allocated is 1 entry to ensure memory allocation
107 * never fails within bt_heap_replace_max.
109 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
112 void bt_heap_free(struct ptr_heap
*heap
)
117 static void heapify(struct ptr_heap
*heap
, size_t i
)
119 void **ptrs
= heap
->ptrs
;
120 size_t l
, r
, largest
;
127 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
131 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
133 if (unlikely(largest
== i
))
136 ptrs
[i
] = ptrs
[largest
];
143 void *bt_heap_replace_max(struct ptr_heap
*heap
, void *p
)
147 if (unlikely(!heap
->len
)) {
148 (void) heap_set_len(heap
, 1);
154 /* Replace the current max and heapify */
161 int bt_heap_insert(struct ptr_heap
*heap
, void *p
)
167 ret
= heap_set_len(heap
, heap
->len
+ 1);
172 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
173 /* Move parent down until we find the right spot */
174 ptrs
[pos
] = ptrs
[parent(pos
)];
182 void *bt_heap_remove(struct ptr_heap
*heap
)
188 (void) heap_set_len(heap
, 0);
189 return heap
->ptrs
[0];
191 /* Shrink, replace the current max by previous last entry and heapify */
192 heap_set_len(heap
, heap
->len
- 1);
193 /* len changed. previous last entry is at heap->len */
194 return bt_heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
197 void *bt_heap_cherrypick(struct ptr_heap
*heap
, void *p
)
199 size_t pos
, len
= heap
->len
;
201 for (pos
= 0; pos
< len
; pos
++)
202 if (unlikely(heap
->ptrs
[pos
] == p
))
206 if (unlikely(heap
->len
== 1)) {
207 (void) heap_set_len(heap
, 0);
209 return heap
->ptrs
[0];
211 /* Replace p with previous last entry and heapify. */
212 heap_set_len(heap
, heap
->len
- 1);
213 /* len changed. previous last entry is at heap->len */
214 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
219 int bt_heap_copy(struct ptr_heap
*dst
, struct ptr_heap
*src
)
223 ret
= bt_heap_init(dst
, src
->alloc_len
, src
->gt
);
227 ret
= heap_set_len(dst
, src
->len
);
231 memcpy(dst
->ptrs
, src
->ptrs
, src
->len
* sizeof(void *));
This page took 0.034406 seconds and 4 git commands to generate.