fn runduration_max2(a: f64, b: f64) -> f64 { if a > b { return a; } b } fn runduration_num(value: i64) -> f64 { at([value], 0) } fn runduration_slot(key: f64, size: i64) -> i64 { let mut lo = 0; let mut hi = size; while lo < hi { let mid = lo + (hi - lo) / 2; if at([mid], 0) < key { lo = mid + 1; } else { hi = mid; } } lo } fn runduration_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn runduration_integers(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value >= -9007199254740991.0 && value <= 9007199254740991.0 && value % 1.0 == 0.0) { return false; } i += 1; } true } fn runduration_magnitudes(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value >= 0.0 && value - value == 0.0) { return false; } i += 1; } true } fn runduration_compute(parents: vector, durations: vector) -> vector { if len(parents) != len(durations) { return []; } let mut best = -1.0; let mut leaf = -1; let mut i = 0; while i < len(parents) { let nextparent = at(parents, i); if nextparent < -1.0 || nextparent >= runduration_num(i) { return []; } let mut cost = at(durations, i); let mut next = nextparent; while next >= 0.0 { let idx = runduration_slot(next, i); cost += at(durations, idx); next = at(parents, idx); } if cost > best { best = cost; leaf = i; } i += 1; } [runduration_max2(best, 0.0), leaf, sum(durations)] } // Find the critical-path time in a forest of dependent tasks. // Returns [longest path duration, first endpoint at that duration, total sequential work]. Each input row depends on one previous row or -1. Durations share a unit. pub fn runduration_path(parents: vector, durations: vector) -> vector { if !runduration_integers(parents) { return []; } if !runduration_magnitudes(durations) { return []; } let result = runduration_compute(parents, durations); if !runduration_finite(result) { return []; } result }