From 7d2d4ae383e02a03dec6aa60207e3a1dfa8e79a9 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 1 Jul 2022 00:19:38 -0600 Subject: 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 --- pkg/util/generic.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'pkg/util/generic.go') 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 +} -- cgit v1.2.3-2-g168b