// Albatross 0.1.0 ยท MIT OR Apache-2.0 // https://github.com/Rahulchaube1/blyx-albatross/tree/v0.1.0 fn dagorder_num(value: i64) -> f64 { at([value], 0) } fn dagorder_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 dagorder_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 dagorder_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 dagorder_compute(parents: vector) -> vector { let mut roots = 0; let mut deepest = 0; let mut leaf = -1; let mut i = 0; while i < len(parents) { let parent = at(parents, i); if parent < -1.0 || parent >= dagorder_num(i) { return []; } if parent == -1.0 { roots += 1; } let mut steps = 1; let mut next = parent; while next >= 0.0 { let idx = dagorder_slot(next, i); next = at(parents, idx); steps += 1; } if steps > deepest { deepest = steps; leaf = i; } i += 1; } [roots, deepest, leaf] } // Validate indexed dependencies in an ordered build forest. // Each stage names one earlier parent or -1 for a root. Returns [root count, maximum chain length in stages, first stage with that depth]. Empty forests have zero depth. pub fn dagorder_forest(parents: vector) -> vector { if !dagorder_integers(parents) { return []; } let result = dagorder_compute(parents); if !dagorder_finite(result) { return []; } result } 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 } fn albatross_max2(a: f64, b: f64) -> f64 { if a > b { return a; } b } fn albatross_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 albatross_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 albatross_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 albatross_compute(parents: vector, durations: vector, workers: f64) -> vector { let shape = dagorder_forest(parents); let timing = runduration_path(parents, durations); if len(shape) == 0 || len(timing) == 0 { return []; } [at(timing, 0), at(timing, 2), albatross_max2(at(timing, 0), at(timing, 2) / workers), at(shape, 1)] } // Estimate a build forest's scheduling lower bound from dependencies and worker capacity. // Returns [critical path, total work, lower bound with given workers, deepest stage count]. The capacity bound assumes divisible worker time; actual nonpreemptive schedules can take longer. pub fn albatross_schedule(parents: vector, durations: vector, workers: f64) -> vector { if !albatross_integers(parents) { return []; } if !albatross_magnitudes(durations) { return []; } if !(workers >= 1.0 && workers <= 9007199254740991.0 && workers % 1.0 == 0.0) { return []; } let result = albatross_compute(parents, durations, workers); if !albatross_finite(result) { return []; } result } fn main() { let parents = [-1, 0, 0, 2]; let durations = [1, 3, 2, 5]; let workers = 2.0; let result = albatross_schedule(parents, durations, workers); if len(result) == 0 { println!("invalid"); return; } let mut i = 0; while i < len(result) { println!(at(result, i)); i += 1; } }