Files
2026-02-04 13:12:06 +07:00

22 lines
361 B
Go

package shared
func MapChildToParent[P any, C any, K comparable](
parents []P,
children []C,
parentKey func(*P) K,
childKey func(C) K,
attach func(*P, C),
) {
index := make(map[K]*P)
for i := range parents {
index[parentKey(&parents[i])] = &parents[i]
}
for _, c := range children {
if p, ok := index[childKey(c)]; ok {
attach(p, c)
}
}
}