//! 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) } /// Returns a pretty-printed representation of the value. /// /// For a value (outer, inner): /// - If outer is positive, inner is nested in outer pairs of parentheses /// - If outer is negative, inner is nested in |outer| pairs of square brackets /// - If outer is 0, returns just the inner value /// /// # Examples /// /// ``` /// use meta_affine::Value; /// assert_eq!(Value::new(0, 0).pretty_print(), "0"); /// assert_eq!(Value::new(0, 1).pretty_print(), "1"); /// assert_eq!(Value::new(1, 0).pretty_print(), "(0)"); /// assert_eq!(Value::new(-1, 0).pretty_print(), "[0]"); /// assert_eq!(Value::new(2, 1).pretty_print(), "((1))"); /// assert_eq!(Value::new(-3, 2).pretty_print(), "[[[2]]]"); /// ``` pub fn pretty_print(&self) -> String { let mut result = self.inner.to_string(); if self.outer > 0 { // Positive outer: nest in parentheses for _ in 0..self.outer { result = format!("({})", result); } } else if self.outer < 0 { // Negative outer: nest in square brackets for _ in 0..self.outer.abs() { result = format!("[{}]", result); } } // If outer == 0, just return the inner value as-is result } } 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 { 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 { 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 { 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 }