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>
27 #define max_t(type, a, b) \
28 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
32 void check_heap(const struct ptr_heap
*heap
)
39 for (i
= 1; i
< heap
->len
; i
++)
40 assert(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
45 size_t parent(size_t i
)
57 size_t right(size_t i
)
63 * Copy of heap->ptrs pointer is invalid after heap_grow.
66 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
70 if (heap
->alloc_len
>= new_len
)
73 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
74 new_ptrs
= calloc(heap
->alloc_len
, sizeof(void *));
78 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
80 heap
->ptrs
= new_ptrs
;
85 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
89 ret
= heap_grow(heap
, new_len
);
96 int heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
97 int gt(void *a
, void *b
))
104 * Minimum size allocated is 1 entry to ensure memory allocation
105 * never fails within heap_replace_max.
107 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
110 void heap_free(struct ptr_heap
*heap
)
115 static void heapify(struct ptr_heap
*heap
, size_t i
)
117 void **ptrs
= heap
->ptrs
;
118 size_t l
, r
, largest
;
125 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
129 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
134 ptrs
[i
] = ptrs
[largest
];
141 void *heap_replace_max(struct ptr_heap
*heap
, void *p
)
146 (void) heap_set_len(heap
, 1);
152 /* Replace the current max and heapify */
159 int heap_insert(struct ptr_heap
*heap
, void *p
)
165 ret
= heap_set_len(heap
, heap
->len
+ 1);
170 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
171 /* Move parent down until we find the right spot */
172 ptrs
[pos
] = ptrs
[parent(pos
)];
180 void *heap_remove(struct ptr_heap
*heap
)
186 (void) heap_set_len(heap
, 0);
187 return heap
->ptrs
[0];
189 /* Shrink, replace the current max by previous last entry and heapify */
190 heap_set_len(heap
, heap
->len
- 1);
191 /* len changed. previous last entry is at heap->len */
192 return heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
195 void *heap_cherrypick(struct ptr_heap
*heap
, void *p
)
197 size_t pos
, len
= heap
->len
;
199 for (pos
= 0; pos
< len
; pos
++)
200 if (heap
->ptrs
[pos
] == p
)
204 if (heap
->len
== 1) {
205 (void) heap_set_len(heap
, 0);
207 return heap
->ptrs
[0];
209 /* Replace p with previous last entry and heapify. */
210 heap_set_len(heap
, heap
->len
- 1);
211 /* len changed. previous last entry is at heap->len */
212 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
This page took 0.032556 seconds and 4 git commands to generate.