From 2ee58d780f58c6450419e259c6e8662137844d1c Mon Sep 17 00:00:00 2001 From: Traian Schiau Date: Mon, 15 May 2023 15:43:45 +0300 Subject: [PATCH] [util/slice] Add util slice. --- pkg/util/slice/slice.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 pkg/util/slice/slice.go diff --git a/pkg/util/slice/slice.go b/pkg/util/slice/slice.go new file mode 100644 index 0000000000..3cd897a003 --- /dev/null +++ b/pkg/util/slice/slice.go @@ -0,0 +1,38 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package slice + +// ToMap create a map[K]V out of the provided slice s using key() to create the map keys and +// val() to create the values. +// +// The caller can compare the length of the map to the one of the slice in order to detect +// potential key conflicts. +func ToMap[K comparable, V any, S ~[]E, E any](s S, key func(int) K, val func(int) V) map[K]V { + ret := make(map[K]V, len(s)) + for i := range s { + ret[key(i)] = val(i) + } + return ret +} + +// ToRefMap create a map[K]*S out of the provided slice s []S using key() to create the map keys. +// +// The caller can compare the length of the map to the one of the slice in order to detect +// potential key conflicts. +func ToRefMap[K comparable, S ~[]E, E any](s S, key func(*E) K) map[K]*E { + return ToMap(s, func(i int) K { return key(&s[i]) }, func(i int) *E { return &s[i] }) +}