summaryrefslogtreecommitdiff
path: root/morphology/meta_affine/value.rs
blob: 71fc2c698bc856dd7144c4e35abbc411daceac82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
}