summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-01 00:19:38 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-01 00:19:38 -0600
commit5fa1cbeedb79f5578a1ffe5a6e2af0872d491f20 (patch)
tree8ce462902b4f27bafea3fe90faaac9b1ec7998e9
parent0e4a8b898639a06840749e7d2ca0b4596d97ad5b (diff)
move dumb map and sort operations to util/generic.go
Also, the optimization of reversing the node list in pass1 isn't relevant anymore now that I'm using rbtrees
-rw-r--r--pkg/util/generic.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/pkg/util/generic.go b/pkg/util/generic.go
index 6474ea5..70fed5b 100644
--- a/pkg/util/generic.go
+++ b/pkg/util/generic.go
@@ -1,6 +1,8 @@
package util
import (
+ "sort"
+
"golang.org/x/exp/constraints"
)
@@ -55,3 +57,23 @@ func Min[T constraints.Ordered](a, b T) T {
}
return b
}
+
+func MapKeys[K comparable, V any](m map[K]V) []K {
+ ret := make([]K, 0, len(m))
+ for k := range m {
+ ret = append(ret, k)
+ }
+ return ret
+}
+
+func SortSlice[T constraints.Ordered](slice []T) {
+ sort.Slice(slice, func(i, j int) bool {
+ return slice[i] < slice[j]
+ })
+}
+
+func SortedMapKeys[K constraints.Ordered, V any](m map[K]V) []K {
+ ret := MapKeys(m)
+ SortSlice(ret)
+ return ret
+}