-
Notifications
You must be signed in to change notification settings - Fork 1
/
assigned-priority.go
43 lines (37 loc) · 1.35 KB
/
assigned-priority.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
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package pqs
import (
"golang.org/x/exp/constraints"
)
// AssignedPriority contains the assigned priority for a priority-queue element
// - V is the element value type whose pointer-value provides identity
// - P is the priority, a descending-ordered type
// - SetPriority updates the priority
// - Cmp makes AssignedPriority ordered
type AssignedPriority[V any, P constraints.Ordered] struct {
Priority P // the main sort value, ordered high to low
Index int // insertion order: lowest/earliest value first: distinguishes between equal priorities
Value *V // the pointer provides identity for this priority
}
func NewAssignedPriority[V any, P constraints.Ordered](priority P, index int, value *V) (assignedPriority *AssignedPriority[V, P]) {
return &AssignedPriority[V, P]{Priority: priority, Index: index, Value: value}
}
func (ap *AssignedPriority[V, P]) SetPriority(priority P) {
ap.Priority = priority
}
// Cmp sorts descending: -1 results appears first
func (a *AssignedPriority[V, P]) Cmp(b *AssignedPriority[V, P]) (result int) {
if a.Priority > b.Priority {
return -1
} else if a.Priority < b.Priority {
return 1
} else if a.Index < b.Index {
return -1
} else if a.Index > b.Index {
return 1
}
return 0
}