LCOV - code coverage report
Current view: top level - py - pairheap.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-724-gfb7d21153.info Lines: 63 63 100.0 %
Date: 2022-12-01 09:37:31 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                 :     573635 : mp_pairheap_t *mp_pairheap_meld(mp_pairheap_lt_t lt, mp_pairheap_t *heap1, mp_pairheap_t *heap2) {
      40         [ +  + ]:     573635 :     if (heap1 == NULL) {
      41                 :            :         return heap2;
      42                 :            :     }
      43         [ +  + ]:     430117 :     if (heap2 == NULL) {
      44                 :            :         return heap1;
      45                 :            :     }
      46         [ +  + ]:     429703 :     if (lt(heap1, heap2)) {
      47         [ +  + ]:     285885 :         if (heap1->child == NULL) {
      48                 :     142978 :             heap1->child = heap2;
      49                 :            :         } else {
      50                 :     142907 :             heap1->child_last->next = heap2;
      51                 :            :         }
      52                 :     285885 :         heap1->child_last = heap2;
      53                 :     285885 :         heap2->next = NEXT_MAKE_RIGHTMOST_PARENT(heap1);
      54                 :     285885 :         return heap1;
      55                 :            :     } else {
      56                 :     143818 :         heap1->next = heap2->child;
      57                 :     143818 :         heap2->child = heap1;
      58         [ +  + ]:     143818 :         if (heap1->next == NULL) {
      59                 :        556 :             heap2->child_last = heap1;
      60                 :        556 :             heap1->next = NEXT_MAKE_RIGHTMOST_PARENT(heap2);
      61                 :            :         }
      62                 :     143818 :         return heap2;
      63                 :            :     }
      64                 :            : }
      65                 :            : 
      66                 :            : // amortised O(log N), stable
      67                 :     143923 : mp_pairheap_t *mp_pairheap_pairing(mp_pairheap_lt_t lt, mp_pairheap_t *child) {
      68         [ +  + ]:     143923 :     if (child == NULL) {
      69                 :            :         return NULL;
      70                 :            :     }
      71                 :            :     mp_pairheap_t *heap = NULL;
      72         [ +  + ]:     430001 :     while (!NEXT_IS_RIGHTMOST_PARENT(child)) {
      73                 :     286483 :         mp_pairheap_t *n1 = child;
      74                 :     286483 :         child = child->next;
      75                 :     286483 :         n1->next = NULL;
      76         [ +  + ]:     286483 :         if (!NEXT_IS_RIGHTMOST_PARENT(child)) {
      77                 :     143189 :             mp_pairheap_t *n2 = child;
      78                 :     143189 :             child = child->next;
      79                 :     143189 :             n2->next = NULL;
      80                 :     143189 :             n1 = mp_pairheap_meld(lt, n1, n2);
      81                 :            :         }
      82                 :     286483 :         heap = mp_pairheap_meld(lt, heap, n1);
      83                 :            :     }
      84                 :     143518 :     heap->next = NULL;
      85                 :     143518 :     return heap;
      86                 :            : }
      87                 :            : 
      88                 :            : // amortised O(log N), stable
      89                 :         74 : 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         [ +  + ]:         74 :     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         [ +  + ]:         31 :     if (node->next == NULL) {
      99                 :            :         return heap;
     100                 :            :     }
     101                 :            : 
     102                 :            :     // Find parent of node
     103                 :            :     mp_pairheap_t *parent = node;
     104         [ +  + ]:         32 :     while (!NEXT_IS_RIGHTMOST_PARENT(parent->next)) {
     105                 :            :         parent = parent->next;
     106                 :            :     }
     107                 :         29 :     parent = NEXT_GET_RIGHTMOST_PARENT(parent->next);
     108                 :            : 
     109                 :            :     // Replace node with pairing of its children
     110                 :         29 :     mp_pairheap_t *next;
     111   [ +  +  +  + ]:         29 :     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         [ +  + ]:         15 :     } 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         [ +  + ]:         17 :         while (node != n->next) {
     129                 :            :             n = n->next;
     130                 :            :         }
     131                 :         11 :         mp_pairheap_t *child = node->child;
     132                 :         11 :         next = node->next;
     133                 :         11 :         node->child = NULL;
     134                 :         11 :         node->next = NULL;
     135                 :         11 :         node = mp_pairheap_pairing(lt, child);
     136         [ +  + ]:         11 :         if (node == NULL) {
     137                 :            :             node = n;
     138                 :            :         } else {
     139                 :          2 :             n->next = node;
     140                 :            :         }
     141                 :            :     }
     142                 :         15 :     node->next = next;
     143         [ +  + ]:         15 :     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