func reorderArrayKeysForMerge()

in pkg/log/structure/merger/merged.go [641:704]


func reorderArrayKeysForMerge(prev []string, patch []string, setElementOrderDirective []string) []string {
	parallelList := []string{}
	liveList := []string{}
	liveOnlyList := []string{}
	patchMap := toIndexMap(patch)
	prevMap := toIndexMap(prev)
	directiveMap := toIndexMap(setElementOrderDirective)
	for _, directiveElem := range setElementOrderDirective {
		if _, found := patchMap[directiveElem]; !found {
			if _, found := prevMap[directiveElem]; !found {
				// Assume the item was existing from the past
				liveList = append(liveList, directiveElem)
			}
		}
	}
	for _, elem := range prev {
		isLiveOnly := false
		if _, found := directiveMap[elem]; !found {
			if _, found := patchMap[elem]; !found {
				isLiveOnly = true
			}
		}
		if isLiveOnly {
			liveOnlyList = append(liveOnlyList, elem)
		} else {
			liveList = append(liveList, elem)
		}
	}
	parallelList = append(parallelList, liveList...)
	parallelList = append(parallelList, patch...)
	parallelList = append(parallelList, setElementOrderDirective...)
	slices.Sort(parallelList)
	parallelList = slices.Compact(parallelList)
	liveMap := toIndexMap(liveList)
	liveOnlyMap := toIndexMap(liveOnlyList)
	slices.SortStableFunc(parallelList, func(a, b string) int {
		if bothInMap(directiveMap, a, b) {
			return directiveMap[a] - directiveMap[b]
		}
		if bothInMap(patchMap, a, b) {
			return patchMap[a] - patchMap[b]
		}
		if bothInMap(liveMap, a, b) {
			return liveMap[a] - liveMap[b]
		}
		if _, found := liveMap[a]; found {
			return -1
		}
		return 1
	})
	slices.SortStableFunc(liveOnlyList, func(a, b string) int {
		if bothInMap(directiveMap, a, b) {
			return directiveMap[a] - directiveMap[b]
		}
		if bothInMap(liveOnlyMap, a, b) {
			return liveOnlyMap[a] - liveOnlyMap[b]
		}
		if _, found := directiveMap[a]; found {
			return -1
		}
		return 1
	})
	return append(liveOnlyList, parallelList...)
}