use leopard_vec::{LQueue, LVec, LMask};
fn main() {
let q = LQueue::new();
let x: LVec<u32> = q.lvec();
let y: LVec<u32> = q.lvec();
let a: LVec<f64> = q.lvec_with_capacity(500);
let b: LVec<f64> = q.lvec_with_capacity(500);
println!("=== Recording and Executing All Operations in One Parallel Batch ===\n");
q.start();
let x = x.fill_with(|i| (i + 1) as u32);
let y = y.fill_with(|i| ((i + 1) * 2) as u32);
let a = a.fill_with(|i| (i + 1) as f64 * 0.5);
let b = b.fill_with(|i| (i + 1) as f64 * 0.25);
let z = &x * &y + &x * &y; let w = &z / &x;
let c = &a * &b;
let d = &c + &a;
let mapped = a.map(|i, val| val * (i as f64 + 1.0));
let branched = x.map_where(
|_, val| *val % 2 == 0, |_, val| val * 10, |_, val| val + 1000, );
q.end();
println!("=== Results from First Batch ===");
if let Some(x_result) = x.materialize() {
println!("x[0..5] = {:?}", &x_result[0..5]);
}
if let Some(y_result) = y.materialize() {
println!("y[0..5] = {:?}", &y_result[0..5]);
}
if let Some(z_result) = z.materialize() {
println!("z[0..5] = 2*x*y = {:?}", &z_result[0..5]);
}
if let Some(w_result) = w.materialize() {
println!("w[0..5] = z/x = {:?}", &w_result[0..5]);
}
if let Some(c_result) = c.materialize() {
println!("c[0..5] = a*b = {:?}", &c_result[0..5]);
}
if let Some(d_result) = d.materialize() {
println!("d[0..5] = c+a = {:?}", &d_result[0..5]);
}
if let Some(mapped_result) = mapped.materialize() {
println!("mapped[0..5] = {:?}", &mapped_result[0..5]);
}
if let Some(branched_result) = branched.materialize() {
println!("branched[0..6] = {:?}", &branched_result[0..6]);
}
println!("\n=== Blend Demo with Mask ===");
let v1: LVec<u32> = q.lvec_with_capacity(10);
let v2: LVec<u32> = q.lvec_with_capacity(10);
let mask = LMask::from_fn(10, |i| i >= 5);
println!("mask: {:?}", mask.as_slice());
q.start();
let v1 = v1.fill_with(|i| i as u32); let v2 = v2.fill_with(|i| (i * 100) as u32);
let blended = v1.blend(&v2, &mask);
let masked_doubled = v1.masked_apply(&mask, |_, val| val * 2);
let masked_filled = v1.masked_fill(&mask, 999);
q.end();
println!("v1: {:?}", v1.materialize().unwrap());
println!("v2: {:?}", v2.materialize().unwrap());
println!("blended (v1 where i<5, v2 where i>=5): {:?}", blended.materialize().unwrap());
println!("masked_doubled (v1*2 where i>=5): {:?}", masked_doubled.materialize().unwrap());
println!("masked_filled (999 where i>=5): {:?}", masked_filled.materialize().unwrap());
println!("\n=== Mask Operations Demo ===");
let mask_a = LMask::from_fn(8, |i| i < 4); let mask_b = LMask::from_fn(8, |i| i % 2 == 0);
println!("mask_a (i < 4): {:?}", mask_a.as_slice());
println!("mask_b (i %% 2 == 0): {:?}", mask_b.as_slice());
println!("mask_a & mask_b: {:?}", (&mask_a & &mask_b).as_slice());
println!("mask_a | mask_b: {:?}", (&mask_a | &mask_b).as_slice());
println!("mask_a ^ mask_b: {:?}", (&mask_a ^ &mask_b).as_slice());
println!("!mask_a: {:?}", (!&mask_a).as_slice());
println!("\n=== Chained Operations Demo ===");
let arr: LVec<f64> = q.lvec_with_capacity(8);
q.start();
let arr = arr.fill_with(|i| i as f64 + 1.0);
let squared = arr.map(|_, v| v * v); let conditional = squared.map_where(
|_, v| *v > 20.0,
|_, v| v / 2.0, |_, v| v * 2.0, );
q.end();
println!("arr: {:?}", arr.materialize().unwrap());
println!("squared: {:?}", squared.materialize().unwrap());
println!("conditional: {:?}", conditional.materialize().unwrap());
println!("\n=== Queue State Demo ===");
println!("q.is_recording() before start: {}", q.is_recording());
q.start();
println!("q.is_recording() after start: {}", q.is_recording());
q.end();
println!("q.is_recording() after end: {}", q.is_recording());
println!("\n=== Key Insights ===");
println!("✓ ALL operations (fill, map, add, mul, div, map_where, blend, etc.) are RECORDED");
println!("✓ No parallel execution happens until q.end() is called");
println!("✓ q.end() executes ALL recorded operations in ONE parallel batch");
println!("✓ This minimizes thread pool creation/destruction overhead!");
println!("✓ Operations are executed in dependency order (DAG)");
println!("✓ Same queue supports multiple types (u32, f64, etc.)");
println!("✓ Masks can be created with LMask::from_fn() for complex patterns");
}