22 lines
361 B
Go
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)
|
|
}
|
|
}
|
|
}
|