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.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include <babeltrace/prio-heap-internal.h>
29 #include <babeltrace/babeltrace-internal.h>
36 void check_heap(const struct ptr_heap
*heap
)
43 for (i
= 1; i
< heap
->len
; i
++)
44 assert(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
49 size_t parent(size_t i
)
61 size_t right(size_t i
)
67 * Copy of heap->ptrs pointer is invalid after heap_grow.
70 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
74 if (likely(heap
->alloc_len
>= new_len
))
77 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
78 new_ptrs
= calloc(heap
->alloc_len
, sizeof(void *));
79 if (unlikely(!new_ptrs
))
81 if (likely(heap
->ptrs
))
82 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
84 heap
->ptrs
= new_ptrs
;
89 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
93 ret
= heap_grow(heap
, new_len
);
100 int bt_heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
101 int gt(void *a
, void *b
))
108 * Minimum size allocated is 1 entry to ensure memory allocation
109 * never fails within bt_heap_replace_max.
111 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
114 void bt_heap_free(struct ptr_heap
*heap
)
119 static void heapify(struct ptr_heap
*heap
, size_t i
)
121 void **ptrs
= heap
->ptrs
;
122 size_t l
, r
, largest
;
129 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
133 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
135 if (unlikely(largest
== i
))
138 ptrs
[i
] = ptrs
[largest
];
145 void *bt_heap_replace_max(struct ptr_heap
*heap
, void *p
)
149 if (unlikely(!heap
->len
)) {
150 (void) heap_set_len(heap
, 1);
156 /* Replace the current max and heapify */
163 int bt_heap_insert(struct ptr_heap
*heap
, void *p
)
169 ret
= heap_set_len(heap
, heap
->len
+ 1);
174 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
175 /* Move parent down until we find the right spot */
176 ptrs
[pos
] = ptrs
[parent(pos
)];
184 void *bt_heap_remove(struct ptr_heap
*heap
)
190 (void) heap_set_len(heap
, 0);
191 return heap
->ptrs
[0];
193 /* Shrink, replace the current max by previous last entry and heapify */
194 heap_set_len(heap
, heap
->len
- 1);
195 /* len changed. previous last entry is at heap->len */
196 return bt_heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
199 void *bt_heap_cherrypick(struct ptr_heap
*heap
, void *p
)
201 size_t pos
, len
= heap
->len
;
203 for (pos
= 0; pos
< len
; pos
++)
204 if (unlikely(heap
->ptrs
[pos
] == p
))
208 if (unlikely(heap
->len
== 1)) {
209 (void) heap_set_len(heap
, 0);
211 return heap
->ptrs
[0];
213 /* Replace p with previous last entry and heapify. */
214 heap_set_len(heap
, heap
->len
- 1);
215 /* len changed. previous last entry is at heap->len */
216 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
221 int bt_heap_copy(struct ptr_heap
*dst
, struct ptr_heap
*src
)
225 ret
= bt_heap_init(dst
, src
->alloc_len
, src
->gt
);
229 ret
= heap_set_len(dst
, src
->len
);
233 memcpy(dst
->ptrs
, src
->ptrs
, src
->len
* sizeof(void *));
This page took 0.044975 seconds and 4 git commands to generate.