blob: f9517f281ebdb825539b1dbb6df1ce88d029d7ce [file] [log] [blame]
Damien Georgefe203bb2020-01-13 17:10:41 +11001/*
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#ifndef MICROPY_INCLUDED_PY_PAIRHEAP_H
27#define MICROPY_INCLUDED_PY_PAIRHEAP_H
28
29// This is an implementation of a pairing heap. It is stable and has deletion
30// support. Only the less-than operation needs to be defined on elements.
31//
32// See original paper for details:
33// Michael L. Fredman, Robert Sedjewick, Daniel D. Sleator, and Robert E. Tarjan.
34// The Pairing Heap: A New Form of Self-Adjusting Heap.
35// Algorithmica 1:111-129, 1986.
36// https://www.cs.cmu.edu/~sleator/papers/pairing-heaps.pdf
37
38#include "py/obj.h"
39
40// This struct forms the nodes of the heap and is intended to be extended, by
41// placing it first in another struct, to include additional information for the
42// element stored in the heap. It includes "base" so it can be a MicroPython
43// object allocated on the heap and the GC can automatically trace all nodes by
44// following the tree structure.
45typedef struct _mp_pairheap_t {
46 mp_obj_base_t base;
47 struct _mp_pairheap_t *child;
48 struct _mp_pairheap_t *child_last;
49 struct _mp_pairheap_t *next;
50} mp_pairheap_t;
51
52// This is the function for the less-than operation on nodes/elements.
53typedef int (*mp_pairheap_lt_t)(mp_pairheap_t*, mp_pairheap_t*);
54
55// Core functions.
56mp_pairheap_t *mp_pairheap_meld(mp_pairheap_lt_t lt, mp_pairheap_t *heap1, mp_pairheap_t *heap2);
57mp_pairheap_t *mp_pairheap_pairing(mp_pairheap_lt_t lt, mp_pairheap_t *child);
58mp_pairheap_t *mp_pairheap_delete(mp_pairheap_lt_t lt, mp_pairheap_t *heap, mp_pairheap_t *node);
59
60// Create a new heap.
61static inline mp_pairheap_t *mp_pairheap_new(mp_pairheap_lt_t lt) {
62 (void)lt;
63 return NULL;
64}
65
66// Test if the heap is empty.
67static inline bool mp_pairheap_is_empty(mp_pairheap_lt_t lt, mp_pairheap_t *heap) {
68 (void)lt;
69 return heap == NULL;
70}
71
72// Peek at the top of the heap. Will return NULL if empty.
73static inline mp_pairheap_t *mp_pairheap_peek(mp_pairheap_lt_t lt, mp_pairheap_t *heap) {
74 (void)lt;
75 return heap;
76}
77
78// Push new node onto existing heap. Returns the new heap.
79static inline mp_pairheap_t *mp_pairheap_push(mp_pairheap_lt_t lt, mp_pairheap_t *heap, mp_pairheap_t *node) {
80 node->child = NULL;
81 node->next = NULL;
82 return mp_pairheap_meld(lt, node, heap); // node is first to be stable
83}
84
85// Pop the top off the heap, which must not be empty. Returns the new heap.
86static inline mp_pairheap_t *mp_pairheap_pop(mp_pairheap_lt_t lt, mp_pairheap_t *heap) {
87 return mp_pairheap_pairing(lt, heap->child);
88}
89
90#endif // MICROPY_INCLUDED_PY_PAIRHEAP_H