#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
pub(crate) use crate::model::{XqmxModel, XqmxSample};
#[derive(Debug, Clone, PartialEq)]
pub enum RegVal {
Unset,
Int(i64),
VecInt(Vec<i64>),
VecXqmx(Vec<XqmxModel>),
Model(XqmxModel),
Sample(XqmxSample),
}
impl Default for RegVal {
fn default() -> Self {
Self::Unset
}
}
impl RegVal {
pub(crate) fn as_int(&self) -> Result<i64, &'static str> {
match self {
Self::Int(n) => Ok(*n),
Self::Unset => Err("unset"),
Self::VecInt(_) => Err("vec<int>"),
Self::VecXqmx(_) => Err("vec<xqmx>"),
Self::Model(_) => Err("model"),
Self::Sample(_) => Err("sample"),
}
}
pub(crate) fn as_model_mut(&mut self) -> Result<&mut XqmxModel, &'static str> {
match self {
Self::Model(m) => Ok(m),
Self::Unset => Err("unset"),
Self::Int(_) => Err("int"),
Self::VecInt(_) => Err("vec<int>"),
Self::VecXqmx(_) => Err("vec<xqmx>"),
Self::Sample(_) => Err("sample"),
}
}
pub(crate) fn as_model(&self) -> Result<&XqmxModel, &'static str> {
match self {
Self::Model(m) => Ok(m),
Self::Unset => Err("unset"),
Self::Int(_) => Err("int"),
Self::VecInt(_) => Err("vec<int>"),
Self::VecXqmx(_) => Err("vec<xqmx>"),
Self::Sample(_) => Err("sample"),
}
}
pub(crate) fn as_vec_int_mut(&mut self) -> Result<&mut Vec<i64>, &'static str> {
match self {
Self::VecInt(v) => Ok(v),
Self::Unset => Err("unset"),
Self::Int(_) => Err("int"),
Self::VecXqmx(_) => Err("vec<xqmx>"),
Self::Model(_) => Err("model"),
Self::Sample(_) => Err("sample"),
}
}
pub(crate) fn as_vec_int(&self) -> Result<&Vec<i64>, &'static str> {
match self {
Self::VecInt(v) => Ok(v),
Self::Unset => Err("unset"),
Self::Int(_) => Err("int"),
Self::VecXqmx(_) => Err("vec<xqmx>"),
Self::Model(_) => Err("model"),
Self::Sample(_) => Err("sample"),
}
}
pub(crate) fn type_name(&self) -> &'static str {
match self {
Self::Unset => "unset",
Self::Int(_) => "int",
Self::VecInt(_) => "vec<int>",
Self::VecXqmx(_) => "vec<xqmx>",
Self::Model(_) => "model",
Self::Sample(_) => "sample",
}
}
pub(crate) fn as_xqmx_grid(&self) -> Result<XqmxGridRef<'_>, &'static str> {
match self {
Self::Model(m) => Ok(XqmxGridRef::Model(m)),
Self::Sample(s) => Ok(XqmxGridRef::Sample(s)),
Self::Unset => Err("unset"),
Self::Int(_) => Err("int"),
Self::VecInt(_) => Err("vec<int>"),
Self::VecXqmx(_) => Err("vec<xqmx>"),
}
}
pub(crate) fn as_xqmx_grid_mut(&mut self) -> Result<XqmxGridRefMut<'_>, &'static str> {
match self {
Self::Model(m) => Ok(XqmxGridRefMut::Model(m)),
Self::Sample(s) => Ok(XqmxGridRefMut::Sample(s)),
Self::Unset => Err("unset"),
Self::Int(_) => Err("int"),
Self::VecInt(_) => Err("vec<int>"),
Self::VecXqmx(_) => Err("vec<xqmx>"),
}
}
}
pub(crate) enum XqmxGridRef<'a> {
Model(&'a XqmxModel),
Sample(&'a XqmxSample),
}
impl XqmxGridRef<'_> {
pub(crate) fn rows(&self) -> usize {
match self {
Self::Model(m) => m.rows,
Self::Sample(s) => s.rows,
}
}
pub(crate) fn cols(&self) -> usize {
match self {
Self::Model(m) => m.cols,
Self::Sample(s) => s.cols,
}
}
pub(crate) fn size(&self) -> usize {
match self {
Self::Model(m) => m.size,
Self::Sample(s) => s.values.len(),
}
}
pub(crate) fn linear(&self, idx: usize) -> i64 {
match self {
Self::Model(m) => m.get_linear(idx),
Self::Sample(s) => s.values.get(idx).copied().unwrap_or(0),
}
}
}
pub(crate) enum XqmxGridRefMut<'a> {
Model(&'a mut XqmxModel),
Sample(&'a mut XqmxSample),
}
impl XqmxGridRefMut<'_> {
pub(crate) fn set_grid(&mut self, rows: usize, cols: usize) {
match self {
Self::Model(m) => {
m.rows = rows;
m.cols = cols;
}
Self::Sample(s) => {
s.rows = rows;
s.cols = cols;
}
}
}
pub(crate) fn size(&self) -> usize {
match self {
Self::Model(m) => m.size,
Self::Sample(s) => s.values.len(),
}
}
pub(crate) fn linear_set(&mut self, idx: usize, val: i64) {
match self {
Self::Model(m) => m.set_linear(idx, val),
Self::Sample(s) => {
if let Some(slot) = s.values.get_mut(idx) {
*slot = val;
}
}
}
}
pub(crate) fn linear_add(&mut self, idx: usize, delta: i64) {
match self {
Self::Model(m) => m.add_linear(idx, delta),
Self::Sample(s) => {
if let Some(slot) = s.values.get_mut(idx) {
*slot += delta;
}
}
}
}
}