2022-10-27 16:48:26 +02:00
|
|
|
package langext
|
|
|
|
|
2023-05-24 22:01:29 +02:00
|
|
|
type MapEntry[T comparable, V any] struct {
|
|
|
|
Key T
|
|
|
|
Value V
|
|
|
|
}
|
|
|
|
|
2022-10-27 16:48:26 +02:00
|
|
|
func MapKeyArr[T comparable, V any](v map[T]V) []T {
|
|
|
|
result := make([]T, 0, len(v))
|
|
|
|
for k := range v {
|
|
|
|
result = append(result, k)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2023-01-31 11:01:45 +01:00
|
|
|
|
2023-05-24 22:01:29 +02:00
|
|
|
func MapValueArr[T comparable, V any](v map[T]V) []V {
|
|
|
|
result := make([]V, 0, len(v))
|
|
|
|
for _, mv := range v {
|
|
|
|
result = append(result, mv)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-01-31 11:01:45 +01:00
|
|
|
func ArrToMap[T comparable, V any](a []V, keyfunc func(V) T) map[T]V {
|
|
|
|
result := make(map[T]V, len(a))
|
|
|
|
for _, v := range a {
|
|
|
|
result[keyfunc(v)] = v
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2023-04-23 19:31:48 +02:00
|
|
|
|
2023-07-19 19:24:58 +02:00
|
|
|
func ArrToSet[T comparable](a []T) map[T]bool {
|
|
|
|
result := make(map[T]bool, len(a))
|
|
|
|
for _, v := range a {
|
|
|
|
result[v] = true
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:01:29 +02:00
|
|
|
func MapToArr[T comparable, V any](v map[T]V) []MapEntry[T, V] {
|
|
|
|
result := make([]MapEntry[T, V], 0, len(v))
|
|
|
|
for mk, mv := range v {
|
|
|
|
result = append(result, MapEntry[T, V]{
|
|
|
|
Key: mk,
|
|
|
|
Value: mv,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-04-23 19:31:48 +02:00
|
|
|
func CopyMap[K comparable, V any](a map[K]V) map[K]V {
|
|
|
|
result := make(map[K]V, len(a))
|
|
|
|
for k, v := range a {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2023-05-25 18:20:31 +02:00
|
|
|
|
|
|
|
func ForceMap[K comparable, V any](v map[K]V) map[K]V {
|
|
|
|
if v == nil {
|
|
|
|
return make(map[K]V, 0)
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|