* @brief Create an empty priority queue with the given element size * and comparator. * * Allocates a PriorityQueue header and an empty backing Vector. No * elements are allocated until you push. * * @param itemSize Size in bytes of one element (must be > 0). * @param compare Three-way comparator that returns `> 0` when `a` has * higher priority than `b`, `< 0` for
| 200 | * - underlying vector_create failure |
| 201 | */ |
| 202 | PriorityQueue* priority_queue_create(size_t itemSize, PQCompareFunc compare) { |
| 203 | PQUEUE_LOG("[priority_queue_create]: enter itemSize=%zu", itemSize); |
| 204 | |
| 205 | if (itemSize == 0) { |
| 206 | PQUEUE_LOG("[priority_queue_create]: itemSize is zero -> NULL"); |
| 207 | return NULL; |
| 208 | } |
| 209 | if (!compare) { |
| 210 | PQUEUE_LOG("[priority_queue_create]: compare is NULL -> NULL"); |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | PriorityQueue* pq = (PriorityQueue*)malloc(sizeof(PriorityQueue)); |
| 215 | if (!pq) { |
| 216 | PQUEUE_LOG("[priority_queue_create]: malloc(PriorityQueue) failed -> NULL"); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | pq->vec = vector_create(itemSize); |
| 221 | if (!pq->vec) { |
| 222 | PQUEUE_LOG("[priority_queue_create]: vector_create(%zu) failed -> NULL", itemSize); |
| 223 | free(pq); |
| 224 | |
| 225 | return NULL; |
| 226 | } |
| 227 | pq->compare = compare; |
| 228 | |
| 229 | PQUEUE_LOG("[priority_queue_create]: exit ok pq=%p vec=%p", (void*)pq, (void*)pq->vec); |
| 230 | return pq; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /** |
no test coverage detected