From 703d4ff8bf723674f1bee3a2fa75aa8b5ed60520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 12 Nov 2024 14:51:18 +0800 Subject: [PATCH] Add common.ReverseMap --- common/cond.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/common/cond.go b/common/cond.go index 9a447543..78e23650 100644 --- a/common/cond.go +++ b/common/cond.go @@ -268,6 +268,14 @@ func Reverse[T any](arr []T) []T { return arr } +func ReverseMap[K comparable, V comparable](m map[K]V) map[V]K { + ret := make(map[V]K, len(m)) + for k, v := range m { + ret[v] = k + } + return ret +} + func Done(ctx context.Context) bool { select { case <-ctx.Done():