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.
20 #include <babeltrace/prio_heap.h>
26 #define max_t(type, a, b) \
27 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
30 static __attribute__((unused
))
31 size_t parent(size_t i
)
43 size_t right(size_t i
)
49 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
53 if (heap
->alloc_len
>= new_len
)
56 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
57 new_ptrs
= calloc(heap
->alloc_len
, sizeof(void *));
61 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
63 heap
->ptrs
= new_ptrs
;
68 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
72 ret
= heap_grow(heap
, new_len
);
79 int heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
80 int gt(void *a
, void *b
))
87 * Minimum size allocated is 1 entry to ensure memory allocation
88 * never fails within heap_replace_max.
90 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
93 void heap_free(struct ptr_heap
*heap
)
98 static void heapify(struct ptr_heap
*heap
, size_t i
)
100 void **ptrs
= heap
->ptrs
;
101 size_t l
, r
, largest
;
106 if (l
<= heap
->len
&& ptrs
[l
] > ptrs
[i
])
110 if (r
<= heap
->len
&& ptrs
[r
] > ptrs
[largest
])
116 ptrs
[i
] = ptrs
[largest
];
126 void *heap_replace_max(struct ptr_heap
*heap
, void *p
)
129 void **ptrs
= heap
->ptrs
;
132 (void) heap_set_len(heap
, 1);
137 /* Replace the current max and heapify */
144 int heap_insert(struct ptr_heap
*heap
, void *p
)
146 void **ptrs
= heap
->ptrs
;
149 ret
= heap_set_len(heap
, heap
->len
+ 1);
152 /* Add the element to the end */
153 ptrs
[heap
->len
- 1] = p
;
159 void *heap_remove(struct ptr_heap
*heap
)
161 void **ptrs
= heap
->ptrs
;
167 (void) heap_set_len(heap
, 0);
170 /* Shrink, replace the current max by previous last entry and heapify */
171 heap_set_len(heap
, heap
->len
- 1);
172 return heap_replace_max(heap
, ptrs
[heap
->len
- 1]);
175 void *heap_cherrypick(struct ptr_heap
*heap
, void *p
)
177 void **ptrs
= heap
->ptrs
;
178 size_t pos
, len
= heap
->len
;
180 for (pos
= 0; pos
< len
; pos
++)
185 if (heap
->len
== 1) {
186 (void) heap_set_len(heap
, 0);
189 /* Replace p with previous last entry and heapify. */
190 heap_set_len(heap
, heap
->len
- 1);
191 ptrs
[pos
] = ptrs
[heap
->len
- 1];
This page took 0.03201 seconds and 4 git commands to generate.