diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-01 00:19:38 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-01 00:19:38 -0600 |
commit | 7d2d4ae383e02a03dec6aa60207e3a1dfa8e79a9 (patch) | |
tree | 5cbe78f32ae384eaae01352a6626bc83461677ff /pkg/util/generic.go | |
parent | aee0fa4cf09ef5af90e28441d673ce440e4c2c16 (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
Diffstat (limited to 'pkg/util/generic.go')
-rw-r--r-- | pkg/util/generic.go | 22 |
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 +} |