#[polydat::polydat_node(category = Arithmetic)]
fn vec_len_i32(v: &[i32]) -> u64 {
v.len() as u64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_len_i64(v: &[i64]) -> u64 {
v.len() as u64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_at_i32(v: &[i32], i: u64) -> i64 {
v.get(i as usize).copied().unwrap_or(0) as i64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_at_i64(v: &[i64], i: u64) -> i64 {
v.get(i as usize).copied().unwrap_or(0)
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_len_f32(v: &[f32]) -> u64 {
v.len() as u64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_len_f64(v: &[f64]) -> u64 {
v.len() as u64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_at_f32(v: &[f32], i: u64) -> f64 {
v.get(i as usize).copied().map_or(0.0, f64::from)
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_at_f64(v: &[f64], i: u64) -> f64 {
v.get(i as usize).copied().unwrap_or(0.0)
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_len_f16(v: &[polydat::half::f16]) -> u64 {
v.len() as u64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_at_f16(v: &[polydat::half::f16], i: u64) -> f64 {
v.get(i as usize).copied().map_or(0.0, f64::from)
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_len_i8(v: &[i8]) -> u64 {
v.len() as u64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_at_i8(v: &[i8], i: u64) -> i64 {
v.get(i as usize).copied().map_or(0, i64::from)
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_len_i16(v: &[i16]) -> u64 {
v.len() as u64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_at_i16(v: &[i16], i: u64) -> i64 {
v.get(i as usize).copied().map_or(0, i64::from)
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_max_i32(v: &[i32]) -> i64 {
v.iter().copied().max().unwrap_or(0) as i64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_max_i64(v: &[i64]) -> i64 {
v.iter().copied().max().unwrap_or(0)
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_min_i32(v: &[i32]) -> i64 {
v.iter().copied().min().unwrap_or(0) as i64
}
#[polydat::polydat_node(category = Arithmetic)]
fn vec_min_i64(v: &[i64]) -> i64 {
v.iter().copied().min().unwrap_or(0)
}
#[polydat::polydat_node(category = Comparison)]
fn vec_contains_i32(v: &[i32], x: i64) -> u64 {
u64::from(i32::try_from(x).is_ok_and(|x| v.contains(&x)))
}
#[polydat::polydat_node(category = Comparison)]
fn vec_contains_i64(v: &[i64], x: i64) -> u64 {
u64::from(v.contains(&x))
}
#[polydat::polydat_node(category = Comparison)]
fn vec_position_i32(v: &[i32], x: i64) -> u64 {
match i32::try_from(x) {
Ok(x) => v.iter().position(|e| *e == x).unwrap_or(v.len()) as u64,
Err(_) => v.len() as u64,
}
}
#[polydat::polydat_node(category = Comparison)]
fn vec_position_i64(v: &[i64], x: i64) -> u64 {
v.iter().position(|e| *e == x).unwrap_or(v.len()) as u64
}
#[polydat::polydat_node(category = Comparison)]
fn vec_count_below_i32(v: &[i32], x: i64) -> u64 {
v.iter().filter(|e| i64::from(**e) < x).count() as u64
}
#[polydat::polydat_node(category = Comparison)]
fn vec_count_below_i64(v: &[i64], x: i64) -> u64 {
v.iter().filter(|e| **e < x).count() as u64
}
#[polydat::polydat_node(category = Comparison)]
fn vec_intersect_count_i32(a: &[i32], b: &[i32]) -> u64 {
let set: std::collections::BTreeSet<i32> = b.iter().copied().collect();
let mine: std::collections::BTreeSet<i32> = a.iter().copied().collect();
mine.intersection(&set).count() as u64
}
#[polydat::polydat_node(category = Comparison)]
fn vec_intersect_count_i64(a: &[i64], b: &[i64]) -> u64 {
let set: std::collections::BTreeSet<i64> = b.iter().copied().collect();
let mine: std::collections::BTreeSet<i64> = a.iter().copied().collect();
mine.intersection(&set).count() as u64
}
#[polydat::polydat_node(category = Comparison)]
fn vec_set_eq_i32(a: &[i32], b: &[i32]) -> u64 {
let x: std::collections::BTreeSet<i32> = a.iter().copied().collect();
let y: std::collections::BTreeSet<i32> = b.iter().copied().collect();
u64::from(x == y)
}
#[polydat::polydat_node(category = Comparison)]
fn vec_set_eq_i64(a: &[i64], b: &[i64]) -> u64 {
let x: std::collections::BTreeSet<i64> = a.iter().copied().collect();
let y: std::collections::BTreeSet<i64> = b.iter().copied().collect();
u64::from(x == y)
}
fn same_by<T, K: PartialEq>(a: &[T], b: &[T], key: impl Fn(&T) -> K) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| key(x) == key(y))
}
#[polydat::polydat_node(category = Comparison)]
fn vec_eq_f16(a: &[polydat::half::f16], b: &[polydat::half::f16]) -> bool {
same_by(a, b, |x| x.to_bits())
}
#[polydat::polydat_node(category = Comparison)]
fn vec_eq_f32(a: &[f32], b: &[f32]) -> bool {
same_by(a, b, |x| x.to_bits())
}
#[polydat::polydat_node(category = Comparison)]
fn vec_eq_f64(a: &[f64], b: &[f64]) -> bool {
same_by(a, b, |x| x.to_bits())
}
#[polydat::polydat_node(category = Comparison)]
fn vec_eq_i8(a: &[i8], b: &[i8]) -> bool {
a == b
}
#[polydat::polydat_node(category = Comparison)]
fn vec_eq_i16(a: &[i16], b: &[i16]) -> bool {
a == b
}
#[polydat::polydat_node(category = Comparison)]
fn vec_eq_i32(a: &[i32], b: &[i32]) -> bool {
a == b
}
#[polydat::polydat_node(category = Comparison)]
fn vec_eq_i64(a: &[i64], b: &[i64]) -> bool {
a == b
}
#[polydat::polydat_node(category = Conversions)]
fn str_to_vec_i32(s: &str) -> Vec<i32> {
parse_int_array("str_to_vec_i32", s)
.into_iter()
.map(|n| {
i32::try_from(n).unwrap_or_else(|_| {
panic!(
"str_to_vec_i32: element {n} is outside i32 [{}, {}]",
i32::MIN,
i32::MAX
)
})
})
.collect()
}
#[polydat::polydat_node(category = Conversions)]
fn str_to_vec_i64(s: &str) -> Vec<i64> {
parse_int_array("str_to_vec_i64", s)
}
#[polydat::polydat_node(category = Conversions)]
fn str_to_vec_f64(s: &str) -> Vec<f64> {
parse_float_array("str_to_vec_f64", s)
}
#[polydat::polydat_node(category = Conversions)]
fn str_to_vec_f32(s: &str) -> Vec<f32> {
parse_float_array("str_to_vec_f32", s)
.into_iter()
.map(|x| {
let narrowed = x as f32;
if narrowed.is_infinite() {
panic!("str_to_vec_f32: element {x:?} is outside the f32 range");
}
narrowed
})
.collect()
}
fn parse_float_array(node: &str, s: &str) -> Vec<f64> {
let raw = s.trim();
let parsed: serde_json::Value = serde_json::from_str(raw)
.unwrap_or_else(|e| panic!("{node}: cannot parse {raw:?} as a JSON array: {e}"));
let arr = parsed
.as_array()
.unwrap_or_else(|| panic!("{node}: parsed JSON is not an array: {raw:?}"));
arr.iter()
.map(|j| {
j.as_f64()
.unwrap_or_else(|| panic!("{node}: element {j} is not a number in {raw:?}"))
})
.collect()
}
fn parse_int_array(node: &str, s: &str) -> Vec<i64> {
let raw = s.trim();
let parsed: serde_json::Value = serde_json::from_str(raw)
.unwrap_or_else(|e| panic!("{node}: cannot parse {raw:?} as a JSON array: {e}"));
let arr = parsed
.as_array()
.unwrap_or_else(|| panic!("{node}: parsed JSON is not an array: {raw:?}"));
arr.iter()
.map(|j| {
j.as_i64()
.unwrap_or_else(|| panic!("{node}: element {j} is not an integer in {raw:?}"))
})
.collect()
}