-
Notifications
You must be signed in to change notification settings - Fork 1
/
argmaxheap.go
74 lines (64 loc) · 1.96 KB
/
argmaxheap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2022, NLP Odyssey Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gomaddness
import "container/heap"
// ArgMaxHeap is a max-heap working on the indices of a Vector, without
// modifying the Vector itself.
type ArgMaxHeap[F Float] struct {
vector Vector[F]
indices []int
}
// NewArgMaxHeap creates and initializes a new ArgMaxHeap object.
func NewArgMaxHeap[F Float](v Vector[F]) *ArgMaxHeap[F] {
indices := make([]int, len(v))
for i := range indices {
indices[i] = i
}
h := &ArgMaxHeap[F]{
vector: v,
indices: indices,
}
heap.Init(h)
return h
}
// FirstArgsMax returns the first n arguments of the maxima (arg max) of v.
//
// If n is greater than Len, only Len indices are returned.
func (h *ArgMaxHeap[_]) FirstArgsMax(n int) []int {
if n > len(h.indices) {
n = len(h.indices)
}
indices := make([]int, n)
for i := range indices {
indices[i] = heap.Pop(h).(int)
}
return indices
}
// Len is the number of indices in the collection.
func (h *ArgMaxHeap[_]) Len() int {
return len(h.indices)
}
// Less reports whether the index at i must sort before the index of j.
// It returns true only if the vector's value at the i-th index is grater
// than the value at the j-th index.
func (h *ArgMaxHeap[_]) Less(i, j int) bool {
return h.vector[h.indices[i]] > h.vector[h.indices[j]]
}
// Swap swaps the i-th and j-th indices.
func (h *ArgMaxHeap[_]) Swap(i, j int) {
h.indices[i], h.indices[j] = h.indices[j], h.indices[i]
}
// Push always panics: in order to simplify the implementation, it is not
// allowed to add new elements.
func (h *ArgMaxHeap[_]) Push(any) {
panic("maddness: unexpected call to ArgMaxHeap.Push")
}
// Pop removes and returns the index of the maximum element
// (according to Less) from the heap.
func (h *ArgMaxHeap[_]) Pop() any {
lastIndex := len(h.indices) - 1
x := h.indices[lastIndex]
h.indices = h.indices[:lastIndex]
return x
}