forked from lavanet/lava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps.go
More file actions
55 lines (47 loc) · 985 Bytes
/
maps.go
File metadata and controls
55 lines (47 loc) · 985 Bytes
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
package maps
import (
"github.com/lavanet/lava/v5/utils/lavaslices"
"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
)
func FindLargestIntValueInMap[K comparable](myMap map[K]int) (K, int) {
var maxVal int
var maxKey K
firstIteration := true
for key, val := range myMap {
if firstIteration || val > maxVal {
maxVal = val
maxKey = key
firstIteration = false
}
}
return maxKey, maxVal
}
func StableSortedKeys[T constraints.Ordered, V any](m map[T]V) []T {
keys := maps.Keys(m)
lavaslices.SortStable(keys)
return keys
}
func GetMaxKey[T constraints.Ordered, V any](m map[T]V) T {
var maxKey T
for k := range m {
if k > maxKey {
maxKey = k
}
}
return maxKey
}
func KeysSlice[T comparable, V any](in map[T]V) []T {
keys := []T{}
for k := range in {
keys = append(keys, k)
}
return keys
}
func ValuesSlice[T comparable, V any](in map[T]V) []V {
values := []V{}
for _, v := range in {
values = append(values, v)
}
return values
}