* @brief Create an empty queue whose elements are @p itemSize bytes each. * * Allocates a Queue header and an empty backing Vector. No elements * are allocated until you push. * * @param itemSize Size in bytes of one element (must be > 0). * * @return Newly-allocated Queue the caller must free with * @ref queue_deallocate, or NULL on: * - `itemSize == 0` * - Q
| 82 | VectorCompareFunc cmp = q1->vec->compare ? q1->vec->compare : q2->vec->compare; |
| 83 | size_t isz = q1->vec->itemSize; |
| 84 | size_t n1 = q1->vec->size - q1->frontIndex; |
| 85 | size_t n2 = q2->vec->size - q2->frontIndex; |
| 86 | size_t minN = (n1 < n2) ? n1 : n2; |
| 87 | const char* a = (const char*)q1->vec->items + q1->frontIndex * isz; |
| 88 | const char* b = (const char*)q2->vec->items + q2->frontIndex * isz; |
| 89 | |
| 90 | for (size_t i = 0; i < minN; ++i) { |
| 91 | int c = cmp ? cmp(a + i * isz, b + i * isz) : memcmp(a + i * isz, b + i * isz, isz); |
| 92 | if (c != 0) { |
| 93 | return (c < 0) ? -1 : 1; |
| 94 | } |
| 95 | } |
| 96 | if (n1 != n2) { |
| 97 | return (n1 < n2) ? -1 : 1; |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * @brief Sets the element comparator used by the queue_is_* comparisons. |
| 106 | * |
| 107 | * Without one the elements' bytes are compared (memcmp), which is not the |
| 108 | * numeric order of int / double elements. NULL queue is a no-op. |
| 109 | */ |
| 110 | void queue_set_compare(Queue* q, QueueCompareFunc compare) { |
no test coverage detected