lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! Original ordered matrix variables.

use std::sync::Arc;

use super::RawKey;
use crate::{
    errors::{Diagnostic, DiagnosticKind, DiagnosticReference, ErrorContext, OperationPhase},
    Error, NumericValues, Result,
};

/// One actual variable and its original scientific identity.
#[derive(Debug, Clone)]
pub struct NamedNumericValues {
    /// Complete original variable key.
    pub key: RawKey,
    /// Original numeric column.
    pub values: NumericValues,
}

/// The actual ordered variable declarations of a matrix.
#[derive(Debug, Clone)]
pub struct VariableValues(Arc<[NamedNumericValues]>);

impl VariableValues {
    pub(crate) fn from_columns(values: Vec<NumericValues>) -> Self {
        Self(
            values
                .into_iter()
                .map(|values| {
                    let key = values
                        .field_key()
                        .cloned()
                        .unwrap_or_else(|| RawKey::from(values.name()));
                    NamedNumericValues { key, values }
                })
                .collect(),
        )
    }

    /// The actual ordered variables.
    pub fn variables(&self) -> &[NamedNumericValues] {
        &self.0
    }

    /// Read a variable by original key, rejecting duplicate declarations.
    pub fn get(&self, key: &RawKey) -> Result<&NumericValues> {
        let matches: Vec<_> = self
            .0
            .iter()
            .enumerate()
            .filter(|(_, variable)| &variable.key == key)
            .collect();
        let context = ErrorContext::new()
            .with_operation(OperationPhase::Check)
            .with_binding(key.clone());
        match matches.as_slice() {
            [] => Err(Error::Data(
                Diagnostic::new(
                    DiagnosticKind::UnknownKey {
                        key: Some(key.clone()),
                    },
                    format!("no matrix variable {key:?}"),
                )
                .with_context(context),
            )),
            [(_, value)] => Ok(&value.values),
            _ => Err(Error::Data(
                Diagnostic::new(
                    DiagnosticKind::Ambiguous {
                        candidates: matches
                            .into_iter()
                            .map(|(slot, _)| DiagnosticReference::Member {
                                domain: None,
                                address: super::MemberAddress::Slot(slot),
                            })
                            .collect(),
                    },
                    format!("ambiguous matrix variable {key:?}"),
                )
                .with_context(context),
            )),
        }
    }

    /// Read a variable by its actual declaration position.
    pub fn get_at(&self, index: usize) -> Option<&NamedNumericValues> {
        self.0.get(index)
    }

    /// Number of variables, independent of observation count.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Whether the declared variable collection is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub(crate) fn columns(&self) -> impl ExactSizeIterator<Item = &NumericValues> {
        self.0.iter().map(|variable| &variable.values)
    }
}