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.h>
29 #include <babeltrace/babeltrace-internal.h>
36 #define max_t(type, a, b) \
37 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
41 void check_heap(const struct ptr_heap
*heap
)
48 for (i
= 1; i
< heap
->len
; i
++)
49 assert(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
54 size_t parent(size_t i
)
66 size_t right(size_t i
)
72 * Copy of heap->ptrs pointer is invalid after heap_grow.
75 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
79 if (likely(heap
->alloc_len
>= new_len
))
82 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
83 new_ptrs
= calloc(heap
->alloc_len
, sizeof(void *));
84 if (unlikely(!new_ptrs
))
86 if (likely(heap
->ptrs
))
87 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
89 heap
->ptrs
= new_ptrs
;
94 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
98 ret
= heap_grow(heap
, new_len
);
105 int bt_heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
106 int gt(void *a
, void *b
))
113 * Minimum size allocated is 1 entry to ensure memory allocation
114 * never fails within bt_heap_replace_max.
116 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
119 void bt_heap_free(struct ptr_heap
*heap
)
124 static void heapify(struct ptr_heap
*heap
, size_t i
)
126 void **ptrs
= heap
->ptrs
;
127 size_t l
, r
, largest
;
134 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
138 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
140 if (unlikely(largest
== i
))
143 ptrs
[i
] = ptrs
[largest
];
150 void *bt_heap_replace_max(struct ptr_heap
*heap
, void *p
)
154 if (unlikely(!heap
->len
)) {
155 (void) heap_set_len(heap
, 1);
161 /* Replace the current max and heapify */
168 int bt_heap_insert(struct ptr_heap
*heap
, void *p
)
174 ret
= heap_set_len(heap
, heap
->len
+ 1);
179 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
180 /* Move parent down until we find the right spot */
181 ptrs
[pos
] = ptrs
[parent(pos
)];
189 void *bt_heap_remove(struct ptr_heap
*heap
)
195 (void) heap_set_len(heap
, 0);
196 return heap
->ptrs
[0];
198 /* Shrink, replace the current max by previous last entry and heapify */
199 heap_set_len(heap
, heap
->len
- 1);
200 /* len changed. previous last entry is at heap->len */
201 return bt_heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
204 void *bt_heap_cherrypick(struct ptr_heap
*heap
, void *p
)
206 size_t pos
, len
= heap
->len
;
208 for (pos
= 0; pos
< len
; pos
++)
209 if (unlikely(heap
->ptrs
[pos
] == p
))
213 if (unlikely(heap
->len
== 1)) {
214 (void) heap_set_len(heap
, 0);
216 return heap
->ptrs
[0];
218 /* Replace p with previous last entry and heapify. */
219 heap_set_len(heap
, heap
->len
- 1);
220 /* len changed. previous last entry is at heap->len */
221 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
226 int bt_heap_copy(struct ptr_heap
*dst
, struct ptr_heap
*src
)
230 ret
= bt_heap_init(dst
, src
->alloc_len
, src
->gt
);
234 ret
= heap_set_len(dst
, src
->len
);
238 memcpy(dst
->ptrs
, src
->ptrs
, src
->len
* sizeof(void *));
This page took 0.038395 seconds and 5 git commands to generate.