eryon-mem 0.0.4

this crate implements the memory-related aspects of the eryon framework
/*
    appellation: state <module>
    authors: @FL03
*/
use crate::MemoryPosition;

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(
    feature = "serde",
    derive(serde_derive::Deserialize, serde_derive::Serialize),
    serde(default, rename_all = "snake_case")
)]
#[repr(C)]
pub struct MemoryState {
    /// Current epoch of the memory system
    pub(crate) epoch: usize,
    /// Total number of features in the memory
    pub(crate) feature_count: usize,
    /// Total number of patterns recognized
    pub(crate) pattern_count: usize,
    /// a positional tracker for the memory system to maintain consistency
    pub(crate) position: MemoryPosition,
    /// Total number of relationships between features
    pub(crate) relationship_count: usize,
}

impl MemoryState {
    /// Creates a new `MemoryState` with default values.
    pub fn new() -> Self {
        MemoryState {
            epoch: 0,
            feature_count: 0,
            pattern_count: 0,
            position: MemoryPosition::zero(),
            relationship_count: 0,
        }
    }
    /// returns a copy of the current epoch
    pub const fn epoch(&self) -> usize {
        self.epoch
    }
    /// returns a mutable reference to the current epoch
    pub const fn epoch_mut(&mut self) -> &mut usize {
        &mut self.epoch
    }
    /// returns a copy of the current feature count
    pub const fn feature_count(&self) -> usize {
        self.feature_count
    }
    /// returns a mutable reference to the current feature count
    pub const fn feature_count_mut(&mut self) -> &mut usize {
        &mut self.feature_count
    }
    /// returns a copy of the current pattern count
    pub const fn pattern_count(&self) -> usize {
        self.pattern_count
    }
    /// returns a mutable reference to the current pattern count
    pub const fn pattern_count_mut(&mut self) -> &mut usize {
        &mut self.pattern_count
    }
    /// returns a reference to the current memory position
    pub const fn position(&self) -> &MemoryPosition {
        &self.position
    }
    /// returns a mutable reference to the current memory position
    pub const fn position_mut(&mut self) -> &mut MemoryPosition {
        &mut self.position
    }
    /// returns a copy of the current relationship count
    pub const fn relationship_count(&self) -> usize {
        self.relationship_count
    }
    /// returns a mutable reference to the current relationship count
    pub const fn relationship_count_mut(&mut self) -> &mut usize {
        &mut self.relationship_count
    }
    /// update the current epoch and return a mutable reference to the current state
    pub fn set_epoch(&mut self, epoch: usize) -> &mut Self {
        self.epoch = epoch;
        self
    }
    /// update the current feature count and return a mutable reference to the current state
    pub fn set_feature_count(&mut self, count: usize) -> &mut Self {
        self.feature_count = count;
        self
    }
    /// update the current pattern count and return a mutable reference to the current state
    pub fn set_pattern_count(&mut self, count: usize) -> &mut Self {
        self.pattern_count = count;
        self
    }
    /// update the current relationship count and return a mutable reference to the current
    /// state
    pub fn set_relationship_count(&mut self, count: usize) -> &mut Self {
        self.relationship_count = count;
        self
    }
    /// sets the current [`position`](MemoryPosition) to a new value and returns a mutable
    /// reference to the current state.
    pub fn set_position(&mut self, position: MemoryPosition) -> &mut Self {
        self.position = position;
        self
    }
    /// consumes the current instance to create another with the given epoch
    pub fn with_epoch(self, epoch: usize) -> Self {
        Self { epoch, ..self }
    }
    /// consumes the current instance to create another with the given feature count
    pub fn with_feature_count(self, count: usize) -> Self {
        Self {
            feature_count: count,
            ..self
        }
    }
    /// consumes the current instance to create another with the given pattern count
    pub fn with_pattern_count(self, count: usize) -> Self {
        Self {
            pattern_count: count,
            ..self
        }
    }
    /// consumes the current instance to create another with the given relationship count
    pub fn with_relationship_count(self, count: usize) -> Self {
        Self {
            relationship_count: count,
            ..self
        }
    }
    /// consumes the current instance to create another with the given position
    pub fn with_position(self, position: MemoryPosition) -> Self {
        Self { position, ..self }
    }

    pub const fn replace_epoch(&mut self, epoch: usize) -> usize {
        core::mem::replace(self.epoch_mut(), epoch)
    }

    /// returns the current epoch after replacing it with the next one
    pub fn next_epoch(&mut self) -> usize {
        self.replace_epoch(self.epoch() + 1)
    }
    /// increments the current feature index and returns the old value
    pub fn next_feature_id(&mut self) -> usize {
        self.position_mut().next_feature_id()
    }
    /// increments the current pattern index and returns the old value
    pub fn next_pattern_id(&mut self) -> usize {
        self.position_mut().next_pattern_id()
    }
}