use burn::prelude::ElementConversion;
use burn::tensor::backend::Backend;
use burn::tensor::{Int, Tensor};
pub(crate) fn rows_to_tensor<B: Backend>(
rows: &[Vec<isize>],
n: usize,
device: &B::Device,
) -> Tensor<B, 2, Int> {
let data: Vec<i64> = rows
.iter()
.flat_map(|row| row.iter().map(|&v| v as i64))
.collect();
Tensor::<B, 1, Int>::from_data(data.as_slice(), device).reshape([rows.len(), n])
}
pub(crate) fn to_rows<B: Backend>(
assignments: &Tensor<B, 2, Int>,
p: usize,
n: usize,
) -> Vec<Vec<isize>> {
let flat: Vec<i64> = assignments
.clone()
.into_data()
.to_vec::<B::IntElem>()
.expect("assignment tensor should be integer")
.into_iter()
.map(|v| v.elem::<i64>())
.collect();
(0..p)
.map(|i| {
flat[i * n..(i + 1) * n]
.iter()
.map(|&v| v as isize)
.collect()
})
.collect()
}