chiark / gitweb /
Allow FiniteNimber based on a slice.
authorSimon Tatham <anakin@pobox.com>
Thu, 10 Apr 2025 11:49:34 +0000 (12:49 +0100)
committerSimon Tatham <anakin@pobox.com>
Thu, 10 Apr 2025 11:49:34 +0000 (12:49 +0100)
This should make it easier to partition things.

.rustfmt.toml [new file with mode: 0644]
src/finitenimber.rs
src/lib.rs

diff --git a/.rustfmt.toml b/.rustfmt.toml
new file mode 100644 (file)
index 0000000..a1ffd27
--- /dev/null
@@ -0,0 +1 @@
+max_width = 79
index 5fa9852fcbde958700f1240fd996571e709ed8b1..828ee644d14fd42211a46033616b669e301122c4 100644 (file)
@@ -1,8 +1,10 @@
-use std::ops::BitXor;
 use std::fmt::Debug;
+use std::ops::BitXor;
 
-pub trait FiniteNimberBase : BitXor<Output=Self> + Clone + Eq + Debug + Sized {
-    type Owned: FiniteNimberBase;
+pub trait FiniteNimberBase:
+    BitXor<Output = Self::Owned> + Eq + Debug + Sized
+{
+    type Owned: FiniteNimberBase + Clone;
 
     fn to_owned(self) -> Self::Owned;
 }
@@ -10,24 +12,59 @@ pub trait FiniteNimberBase : BitXor<Output=Self> + Clone + Eq + Debug + Sized {
 impl FiniteNimberBase for u64 {
     type Owned = Self;
 
-    fn to_owned(self) -> Self::Owned {self}
+    fn to_owned(self) -> Self::Owned {
+        self
+    }
 }
 
 #[derive(Clone, PartialEq, Eq, Debug)]
 pub struct IntVector(pub Vec<u64>);
 
+#[derive(PartialEq, Eq, Debug)]
+pub struct IntSlice<'a>(pub &'a [u64]);
+
 impl BitXor for IntVector {
     type Output = Self;
 
     fn bitxor(self, other: Self) -> Self {
-        Self(self.0.into_iter().zip(other.0.into_iter()).map(|(a,b)| a ^ b).collect())
+        Self(
+            self.0
+                .into_iter()
+                .zip(other.0.into_iter())
+                .map(|(a, b)| a ^ b)
+                .collect(),
+        )
+    }
+}
+
+impl BitXor for IntSlice<'_> {
+    type Output = IntVector;
+
+    fn bitxor(self, other: Self) -> IntVector {
+        IntVector(
+            self.0
+                .iter()
+                .zip(other.0.iter())
+                .map(|(a, b)| a ^ b)
+                .collect(),
+        )
     }
 }
 
 impl FiniteNimberBase for IntVector {
     type Owned = Self;
 
-    fn to_owned(self) -> Self::Owned {self}
+    fn to_owned(self) -> Self::Owned {
+        self
+    }
+}
+
+impl FiniteNimberBase for IntSlice<'_> {
+    type Owned = IntVector;
+
+    fn to_owned(self) -> Self::Owned {
+        IntVector(self.0.into())
+    }
 }
 
 #[derive(Clone, PartialEq, Eq, Debug)]
@@ -45,6 +82,6 @@ impl<T: FiniteNimberBase> std::ops::Add<FiniteNimber<T>> for FiniteNimber<T> {
     type Output = FiniteNimber<T::Owned>;
 
     fn add(self, other: Self) -> Self::Output {
-        Self::Output::new((self.n ^ other.n).to_owned())
+        Self::Output::new(self.n ^ other.n)
     }
 }
index 496d05375975206fc71f8a981613041483bdea2e..d40eed06ef89e57fc16cb43e422f2662b040ea6d 100644 (file)
@@ -1,6 +1,7 @@
 mod finitenimber;
 
 pub use finitenimber::FiniteNimber;
+pub use finitenimber::IntSlice;
 pub use finitenimber::IntVector;
 
 #[cfg(test)]
@@ -16,5 +17,9 @@ mod tests {
         let a = FiniteNimber::new(IntVector(vec![1, 1, 0, 0]));
         let b = FiniteNimber::new(IntVector(vec![1, 0, 1, 0]));
         assert_eq!(a + b, FiniteNimber::new(IntVector(vec![0, 1, 1, 0])));
+
+        let a = FiniteNimber::new(IntSlice(&[1, 1, 0, 0]));
+        let b = FiniteNimber::new(IntSlice(&[1, 0, 1, 0]));
+        assert_eq!(a + b, FiniteNimber::new(IntVector(vec![0, 1, 1, 0])));
     }
 }