From c1578391cc2089cd224fd8325c333038e0ba7b7b Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 31 Mar 2023 18:01:47 -0600 Subject: maps: Add HasKey and HaveAnyKeysInCommon functions, use them --- lib/maps/maputil.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib/maps/maputil.go') diff --git a/lib/maps/maputil.go b/lib/maps/maputil.go index d409e70..63e52a0 100644 --- a/lib/maps/maputil.go +++ b/lib/maps/maputil.go @@ -25,3 +25,20 @@ func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K { slices.Sort(ret) return ret } + +func HasKey[K comparable, V any](m map[K]V, k K) bool { + _, has := m[k] + return has +} + +func HaveAnyKeysInCommon[K comparable, V1, V2 any](small map[K]V1, big map[K]V2) bool { + if len(big) < len(small) { + return HaveAnyKeysInCommon(big, small) + } + for v := range small { + if _, ok := big[v]; ok { + return true + } + } + return false +} -- cgit v1.2.3-2-g168b