|
| 1 | +/* |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2021 Florimond Husquinet |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +/* |
| 26 | +A generic implementation of a d-ary heap. |
| 27 | +
|
| 28 | +The d-ary heap or d-heap is a priority queue data structure, a generalization |
| 29 | +of the binary heap in which the nodes have d children instead of 2. |
| 30 | +*/ |
| 31 | +package heap |
| 32 | + |
| 33 | +import "math" |
| 34 | + |
| 35 | +type DaryHeap[T any] struct { |
| 36 | + d int |
| 37 | + data []T |
| 38 | + compare func(T, T) int |
| 39 | +} |
| 40 | + |
| 41 | +func NewDaryHeap[T any](d int, compare func(T, T) int) *DaryHeap[T] { |
| 42 | + return &DaryHeap[T]{ |
| 43 | + d: d, |
| 44 | + data: make([]T, 0), |
| 45 | + compare: compare, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (h *DaryHeap[T]) Len() int { |
| 50 | + return len(h.data) |
| 51 | +} |
| 52 | + |
| 53 | +func (h *DaryHeap[T]) Push(value T) { |
| 54 | + h.data = append(h.data, value) |
| 55 | + h.bubbleUp(h.Len() - 1) |
| 56 | +} |
| 57 | + |
| 58 | +func (h *DaryHeap[T]) Pop() (ok bool, value T) { |
| 59 | + if h.Len() == 0 { |
| 60 | + return false, value |
| 61 | + } |
| 62 | + var top = h.data[0] |
| 63 | + h.data[0] = h.data[h.Len()-1] |
| 64 | + h.data = h.data[:h.Len()-1] |
| 65 | + h.sinkDown(0) |
| 66 | + return true, top |
| 67 | +} |
| 68 | + |
| 69 | +// Min heap: if a node is less than its parent, swap them. |
| 70 | +func (h *DaryHeap[T]) bubbleUp(index int) { |
| 71 | + if index == 0 { |
| 72 | + return |
| 73 | + } |
| 74 | + var parent = (index - 1) / h.d // Todo: make test fail if d is not 2 but you divide by 2 |
| 75 | + if h.compare(h.data[index], h.data[parent]) < 0 { |
| 76 | + h.swap(index, parent) |
| 77 | + h.bubbleUp(parent) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Min heap: if a node is greater than its children, swap the node with the smallest child. |
| 82 | +func (h *DaryHeap[T]) sinkDown(index int) { |
| 83 | + var childrenIndex = int(math.Pow(2, float64(index))) |
| 84 | + var smallest = index |
| 85 | + |
| 86 | + for i := 0; i < h.d; i++ { |
| 87 | + var child = childrenIndex + i |
| 88 | + if child >= h.Len() { |
| 89 | + break |
| 90 | + } |
| 91 | + if h.compare(h.data[child], h.data[smallest]) < 0 { |
| 92 | + smallest = child |
| 93 | + } |
| 94 | + } |
| 95 | + if smallest != index { |
| 96 | + h.swap(index, smallest) |
| 97 | + h.sinkDown(smallest) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func (h *DaryHeap[T]) swap(i, j int) { |
| 102 | + var tmp = h.data[i] |
| 103 | + h.data[i] = h.data[j] |
| 104 | + h.data[j] = tmp |
| 105 | +} |
0 commit comments