LCOV - code coverage report
Current view: top level - source/specialized/priority_queue.c (source / functions) Coverage Total Hit
Test: CCC Test Suite Coverage Report Lines: 97.1 % 307 298
Test Date: 2026-12-03 13:34:35 Functions: 100.0 % 33 33

            Line data    Source code
       1              : /** Copyright 2025 Alexander G. Lopez
       2              : 
       3              : Licensed under the Apache License, Version 2.0 (the "License");
       4              : you may not use this file except in compliance with the License.
       5              : You may obtain a copy of the License at
       6              : 
       7              :    http://www.apache.org/licenses/LICENSE-2.0
       8              : 
       9              : Unless required by applicable law or agreed to in writing, software
      10              : distributed under the License is distributed on an "AS IS" BASIS,
      11              : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12              : See the License for the specific language governing permissions and
      13              : limitations under the License. */
      14              : /** C23 provided headers. */
      15              : #include <stddef.h>
      16              : 
      17              : /** CCC provided headers. */
      18              : #include "ccc/configuration.h" /* IWYU pragma: keep */
      19              : #include "ccc/specialized/priority_queue.h"
      20              : #include "ccc/specialized/private/private_priority_queue.h"
      21              : #include "ccc/types.h"
      22              : 
      23              : /*=========================  Function Prototypes   ==========================*/
      24              : 
      25              : static struct CCC_Priority_queue_node *
      26              : elem_in(struct CCC_Priority_queue const *, void const *);
      27              : static struct CCC_Priority_queue_node *merge(
      28              :     struct CCC_Priority_queue *,
      29              :     struct CCC_Priority_queue_node *,
      30              :     struct CCC_Priority_queue_node *
      31              : );
      32              : static void
      33              : link_child(struct CCC_Priority_queue_node *, struct CCC_Priority_queue_node *);
      34              : static void init_node(struct CCC_Priority_queue_node *);
      35              : static size_t traversal_count(struct CCC_Priority_queue_node const *);
      36              : static CCC_Tribool has_valid_links(
      37              :     struct CCC_Priority_queue const *,
      38              :     struct CCC_Priority_queue_node const *,
      39              :     struct CCC_Priority_queue_node const *
      40              : );
      41              : static struct CCC_Priority_queue_node *
      42              : delete_node(struct CCC_Priority_queue *, struct CCC_Priority_queue_node *);
      43              : static struct CCC_Priority_queue_node *
      44              : delete_root(struct CCC_Priority_queue *, struct CCC_Priority_queue_node *);
      45              : static void clear_node(struct CCC_Priority_queue_node *);
      46              : static void cut_child(struct CCC_Priority_queue_node *);
      47              : static void *struct_base(
      48              :     struct CCC_Priority_queue const *, struct CCC_Priority_queue_node const *
      49              : );
      50              : static CCC_Order order(
      51              :     struct CCC_Priority_queue const *,
      52              :     struct CCC_Priority_queue_node const *,
      53              :     struct CCC_Priority_queue_node const *
      54              : );
      55              : static void
      56              : update_fixup(struct CCC_Priority_queue *, struct CCC_Priority_queue_node *);
      57              : static void
      58              : increase_fixup(struct CCC_Priority_queue *, struct CCC_Priority_queue_node *);
      59              : static void
      60              : decrease_fixup(struct CCC_Priority_queue *, struct CCC_Priority_queue_node *);
      61              : 
      62              : /*=========================  Interface Functions   ==========================*/
      63              : 
      64              : void *
      65          614 : CCC_priority_queue_front(CCC_Priority_queue const *const priority_queue) {
      66          614 :     if (!priority_queue) {
      67            1 :         return NULL;
      68              :     }
      69          613 :     return priority_queue->root
      70          613 :              ? struct_base(priority_queue, priority_queue->root)
      71              :              : NULL;
      72          614 : }
      73              : 
      74              : void *
      75         2884 : CCC_priority_queue_push(
      76              :     CCC_Priority_queue *const priority_queue,
      77              :     CCC_Priority_queue_node *type_intruder,
      78              :     CCC_Allocator const *const allocator
      79              : ) {
      80         2884 :     if (!type_intruder || !priority_queue || !allocator) {
      81            3 :         return NULL;
      82              :     }
      83         2881 :     void *ret = struct_base(priority_queue, type_intruder);
      84         2881 :     if (allocator->allocate) {
      85        11476 :         void *const node = allocator->allocate((CCC_Allocator_arguments){
      86              :             .input = NULL,
      87         2869 :             .bytes = priority_queue->sizeof_type,
      88         2869 :             .alignment = priority_queue->alignof_type,
      89         2869 :             .context = allocator->context,
      90              :         });
      91         2869 :         if (!node) {
      92            1 :             return NULL;
      93              :         }
      94         2868 :         (void)memcpy(node, ret, priority_queue->sizeof_type);
      95         2868 :         ret = node;
      96         2868 :         type_intruder = elem_in(priority_queue, ret);
      97         2869 :     }
      98         2880 :     init_node(type_intruder);
      99         2880 :     priority_queue->root
     100         5760 :         = merge(priority_queue, priority_queue->root, type_intruder);
     101         2880 :     ++priority_queue->count;
     102         2880 :     return ret;
     103         2884 : }
     104              : 
     105              : CCC_Result
     106          706 : CCC_priority_queue_pop(
     107              :     CCC_Priority_queue *const priority_queue,
     108              :     CCC_Allocator const *const allocator
     109              : ) {
     110          706 :     if (!priority_queue || !priority_queue->root || !allocator) {
     111            2 :         return CCC_RESULT_ARGUMENT_ERROR;
     112              :     }
     113          704 :     struct CCC_Priority_queue_node *const popped = priority_queue->root;
     114          704 :     priority_queue->root = delete_root(priority_queue, priority_queue->root);
     115          704 :     priority_queue->count--;
     116          704 :     clear_node(popped);
     117          704 :     if (allocator->allocate) {
     118         2816 :         (void)allocator->allocate((CCC_Allocator_arguments){
     119          704 :             .input = struct_base(priority_queue, popped),
     120              :             .bytes = 0,
     121          704 :             .alignment = priority_queue->alignof_type,
     122          704 :             .context = allocator->context,
     123              :         });
     124          704 :     }
     125          704 :     return CCC_RESULT_OK;
     126          706 : }
     127              : 
     128              : void *
     129         1224 : CCC_priority_queue_extract(
     130              :     CCC_Priority_queue *const priority_queue,
     131              :     CCC_Priority_queue_node *const type_intruder
     132              : ) {
     133         1224 :     if (!priority_queue || !type_intruder || !priority_queue->root
     134         1223 :         || !type_intruder->next || !type_intruder->prev) {
     135            1 :         return NULL;
     136              :     }
     137         1223 :     priority_queue->root = delete_node(priority_queue, type_intruder);
     138         1223 :     priority_queue->count--;
     139         1223 :     clear_node(type_intruder);
     140         1223 :     return struct_base(priority_queue, type_intruder);
     141         1224 : }
     142              : 
     143              : CCC_Result
     144          102 : CCC_priority_queue_erase(
     145              :     CCC_Priority_queue *const priority_queue,
     146              :     CCC_Priority_queue_node *const type_intruder,
     147              :     CCC_Allocator const *const allocator
     148              : ) {
     149          102 :     if (!priority_queue || !type_intruder || !priority_queue->root || !allocator
     150          100 :         || !type_intruder->next || !type_intruder->prev) {
     151            2 :         return CCC_RESULT_ARGUMENT_ERROR;
     152              :     }
     153          100 :     priority_queue->root = delete_node(priority_queue, type_intruder);
     154          100 :     priority_queue->count--;
     155          100 :     clear_node(type_intruder);
     156          100 :     if (allocator->allocate) {
     157          400 :         (void)allocator->allocate((CCC_Allocator_arguments){
     158          100 :             .input = struct_base(priority_queue, type_intruder),
     159              :             .bytes = 0,
     160          100 :             .alignment = priority_queue->alignof_type,
     161          100 :             .context = allocator->context,
     162              :         });
     163          100 :     }
     164          100 :     return CCC_RESULT_OK;
     165          102 : }
     166              : 
     167              : /** Deletes all nodes in the heap in linear time and constant space. This is
     168              : achieved by continually bringing up any child lists and splicing them into the
     169              : current child list being considered. We are avoiding recursion or amortized
     170              : O(log(N)) pops with this method. */
     171              : CCC_Result
     172            7 : CCC_priority_queue_clear(
     173              :     CCC_Priority_queue *const priority_queue,
     174              :     CCC_Destructor const *const destructor,
     175              :     CCC_Allocator const *const allocator
     176              : ) {
     177            7 :     if (!priority_queue || !destructor || !allocator) {
     178            3 :         return CCC_RESULT_ARGUMENT_ERROR;
     179              :     }
     180            4 :     struct CCC_Priority_queue_node *node = priority_queue->root;
     181          166 :     while (node) {
     182              :         /* The child and its siblings cut to the front of the line and we
     183              :            start again as if the child is the first in this sibling list. */
     184          162 :         if (node->child) {
     185            9 :             struct CCC_Priority_queue_node *const child = node->child;
     186            9 :             struct CCC_Priority_queue_node *const node_end = node->next;
     187              :             /* Final element of e child list pick up child as head */
     188            9 :             node_end->prev = child;
     189              :             /* Now e picks up the last (wrapping) element of child list. */
     190            9 :             node->next = child->next;
     191              :             /* Child has a list so don't just set child's prev to e. */
     192            9 :             child->next->prev = node;
     193              :             /* Child list wrapping element is now end of e list. */
     194            9 :             child->next = node_end;
     195              :             /* Our traversal now jumps to start of list we spliced in. */
     196            9 :             node->child = NULL;
     197            9 :             node = child;
     198              :             continue;
     199            9 :         }
     200              :         /* No more child lists to splice in so this node is done.  */
     201          306 :         struct CCC_Priority_queue_node *const prev_node
     202          153 :             = node->prev == node ? NULL : node->prev;
     203          153 :         node->next->prev = node->prev;
     204          153 :         node->prev->next = node->next;
     205          153 :         node->parent = node->next = node->prev = node->child = NULL;
     206          153 :         void *const destroy_this = struct_base(priority_queue, node);
     207          153 :         if (destructor->destroy) {
     208          150 :             destructor->destroy((CCC_Arguments){
     209           50 :                 .type = destroy_this,
     210           50 :                 .context = destructor->context,
     211              :             });
     212           50 :         }
     213          153 :         if (allocator->allocate) {
     214          612 :             (void)allocator->allocate((CCC_Allocator_arguments){
     215          153 :                 .input = destroy_this,
     216              :                 .bytes = 0,
     217          153 :                 .alignment = priority_queue->alignof_type,
     218          153 :                 .context = allocator->context,
     219              :             });
     220          153 :         }
     221          153 :         node = prev_node;
     222          153 :     }
     223            4 :     priority_queue->count = 0;
     224            4 :     priority_queue->root = NULL;
     225            4 :     return CCC_RESULT_OK;
     226            7 : }
     227              : 
     228              : CCC_Tribool
     229          618 : CCC_priority_queue_is_empty(CCC_Priority_queue const *const priority_queue) {
     230          618 :     if (!priority_queue) {
     231            1 :         return CCC_TRIBOOL_ERROR;
     232              :     }
     233          617 :     return !priority_queue->count;
     234          618 : }
     235              : 
     236              : CCC_Count
     237          548 : CCC_priority_queue_count(CCC_Priority_queue const *const priority_queue) {
     238          548 :     if (!priority_queue) {
     239            1 :         return (CCC_Count){.error = CCC_RESULT_ARGUMENT_ERROR};
     240              :     }
     241          547 :     return (CCC_Count){.count = priority_queue->count};
     242          548 : }
     243              : 
     244              : /** This is a difficult function. Without knowing if this new value is greater
     245              : or less than the previous we must always perform a delete and reinsert if the
     246              : value has not broken total order with the parent. It is not sufficient to check
     247              : if the value has exceeded the value of the first left child as any sibling of
     248              : that left child may be bigger than or smaller than that left child value. */
     249              : void *
     250           79 : CCC_priority_queue_update(
     251              :     CCC_Priority_queue *const priority_queue,
     252              :     CCC_Priority_queue_node *const type_intruder,
     253              :     CCC_Modifier const *const modifier
     254              : ) {
     255           79 :     if (!priority_queue || !type_intruder || !modifier || !modifier->modify
     256           76 :         || !type_intruder->next || !type_intruder->prev) {
     257            3 :         return NULL;
     258              :     }
     259          228 :     modifier->modify((CCC_Arguments){
     260           76 :         .type = struct_base(priority_queue, type_intruder),
     261           76 :         .context = modifier->context,
     262              :     });
     263           76 :     update_fixup(priority_queue, type_intruder);
     264           76 :     return struct_base(priority_queue, type_intruder);
     265           79 : }
     266              : 
     267              : /* Preferable to use this function if it is known the value is increasing.
     268              :    Much more efficient. */
     269              : void *
     270           59 : CCC_priority_queue_increase(
     271              :     CCC_Priority_queue *const priority_queue,
     272              :     CCC_Priority_queue_node *const type_intruder,
     273              :     CCC_Modifier const *const modifier
     274              : ) {
     275           59 :     if (!priority_queue || !type_intruder || !modifier || !modifier->modify
     276           56 :         || !type_intruder->next || !type_intruder->prev) {
     277            3 :         return NULL;
     278              :     }
     279          168 :     modifier->modify((CCC_Arguments){
     280           56 :         .type = struct_base(priority_queue, type_intruder),
     281           56 :         .context = modifier->context,
     282              :     });
     283           56 :     increase_fixup(priority_queue, type_intruder);
     284           56 :     return struct_base(priority_queue, type_intruder);
     285           59 : }
     286              : 
     287              : /* Preferable to use this function if it is known the value is decreasing.
     288              :    Much more efficient. */
     289              : void *
     290          152 : CCC_priority_queue_decrease(
     291              :     CCC_Priority_queue *const priority_queue,
     292              :     CCC_Priority_queue_node *const type_intruder,
     293              :     CCC_Modifier const *const modifier
     294              : ) {
     295          152 :     if (!priority_queue || !type_intruder || !modifier || !modifier->modify
     296          149 :         || !type_intruder->next || !type_intruder->prev) {
     297            3 :         return NULL;
     298              :     }
     299          447 :     modifier->modify((CCC_Arguments){
     300          149 :         .type = struct_base(priority_queue, type_intruder),
     301          149 :         .context = modifier->context,
     302              :     });
     303          149 :     decrease_fixup(priority_queue, type_intruder);
     304          149 :     return struct_base(priority_queue, type_intruder);
     305          152 : }
     306              : 
     307              : CCC_Tribool
     308         5303 : CCC_priority_queue_validate(CCC_Priority_queue const *const priority_queue) {
     309         5303 :     if (!priority_queue
     310         5303 :         || (priority_queue->root && priority_queue->root->parent)) {
     311            0 :         return CCC_FALSE;
     312              :     }
     313         5303 :     if (!has_valid_links(priority_queue, NULL, priority_queue->root)) {
     314            0 :         return CCC_FALSE;
     315              :     }
     316         5303 :     if (traversal_count(priority_queue->root) != priority_queue->count) {
     317            0 :         return CCC_FALSE;
     318              :     }
     319         5303 :     return CCC_TRUE;
     320         5303 : }
     321              : 
     322              : CCC_Order
     323            5 : CCC_priority_queue_order(CCC_Priority_queue const *const priority_queue) {
     324            5 :     return priority_queue ? priority_queue->order : CCC_ORDER_ERROR;
     325              : }
     326              : 
     327              : /*=========================  Private Interface     ==========================*/
     328              : 
     329              : void
     330            3 : CCC_private_priority_queue_push(
     331              :     struct CCC_Priority_queue *const priority_queue,
     332              :     struct CCC_Priority_queue_node *const node
     333              : ) {
     334            3 :     init_node(node);
     335            3 :     priority_queue->root = merge(priority_queue, priority_queue->root, node);
     336            3 :     ++priority_queue->count;
     337            3 : }
     338              : 
     339              : struct CCC_Priority_queue_node *
     340          276 : CCC_private_priority_queue_node_in(
     341              :     struct CCC_Priority_queue const *const priority_queue,
     342              :     void const *const any_struct
     343              : ) {
     344          276 :     return elem_in(priority_queue, any_struct);
     345              : }
     346              : 
     347              : void
     348           76 : CCC_private_priority_queue_update_fixup(
     349              :     struct CCC_Priority_queue *const pq,
     350              :     struct CCC_Priority_queue_node *const node
     351              : ) {
     352           76 :     return update_fixup(pq, node);
     353           76 : }
     354              : 
     355              : void
     356           43 : CCC_private_priority_queue_increase_fixup(
     357              :     struct CCC_Priority_queue *const pq,
     358              :     struct CCC_Priority_queue_node *const node
     359              : ) {
     360           43 :     return increase_fixup(pq, node);
     361           43 : }
     362              : 
     363              : void
     364          154 : CCC_private_priority_queue_decrease_fixup(
     365              :     struct CCC_Priority_queue *const pq,
     366              :     struct CCC_Priority_queue_node *const node
     367              : ) {
     368          154 :     return decrease_fixup(pq, node);
     369          154 : }
     370              : 
     371              : /*========================   Static Helpers  ================================*/
     372              : 
     373              : static void
     374          152 : update_fixup(
     375              :     struct CCC_Priority_queue *const priority_queue,
     376              :     struct CCC_Priority_queue_node *const node
     377              : ) {
     378              :     /* We could get lucky with a fast path but otherwise there is no way to
     379              :        know whether this is an increase or decrease and by how much. */
     380          152 :     if (node->parent
     381          152 :         && order(priority_queue, node, node->parent) == priority_queue->order) {
     382            1 :         cut_child(node);
     383            1 :     } else {
     384          151 :         priority_queue->root = delete_node(priority_queue, node);
     385          151 :         init_node(node);
     386              :     }
     387          152 :     priority_queue->root = merge(priority_queue, priority_queue->root, node);
     388          152 : }
     389              : 
     390              : static void
     391           99 : increase_fixup(
     392              :     struct CCC_Priority_queue *const priority_queue,
     393              :     struct CCC_Priority_queue_node *const node
     394              : ) {
     395           99 :     if (priority_queue->order == CCC_ORDER_GREATER
     396           99 :         && node == priority_queue->root) {
     397            1 :         return;
     398              :     }
     399           98 :     if (priority_queue->order == CCC_ORDER_GREATER) {
     400            1 :         cut_child(node);
     401            1 :     } else {
     402           97 :         priority_queue->root = delete_node(priority_queue, node);
     403           97 :         init_node(node);
     404              :     }
     405           98 :     priority_queue->root = merge(priority_queue, priority_queue->root, node);
     406          197 : }
     407              : 
     408              : static void
     409          303 : decrease_fixup(
     410              :     struct CCC_Priority_queue *const priority_queue,
     411              :     struct CCC_Priority_queue_node *const node
     412              : ) {
     413          303 :     if (priority_queue->order == CCC_ORDER_LESSER
     414          303 :         && node == priority_queue->root) {
     415            1 :         return;
     416              :     }
     417          302 :     if (priority_queue->order == CCC_ORDER_LESSER) {
     418          301 :         cut_child(node);
     419          301 :     } else {
     420            1 :         priority_queue->root = delete_node(priority_queue, node);
     421            1 :         init_node(node);
     422              :     }
     423          302 :     priority_queue->root = merge(priority_queue, priority_queue->root, node);
     424          605 : }
     425              : 
     426              : /** Cuts the child out of its current sibling list and redirects parent if
     427              : this child is directly pointed to by parent. The child is then made into its
     428              : own circular sibling list. The left child of this child, if one exists, is
     429              : still pointed to and not modified by this function. */
     430              : static void
     431         1745 : cut_child(struct CCC_Priority_queue_node *const child) {
     432         1745 :     child->next->prev = child->prev;
     433         1745 :     child->prev->next = child->next;
     434         1745 :     if (child->parent && child == child->parent->child) {
     435              :         /* To preserve the shuffle down properties the prev child should
     436              :            become the new child as that is the next youngest node. */
     437         1031 :         child->parent->child = child->prev == child ? NULL : child->prev;
     438         1031 :     }
     439         1745 :     child->parent = NULL;
     440         1745 :     child->next = child->prev = child;
     441         1745 : }
     442              : 
     443              : static struct CCC_Priority_queue_node *
     444         1572 : delete_node(
     445              :     struct CCC_Priority_queue *const priority_queue,
     446              :     struct CCC_Priority_queue_node *const root
     447              : ) {
     448         1572 :     if (priority_queue->root == root) {
     449          130 :         return delete_root(priority_queue, root);
     450              :     }
     451         1442 :     cut_child(root);
     452         1442 :     return merge(
     453         1442 :         priority_queue, priority_queue->root, delete_root(priority_queue, root)
     454              :     );
     455         1572 : }
     456              : 
     457              : /* Uses Fredman et al. oldest to youngest pairing method mentioned on pg 124
     458              : of the paper to pair nodes in one pass. Of all the variants for pairing given
     459              : in the paper this one is the back-to-front variant and the only one for which
     460              : the runtime analysis holds identically to the two-pass standard variant. A
     461              : non-trivial example for a heap.
     462              : 
     463              : < = next_sibling
     464              : > = prev_sibling
     465              : 
     466              :   ┌<1>┐
     467              :   └/──┘
     468              : ┌<9>─<1>─<9>─<7>─<8>┐
     469              : └───────────────────┘
     470              :         |
     471              :         v
     472              : ┌<9>─<1>─<7>─<8>┐
     473              : └────────/──────┘
     474              :       ┌<9>┐
     475              :       └───┘
     476              :         |
     477              :         v
     478              : ┌<9>─<1>─<7>┐
     479              : └────────/──┘
     480              :       ┌<8>─<9>┐
     481              :       └───────┘
     482              :         |
     483              :         v
     484              :   ┌<1>─<7>┐
     485              :   └/────/─┘
     486              : ┌<9>┐┌<8>─<9>┐
     487              : └───┘└───────┘
     488              :         |
     489              :         v
     490              :     ┌<1>┐
     491              :     └/──┘
     492              :   ┌<7>─<9>┐
     493              :   └/──────┘
     494              : ┌<8>─<9>┐
     495              : └───────┘
     496              : 
     497              : Delete root is the slowest operation offered by the priority queue and in
     498              : part contributes to the amortized `o(log(N))` runtime of the decrease key
     499              : operation. */
     500              : static struct CCC_Priority_queue_node *
     501         2276 : delete_root(
     502              :     struct CCC_Priority_queue *const priority_queue,
     503              :     struct CCC_Priority_queue_node *root
     504              : ) {
     505         2276 :     if (!root->child) {
     506          959 :         return NULL;
     507              :     }
     508         1317 :     struct CCC_Priority_queue_node *const eldest = root->child->next;
     509         1317 :     struct CCC_Priority_queue_node *accumulator = root->child->next;
     510         1317 :     struct CCC_Priority_queue_node *cur = root->child->next->next;
     511         4139 :     while (cur != eldest && cur->next != eldest) {
     512         2822 :         struct CCC_Priority_queue_node *const next = cur->next;
     513         2822 :         struct CCC_Priority_queue_node *const next_cur = cur->next->next;
     514         2822 :         next->next = next->prev = NULL;
     515         2822 :         cur->next = cur->prev = NULL;
     516              :         /* Double merge ensures `O(log(N))` steps rather than O(N). */
     517         2822 :         accumulator = merge(
     518         2822 :             priority_queue, accumulator, merge(priority_queue, cur, next)
     519              :         );
     520         2822 :         cur = next_cur;
     521         2822 :     }
     522              :     /* This covers the odd or even case for number of pairings. */
     523              :     root
     524         1317 :         = cur == eldest ? accumulator : merge(priority_queue, accumulator, cur);
     525              :     /* The root is always alone in its circular list at the end of merges. */
     526         1317 :     root->next = root->prev = root;
     527         1317 :     root->parent = NULL;
     528         1317 :     return root;
     529         2276 : }
     530              : 
     531              : /** Merges two priority queues, making the winner by ordering the root and
     532              : pushing the loser to the left child ring. Old should be the element that has
     533              : been in the queue longer and new, newer. This algorithm will still work if this
     534              : argument ordering is not respected and it does not change runtime, but it is how
     535              : to comply with the strategy outlined in the Fredman et. al. paper. */
     536              : static struct CCC_Priority_queue_node *
     537        10980 : merge(
     538              :     struct CCC_Priority_queue *const priority_queue,
     539              :     struct CCC_Priority_queue_node *const old,
     540              :     struct CCC_Priority_queue_node *const new
     541              : ) {
     542        10980 :     if (!old || !new || old == new) {
     543          978 :         return old ? old : new;
     544              :     }
     545        10002 :     if (order(priority_queue, new, old) == priority_queue->order) {
     546         1765 :         link_child(new, old);
     547         1765 :         return new;
     548              :     }
     549         8237 :     link_child(old, new);
     550         8237 :     return old;
     551        10980 : }
     552              : 
     553              : /* Oldest nodes shuffle down, new drops in to replace. This supports the
     554              : ring representation from Fredman et al., pg 125, fig 14 where the left
     555              : child's next pointer wraps to the last element in the list. The previous
     556              : pointer is to support faster deletes and decrease key operations.
     557              : 
     558              : < = next_sibling
     559              : > = prev_sibling
     560              : 
     561              :      A        A            A
     562              :     ╱        ╱            ╱
     563              : ┌─<B>─┐  ┌─<C>──<B>─┐ ┌─<D>──<C>──<B>─┐
     564              : └─────┘  └──────────┘ └───────────────┘
     565              : 
     566              : Pairing in the delete root phase would then start at B in this example and work
     567              : towards D. That is the oldest to youngest order mentioned in the paper and
     568              : helps set up the one-pass back-to-front variant mentioned in the paper allowing
     569              : the same runtime guarantees as the two pass standard pairing heap. */
     570              : static void
     571        10002 : link_child(
     572              :     struct CCC_Priority_queue_node *const parent,
     573              :     struct CCC_Priority_queue_node *const child
     574              : ) {
     575        10002 :     if (parent->child) {
     576         8168 :         child->next = parent->child->next;
     577         8168 :         child->prev = parent->child;
     578         8168 :         parent->child->next->prev = child;
     579         8168 :         parent->child->next = child;
     580         8168 :     } else {
     581         1834 :         child->next = child->prev = child;
     582              :     }
     583        10002 :     parent->child = child;
     584        10002 :     child->parent = parent;
     585        10002 : }
     586              : 
     587              : static inline CCC_Order
     588      1160680 : order(
     589              :     struct CCC_Priority_queue const *const priority_queue,
     590              :     struct CCC_Priority_queue_node const *const left,
     591              :     struct CCC_Priority_queue_node const *const right
     592              : ) {
     593      4642720 :     return priority_queue->comparator.compare((CCC_Comparator_arguments){
     594      1160680 :         .type_left = struct_base(priority_queue, left),
     595      1160680 :         .type_right = struct_base(priority_queue, right),
     596      1160680 :         .context = priority_queue->comparator.context,
     597              :     });
     598              : }
     599              : 
     600              : static inline void *
     601      2327596 : struct_base(
     602              :     struct CCC_Priority_queue const *const priority_queue,
     603              :     struct CCC_Priority_queue_node const *const node
     604              : ) {
     605      2327596 :     return ((char *)&(node->child)) - priority_queue->type_intruder_offset;
     606              : }
     607              : 
     608              : static inline struct CCC_Priority_queue_node *
     609         3144 : elem_in(
     610              :     struct CCC_Priority_queue const *const priority_queue,
     611              :     void const *const any_struct
     612              : ) {
     613         3144 :     return (struct CCC_Priority_queue_node
     614         3144 :                 *)((char *)any_struct + priority_queue->type_intruder_offset);
     615              : }
     616              : 
     617              : static inline void
     618         3132 : init_node(struct CCC_Priority_queue_node *const node) {
     619         3132 :     node->child = node->parent = NULL;
     620         3132 :     node->next = node->prev = node;
     621         3132 : }
     622              : 
     623              : static inline void
     624         2027 : clear_node(struct CCC_Priority_queue_node *const node) {
     625         2027 :     node->child = node->next = node->prev = node->parent = NULL;
     626         2027 : }
     627              : 
     628              : /*========================     Validation ================================*/
     629              : 
     630              : /* NOLINTBEGIN(*misc-no-recursion) */
     631              : 
     632              : static size_t
     633      1161109 : traversal_count(struct CCC_Priority_queue_node const *const root) {
     634      1161109 :     if (!root) {
     635       973054 :         return 0;
     636              :     }
     637       188055 :     size_t count = 0;
     638       188055 :     struct CCC_Priority_queue_node const *cur = root;
     639       188055 :     do {
     640      1155806 :         count += 1 + traversal_count(cur->child);
     641      1155806 :     } while ((cur = cur->next) != root);
     642       188055 :     return count;
     643      1161109 : }
     644              : 
     645              : static CCC_Tribool
     646      1161109 : has_valid_links(
     647              :     struct CCC_Priority_queue const *const priority_queue,
     648              :     struct CCC_Priority_queue_node const *const parent,
     649              :     struct CCC_Priority_queue_node const *const child
     650              : ) {
     651      1161109 :     if (!child) {
     652       973054 :         return CCC_TRUE;
     653              :     }
     654       188055 :     struct CCC_Priority_queue_node const *current = child;
     655       188055 :     CCC_Order const wrong_order = priority_queue->order == CCC_ORDER_LESSER
     656              :                                     ? CCC_ORDER_GREATER
     657              :                                     : CCC_ORDER_LESSER;
     658       188055 :     do {
     659              :         /* Reminder: Don't combine these if checks into one. Separating them
     660              :            makes it easier to find the problem when stepping through gdb. */
     661      1155806 :         if (!current) {
     662            0 :             return CCC_FALSE;
     663              :         }
     664      1155806 :         if (parent && current->parent != parent) {
     665            0 :             return CCC_FALSE;
     666              :         }
     667      1155806 :         if (parent && parent->child != current->parent->child) {
     668            0 :             return CCC_FALSE;
     669              :         }
     670      1155806 :         if (current->next->prev != current || current->prev->next != current) {
     671            0 :             return CCC_FALSE;
     672              :         }
     673      1155806 :         if (parent && (order(priority_queue, parent, current) == wrong_order)) {
     674            0 :             return CCC_FALSE;
     675              :         }
     676              :         /* RECURSE! */
     677      1155806 :         if (!has_valid_links(priority_queue, current, current->child)) {
     678            0 :             return CCC_FALSE;
     679              :         }
     680      1155806 :     } while ((current = current->next) != child);
     681       188055 :     return CCC_TRUE;
     682      1161109 : }
     683              : 
     684              : /* NOLINTEND(*misc-no-recursion) */
        

Generated by: LCOV version 2.4-beta