Skip to content

Commit

Permalink
[util/slice] Add util slice.
Browse files Browse the repository at this point in the history
  • Loading branch information
trasc committed May 15, 2023
1 parent 970867e commit 2ee58d7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/util/slice/slice.go
Original file line number Diff line number Diff line change
@@ -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] })
}

0 comments on commit 2ee58d7

Please sign in to comment.