LCOV - code coverage report
Current view: top level - py - pairheap.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-343-g7b050b366.info Lines: 63 63 100.0 %
Date: 2024-04-25 17:00:48 Functions: 3 3 100.0 %
Branches: 36 36 100.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2020 Damien P. George
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include "py/pairheap.h"
      28                 :            : 
      29                 :            : // The mp_pairheap_t.next pointer can take one of the following values:
      30                 :            : //   - NULL: the node is the top of the heap
      31                 :            : //   - LSB set: the node is the last of the children and points to its parent node
      32                 :            : //   - other: the node is a child and not the last child
      33                 :            : // The macros below help manage this pointer.
      34                 :            : #define NEXT_MAKE_RIGHTMOST_PARENT(parent) ((void *)((uintptr_t)(parent) | 1))
      35                 :            : #define NEXT_IS_RIGHTMOST_PARENT(next) ((uintptr_t)(next) & 1)
      36                 :            : #define NEXT_GET_RIGHTMOST_PARENT(next) ((void *)((uintptr_t)(next) & ~1))
      37                 :            : 
      38                 :            : // O(1), stable
      39                 :     569253 : mp_pairheap_t *mp_pairheap_meld(mp_pairheap_lt_t lt, mp_pairheap_t *heap1, mp_pairheap_t *heap2) {
      40         [ +  + ]:     569253 :     if (heap1 == NULL) {
      41                 :            :         return heap2;
      42                 :            :     }
      43         [ +  + ]:     426826 :     if (heap2 == NULL) {
      44                 :            :         return heap1;
      45                 :            :     }
      46         [ +  + ]:     426293 :     if (lt(heap1, heap2)) {
      47         [ +  + ]:     283514 :         if (heap1->child == NULL) {
      48                 :     141793 :             heap1->child = heap2;
      49                 :            :         } else {
      50                 :     141721 :             heap1->child_last->next = heap2;
      51                 :            :         }
      52                 :     283514 :         heap1->child_last = heap2;
      53                 :     283514 :         heap2->next = NEXT_MAKE_RIGHTMOST_PARENT(heap1);
      54                 :     283514 :         return heap1;
      55                 :            :     } else {
      56                 :     142779 :         heap1->next = heap2->child;
      57                 :     142779 :         heap2->child = heap1;
      58         [ +  + ]:     142779 :         if (heap1->next == NULL) {
      59                 :        650 :             heap2->child_last = heap1;
      60                 :        650 :             heap1->next = NEXT_MAKE_RIGHTMOST_PARENT(heap2);
      61                 :            :         }
      62                 :     142779 :         return heap2;
      63                 :            :     }
      64                 :            : }
      65                 :            : 
      66                 :            : // amortised O(log N), stable
      67                 :     142952 : mp_pairheap_t *mp_pairheap_pairing(mp_pairheap_lt_t lt, mp_pairheap_t *child) {
      68         [ +  + ]:     142952 :     if (child == NULL) {
      69                 :            :         return NULL;
      70                 :            :     }
      71                 :            :     mp_pairheap_t *heap = NULL;
      72         [ +  + ]:     426641 :     while (!NEXT_IS_RIGHTMOST_PARENT(child)) {
      73                 :     284214 :         mp_pairheap_t *n1 = child;
      74                 :     284214 :         child = child->next;
      75                 :     284214 :         n1->next = NULL;
      76         [ +  + ]:     284214 :         if (!NEXT_IS_RIGHTMOST_PARENT(child)) {
      77                 :     142047 :             mp_pairheap_t *n2 = child;
      78                 :     142047 :             child = child->next;
      79                 :     142047 :             n2->next = NULL;
      80                 :     142047 :             n1 = mp_pairheap_meld(lt, n1, n2);
      81                 :            :         }
      82                 :     284214 :         heap = mp_pairheap_meld(lt, heap, n1);
      83                 :            :     }
      84                 :     142427 :     heap->next = NULL;
      85                 :     142427 :     return heap;
      86                 :            : }
      87                 :            : 
      88                 :            : // amortised O(log N), stable
      89                 :         75 : mp_pairheap_t *mp_pairheap_delete(mp_pairheap_lt_t lt, mp_pairheap_t *heap, mp_pairheap_t *node) {
      90                 :            :     // Simple case of the top being the node to delete
      91         [ +  + ]:         75 :     if (node == heap) {
      92                 :         43 :         mp_pairheap_t *child = heap->child;
      93                 :         43 :         node->child = NULL;
      94                 :         43 :         return mp_pairheap_pairing(lt, child);
      95                 :            :     }
      96                 :            : 
      97                 :            :     // Case where node is not in the heap
      98         [ +  + ]:         32 :     if (node->next == NULL) {
      99                 :            :         return heap;
     100                 :            :     }
     101                 :            : 
     102                 :            :     // Find parent of node
     103                 :            :     mp_pairheap_t *parent = node;
     104         [ +  + ]:         34 :     while (!NEXT_IS_RIGHTMOST_PARENT(parent->next)) {
     105                 :            :         parent = parent->next;
     106                 :            :     }
     107                 :         30 :     parent = NEXT_GET_RIGHTMOST_PARENT(parent->next);
     108                 :            : 
     109                 :            :     // Replace node with pairing of its children
     110                 :         30 :     mp_pairheap_t *next;
     111   [ +  +  +  + ]:         30 :     if (node == parent->child && node->child == NULL) {
     112         [ +  + ]:         14 :         if (NEXT_IS_RIGHTMOST_PARENT(node->next)) {
     113                 :         12 :             parent->child = NULL;
     114                 :            :         } else {
     115                 :          2 :             parent->child = node->next;
     116                 :            :         }
     117                 :         14 :         node->next = NULL;
     118                 :         14 :         return heap;
     119         [ +  + ]:         16 :     } else if (node == parent->child) {
     120                 :          4 :         mp_pairheap_t *child = node->child;
     121                 :          4 :         next = node->next;
     122                 :          4 :         node->child = NULL;
     123                 :          4 :         node->next = NULL;
     124                 :          4 :         node = mp_pairheap_pairing(lt, child);
     125                 :          4 :         parent->child = node;
     126                 :            :     } else {
     127                 :            :         mp_pairheap_t *n = parent->child;
     128         [ +  + ]:         18 :         while (node != n->next) {
     129                 :            :             n = n->next;
     130                 :            :         }
     131                 :         12 :         mp_pairheap_t *child = node->child;
     132                 :         12 :         next = node->next;
     133                 :         12 :         node->child = NULL;
     134                 :         12 :         node->next = NULL;
     135                 :         12 :         node = mp_pairheap_pairing(lt, child);
     136         [ +  + ]:         12 :         if (node == NULL) {
     137                 :            :             node = n;
     138                 :            :         } else {
     139                 :          2 :             n->next = node;
     140                 :            :         }
     141                 :            :     }
     142                 :         16 :     node->next = next;
     143         [ +  + ]:         16 :     if (NEXT_IS_RIGHTMOST_PARENT(next)) {
     144                 :         14 :         parent->child_last = node;
     145                 :            :     }
     146                 :            :     return heap;
     147                 :            : }

Generated by: LCOV version 1.15-5-g462f71d