use serde_json::Value;
use super::classic::GeneratorError;
use super::rng::{seeded_rng, uniform_f64};
use crate::SimplicialComplex;
fn all_combinations(n: usize, k: usize) -> Vec<Vec<usize>> {
let mut out = Vec::new();
if k == 0 || k > n {
return out;
}
let mut idx: Vec<usize> = (0..k).collect();
loop {
out.push(idx.clone());
let mut i = k;
while i > 0 {
i -= 1;
if idx[i] != i + n - k {
idx[i] += 1;
for j in (i + 1)..k {
idx[j] = idx[j - 1] + 1;
}
break;
}
if i == 0 {
return out;
}
}
}
}
pub fn random_simplicial_complex(
n: usize,
ps: &[f64],
seed: Option<u64>,
) -> Result<SimplicialComplex, GeneratorError> {
if ps.iter().any(|&p| !(0.0..=1.0).contains(&p)) {
return Err(GeneratorError::InvalidParameter(
"all elements of ps must be between 0 and 1".into(),
));
}
let mut rng = seeded_rng(seed);
let mut simplices: Vec<(Vec<String>, Option<String>, Value)> = Vec::new();
for (i, &p) in ps.iter().enumerate() {
let d = i + 1; for combo in all_combinations(n, d + 1) {
if uniform_f64(&mut rng) <= p {
simplices.push((
combo.iter().map(|x| x.to_string()).collect(),
None,
Value::Null,
));
}
}
}
let mut s = SimplicialComplex::new();
s.add_nodes_from((0..n).map(|i| (i.to_string(), Value::Null)));
s.add_simplices_from(simplices);
Ok(s)
}