2023-10-20 13:41:06 +02:00
|
|
|
package lib
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
import (
|
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
|
|
|
)
|
|
|
|
|
2023-10-20 13:41:06 +02:00
|
|
|
// MapToSlice converts a map to a slice in go
|
|
|
|
func MapValues[K comparable, V any](m map[K]V) []V {
|
|
|
|
return MapValuesWithExclude(m, map[K]struct{}{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func MapValuesWithExclude[K comparable, V any](m map[K]V, exclude map[K]struct{}) []V {
|
|
|
|
values := make([]V, len(m)-len(exclude))
|
|
|
|
|
|
|
|
i := 0
|
2023-10-21 19:08:45 +02:00
|
|
|
|
|
|
|
if len(m)-len(exclude) <= 0 {
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
2023-10-20 13:41:06 +02:00
|
|
|
for k, v := range m {
|
|
|
|
if _, excluded := exclude[k]; excluded {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
logging.Log.WriteInfof("Key %s", k)
|
|
|
|
|
2023-10-20 13:41:06 +02:00
|
|
|
values[i] = v
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return values
|
|
|
|
}
|
2023-10-25 19:34:38 +02:00
|
|
|
|
|
|
|
func MapKeys[K comparable, V any](m map[K]V) []K {
|
|
|
|
values := make([]K, len(m))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for k, _ := range m {
|
|
|
|
values[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return values
|
|
|
|
}
|