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>
21 #include <babeltrace/babeltrace-internal.h>
28 #define max_t(type, a, b) \
29 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
33 void check_heap(const struct ptr_heap
*heap
)
40 for (i
= 1; i
< heap
->len
; i
++)
41 assert(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
46 size_t parent(size_t i
)
58 size_t right(size_t i
)
64 * Copy of heap->ptrs pointer is invalid after heap_grow.
67 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
71 if (likely(heap
->alloc_len
>= new_len
))
74 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
75 new_ptrs
= calloc(heap
->alloc_len
, sizeof(void *));
76 if (unlikely(!new_ptrs
))
78 if (likely(heap
->ptrs
))
79 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
81 heap
->ptrs
= new_ptrs
;
86 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
90 ret
= heap_grow(heap
, new_len
);
97 int heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
98 int gt(void *a
, void *b
))
105 * Minimum size allocated is 1 entry to ensure memory allocation
106 * never fails within heap_replace_max.
108 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
111 void heap_free(struct ptr_heap
*heap
)
116 static void heapify(struct ptr_heap
*heap
, size_t i
)
118 void **ptrs
= heap
->ptrs
;
119 size_t l
, r
, largest
;
126 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
130 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
132 if (unlikely(largest
== i
))
135 ptrs
[i
] = ptrs
[largest
];
142 void *heap_replace_max(struct ptr_heap
*heap
, void *p
)
146 if (unlikely(!heap
->len
)) {
147 (void) heap_set_len(heap
, 1);
153 /* Replace the current max and heapify */
160 int heap_insert(struct ptr_heap
*heap
, void *p
)
166 ret
= heap_set_len(heap
, heap
->len
+ 1);
171 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
172 /* Move parent down until we find the right spot */
173 ptrs
[pos
] = ptrs
[parent(pos
)];
181 void *heap_remove(struct ptr_heap
*heap
)
187 (void) heap_set_len(heap
, 0);
188 return heap
->ptrs
[0];
190 /* Shrink, replace the current max by previous last entry and heapify */
191 heap_set_len(heap
, heap
->len
- 1);
192 /* len changed. previous last entry is at heap->len */
193 return heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
196 void *heap_cherrypick(struct ptr_heap
*heap
, void *p
)
198 size_t pos
, len
= heap
->len
;
200 for (pos
= 0; pos
< len
; pos
++)
201 if (unlikely(heap
->ptrs
[pos
] == p
))
205 if (unlikely(heap
->len
== 1)) {
206 (void) heap_set_len(heap
, 0);
208 return heap
->ptrs
[0];
210 /* Replace p with previous last entry and heapify. */
211 heap_set_len(heap
, heap
->len
- 1);
212 /* len changed. previous last entry is at heap->len */
213 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
This page took 0.032865 seconds and 4 git commands to generate.