use crate::value::{Array, Value};
pub fn block_parts(block: &Value) -> Option<(Array, Vec<usize>)> {
let (values, placement) = match block {
Value::Object(o) if o.args.len() >= 2 => (o.args[0].clone(), o.args[1].clone()),
Value::Object(o) => match o.state.as_deref()? {
Value::Dict(d) => (d.get("values")?.clone(), d.get("placement")?.clone()),
Value::Tuple(t) | Value::List(t) => (t.first()?.clone(), t.get(1)?.clone()),
_ => return None,
},
Value::Tuple(t) | Value::List(t) => (t.first()?.clone(), t.get(1)?.clone()),
_ => return None,
};
Some((values_array(&values)?, positions(&placement)))
}
pub fn values_array(v: &Value) -> Option<Array> {
match v {
Value::Array(a) => Some(a.clone()),
Value::Object(o) if o.name == "_restore_array" => super::arrow::restore_array(o),
Value::Object(o) if o.name == "chunked_array" => super::arrow::chunked_array(o),
Value::Dict(d) => d.iter().find_map(|(_, x)| values_array(x)),
Value::Object(o) => {
for candidate in o.args.iter().chain(o.state.as_deref()) {
if let Some(a) = values_array(candidate) {
return Some(a);
}
}
None
}
Value::Tuple(items) | Value::List(items) => {
if let Some(a) = items.iter().find_map(values_array) {
return Some(a);
}
let strs: Vec<String> =
items.iter().filter_map(|x| x.as_str().map(String::from)).collect();
if !strs.is_empty() && strs.len() == items.len() {
let n = strs.len();
return ndarray::ArrayD::from_shape_vec(ndarray::IxDyn(&[1, n]), strs)
.ok()
.map(Array::Str);
}
None
}
_ => None,
}
}
pub fn positions(v: &Value) -> Vec<usize> {
match v {
Value::Array(a) => (0..a.len()).filter_map(|i| int_at(a, i)).collect(),
Value::List(items) | Value::Tuple(items) => {
items.iter().filter_map(|x| x.as_i64()).map(|x| x as usize).collect()
}
Value::Object(o) if o.name == "slice" => {
let n = |i: usize, d: i64| o.args.get(i).and_then(|v| v.as_i64()).unwrap_or(d);
let (start, stop, step) = (n(0, 0), n(1, 0), n(2, 1).max(1));
(start..stop).step_by(step as usize).map(|x| x as usize).collect()
}
_ => Vec::new(),
}
}
fn int_at(a: &Array, i: usize) -> Option<usize> {
a.as_i64()
.map(|x| x[i])
.or_else(|| a.as_i32().map(|x| x[i] as i64))
.or_else(|| a.as_i16().map(|x| x[i] as i64))
.filter(|x| *x >= 0)
.map(|x| x as usize)
}