import Foundation /// Compute once per data/filter/sort event, never from row rendering or scroll position. nonisolated enum ProcessTableProjection { static func rows(_ input: [ProcessMetrics], query: String, byMemory: Bool, order: [ProcessIdentity]) -> [ProcessMetrics] { let ranks = Dictionary(uniqueKeysWithValues: order.enumerated().map { ($0.element, $0.offset) }) return input.filter { query.isEmpty || $0.name.localizedCaseInsensitiveContains(query) || String($0.id.pid).contains(query) || ($0.path?.localizedCaseInsensitiveContains(query) ?? false) }.sorted { let aRank = ranks[$0.id] ?? Int.max, bRank = ranks[$1.id] ?? Int.max if aRank != bRank { return aRank < bRank } let a = byMemory ? $0.residentBytes.map(Double.init) ?? -1 : $0.cpu ?? -1 let b = byMemory ? $1.residentBytes.map(Double.init) ?? -1 : $1.cpu ?? -1 if a != b { return a > b } return $0.name == $1.name ? $0.id.pid < $1.id.pid : $0.name < $1.name } } }