get the top N values. Given as a callback to avoid allocating. bool result indicates if there were more than N values
(n int, cb func(key T))
| 546 | // get the top N values. Given as a callback to avoid allocating. |
| 547 | // bool result indicates if there were more than N values |
| 548 | func (t *topN[T]) get(n int, cb func(key T)) { |
| 549 | if len(t.entries) <= n { |
| 550 | // <= N, no need to sort |
| 551 | for _, e := range t.entries { |
| 552 | cb(e.key) |
| 553 | } |
| 554 | return |
| 555 | } |
| 556 | |
| 557 | sort.Slice(t.entries, func(i, j int) bool { |
| 558 | return t.entries[i].total > t.entries[j].total // Sort descending |
| 559 | }) |
| 560 | |
| 561 | for i := 0; i < n; i++ { |
| 562 | cb(t.entries[i].key) |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | func (t *topN[T]) reset() { |
| 567 | t.entries = t.entries[:0] |
no outgoing calls
no test coverage detected