|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +/// A data structure that keep satisfies the "heap property". This property |
| 6 | +/// dictates that in a max-heap, each node's value is greater than or equal |
| 7 | +/// to its children's values, and in a min-heap, each node's value is less |
| 8 | +/// than or equal to its children's values. |
| 9 | +/// |
| 10 | +/// The provided comparator decides which kind of heap is being built. |
| 11 | +class _Heap<T> { |
| 12 | + final Comparator<T> _compare; |
| 13 | + final _items = <T>[]; |
| 14 | + |
| 15 | + _Heap(this._compare); |
| 16 | + |
| 17 | + int get length => _items.length; |
| 18 | + |
| 19 | + void _up(int index) { |
| 20 | + final item = _items[index]; |
| 21 | + while (index > 0) { |
| 22 | + final parentIndex = (index - 1) >> 1; |
| 23 | + final parent = _items[parentIndex]; |
| 24 | + if (_compare(parent, item) <= 0) { |
| 25 | + return; |
| 26 | + } |
| 27 | + _items[parentIndex] = item; |
| 28 | + _items[index] = parent; |
| 29 | + index = parentIndex; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + void _down(int index) { |
| 34 | + final maxLength = _items.length; |
| 35 | + final item = _items[index]; |
| 36 | + while (index < maxLength) { |
| 37 | + final leftIndex = (index << 1) + 1; |
| 38 | + if (leftIndex >= maxLength) { |
| 39 | + return; |
| 40 | + } |
| 41 | + var childIndex = leftIndex; |
| 42 | + final rightIndex = leftIndex + 1; |
| 43 | + if (rightIndex < maxLength && |
| 44 | + _compare(_items[leftIndex], _items[rightIndex]) > 0) { |
| 45 | + childIndex = rightIndex; |
| 46 | + } |
| 47 | + if (_compare(item, _items[childIndex]) <= 0) { |
| 48 | + return; |
| 49 | + } |
| 50 | + _items[index] = _items[childIndex]; |
| 51 | + _up(index); |
| 52 | + _items[childIndex] = item; |
| 53 | + index = childIndex; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Builds a sorted list of the top-k items using the provided comparator. |
| 59 | +/// |
| 60 | +/// The algorithm uses min-heap to select the top-k items, and then builds |
| 61 | +/// a max-heap and uses heap sort to return the items in descending order. |
| 62 | +class TopKSortedListBuilder<T> { |
| 63 | + final int _k; |
| 64 | + final _Heap<T> _heap; |
| 65 | + |
| 66 | + TopKSortedListBuilder(this._k, Comparator<T> compare) |
| 67 | + : _heap = _Heap<T>(compare); |
| 68 | + |
| 69 | + void addAll(Iterable<T> items) { |
| 70 | + for (final item in items) { |
| 71 | + add(item); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + void add(T item) { |
| 76 | + _heap._items.add(item); |
| 77 | + } |
| 78 | + |
| 79 | + /// Gets and removes the top-k items from the current list. |
| 80 | + Iterable<T> getTopK() sync* { |
| 81 | + if (_heap._items.isEmpty) { |
| 82 | + return; |
| 83 | + } |
| 84 | + for (var i = _heap._items.length >> 1; i >= 0; i--) { |
| 85 | + _heap._down(i); |
| 86 | + } |
| 87 | + var count = _k; |
| 88 | + while (count > 0 && _heap._items.isNotEmpty) { |
| 89 | + yield _heap._items[0]; |
| 90 | + count--; |
| 91 | + final last = _heap._items.removeLast(); |
| 92 | + if (_heap._items.isEmpty) { |
| 93 | + break; |
| 94 | + } |
| 95 | + _heap._items[0] = last; |
| 96 | + _heap._down(0); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments