diff options
| author | jacksonfu14 <jackson@fashionunited.com> | 2025-11-06 07:50:44 +0100 |
|---|---|---|
| committer | Jackson Moore <jacksonmoore@tuta.io> | 2025-11-06 07:50:44 +0100 |
| commit | e4c5409397e6e833984d221d63a62ea7bcb2c4a9 (patch) | |
| tree | 9754d7a8b1a7f6185a0585c7a7a783f24326b2dd /morphology/src/value.rs | |
| parent | c20b1fe88b0ab1469300bd53be14faa6a5b917a0 (diff) | |
refactor: use cargo
Diffstat (limited to 'morphology/src/value.rs')
| -rw-r--r-- | morphology/src/value.rs | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/morphology/src/value.rs b/morphology/src/value.rs new file mode 100644 index 0000000..71fc2c6 --- /dev/null +++ b/morphology/src/value.rs @@ -0,0 +1,170 @@ +//! Core Data Structures for Meta-Affine Space +//! +//! A meta-affine space consists of 2-tuple values (outer, inner) derived from (0,0) +//! using five derivation rules: +//! +//! 1. (0,0) is a value in the space +//! 2. If (n,0) is a value, then (n+1,0) is also a value +//! 3. If (n,0) is a value, then (n-1,0) is also a value +//! 4. If (m,n) is a value, then (m,n+1) is also a value +//! 5. If (m,n) is a value, then (m,n-1) is also a value +//! +//! Every value can be generated via a minimum derivation path from (0,0). + +use std::fmt; + +/// Represents a value in meta-affine space as a 2-tuple (outer, inner). +/// +/// Values are copyable, comparable for equality, and can be used in sets and maps. +/// The Copy trait allows for efficient passing and immutable semantics. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct Value { + pub outer: i32, + pub inner: i32, +} + +impl Value { + /// Creates a new MetaAffine Value. + /// + /// # Examples + /// + /// ``` + /// use meta_affine::Value; + /// let v = Value::new(2, 3); + /// assert_eq!(v.outer, 2); + /// assert_eq!(v.inner, 3); + /// ``` + pub const fn new(outer: i32, inner: i32) -> Self { + Value { outer, inner } + } + + /// Returns the origin value (0, 0). + /// + /// # Examples + /// + /// ``` + /// use meta_affine::Value; + /// let origin = Value::origin(); + /// assert_eq!(origin, Value::new(0, 0)); + /// ``` + pub const fn origin() -> Self { + Value::new(0, 0) + } + + /// Checks if this is the origin value (0,0). + /// + /// # Examples + /// + /// ``` + /// use meta_affine::Value; + /// assert!(Value::new(0, 0).is_origin()); + /// assert!(!Value::new(1, 0).is_origin()); + /// ``` + pub const fn is_origin(&self) -> bool { + self.outer == 0 && self.inner == 0 + } + + /// Returns a tuple representation of the value. + pub const fn as_tuple(&self) -> (i32, i32) { + (self.outer, self.inner) + } +} + +impl fmt::Display for Value { + /// Formats the value as a string in the form "(outer,inner)". + /// + /// # Examples + /// + /// ``` + /// use meta_affine::Value; + /// let v = Value::new(2, -3); + /// assert_eq!(format!("{}", v), "(2,-3)"); + /// ``` + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "({},{})", self.outer, self.inner) + } +} + +/// Computes the minimum derivation path from (0,0) to the given value. +/// +/// The minimum derivation path consists of: +/// 1. Starting at (0,0) +/// 2. Applying rule 2 OR rule 3 (not both) |m| times to reach (m, 0) +/// 3. Applying rule 4 OR rule 5 (not both) |n| times to reach (m, n) +/// +/// # Examples +/// +/// ``` +/// use meta_affine::{Value, get_minimum_derivation_path}; +/// let path = get_minimum_derivation_path(Value::new(2, 3)); +/// assert_eq!(path.len(), 6); +/// assert_eq!(path[0], Value::new(0, 0)); +/// assert_eq!(path[5], Value::new(2, 3)); +/// ``` +pub fn get_minimum_derivation_path(value: Value) -> Vec<Value> { + let mut path = Vec::new(); + + // Start with origin + path.push(Value::origin()); + + // Generate outer path from (0,0) to (m,0) + let outer_path = generate_outer_path(value.outer); + path.extend(outer_path); + + // Generate inner path from (m,0) to (m,n) + let inner_path = generate_inner_path(value.outer, value.inner); + path.extend(inner_path); + + path +} + +/// Generate the path along the outer coordinate from (0,0) to (m,0). +/// Excludes the origin (0,0). +fn generate_outer_path(m: i32) -> Vec<Value> { + match m.cmp(&0) { + std::cmp::Ordering::Greater => { + // Apply rule 2: (n,0) -> (n+1,0) + (1..=m).map(|i| Value::new(i, 0)).collect() + } + std::cmp::Ordering::Less => { + // Apply rule 3: (n,0) -> (n-1,0) + // Generate -1, -2, ..., m + // Range m..0 gives [m, m+1, ..., -1], reversed gives [-1, -2, ..., m] + (m..0).rev().map(|i| Value::new(i, 0)).collect() + } + std::cmp::Ordering::Equal => Vec::new(), + } +} + +/// Generate the path along the inner coordinate from (m,0) to (m,n). +/// Excludes the starting point (m,0). +fn generate_inner_path(m: i32, n: i32) -> Vec<Value> { + match n.cmp(&0) { + std::cmp::Ordering::Greater => { + // Apply rule 4: (m,n) -> (m,n+1) + (1..=n).map(|i| Value::new(m, i)).collect() + } + std::cmp::Ordering::Less => { + // Apply rule 5: (m,n) -> (m,n-1) + // Generate -1, -2, ..., n + // Range n..0 gives [n, n+1, ..., -1], reversed gives [-1, -2, ..., n] + (n..0).rev().map(|i| Value::new(m, i)).collect() + } + std::cmp::Ordering::Equal => Vec::new(), + } +} + +/// Returns the length of the minimum derivation path for a given value. +/// +/// This is equivalent to |outer| + |inner| + 1 (for the origin). +/// +/// # Examples +/// +/// ``` +/// use meta_affine::{Value, path_length}; +/// assert_eq!(path_length(Value::new(2, 3)), 6); +/// assert_eq!(path_length(Value::new(-2, -3)), 6); +/// ``` +pub fn path_length(value: Value) -> usize { + (value.outer.abs() + value.inner.abs() + 1) as usize +} |