chiark / gitweb /
Allow variable-sized FiniteNimber.
authorSimon Tatham <anakin@pobox.com>
Thu, 10 Apr 2025 11:41:43 +0000 (12:41 +0100)
committerSimon Tatham <anakin@pobox.com>
Thu, 10 Apr 2025 11:41:43 +0000 (12:41 +0100)
src/finitenimber.rs
src/lib.rs

index 9b544f6d0fcbcd24d1a7211c039be3e72841d091..5fa9852fcbde958700f1240fd996571e709ed8b1 100644 (file)
@@ -1,6 +1,7 @@
+use std::ops::BitXor;
 use std::fmt::Debug;
 
-pub trait FiniteNimberBase : std::ops::BitXor<Output=Self> + Clone + Eq + Debug + Sized {
+pub trait FiniteNimberBase : BitXor<Output=Self> + Clone + Eq + Debug + Sized {
     type Owned: FiniteNimberBase;
 
     fn to_owned(self) -> Self::Owned;
@@ -12,6 +13,23 @@ impl FiniteNimberBase for u64 {
     fn to_owned(self) -> Self::Owned {self}
 }
 
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct IntVector(pub Vec<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())
+    }
+}
+
+impl FiniteNimberBase for IntVector {
+    type Owned = Self;
+
+    fn to_owned(self) -> Self::Owned {self}
+}
+
 #[derive(Clone, PartialEq, Eq, Debug)]
 pub struct FiniteNimber<T: FiniteNimberBase> {
     n: T,
index dcc764dd3227b51ffd7fe7b0cb314d8c8660d6ad..496d05375975206fc71f8a981613041483bdea2e 100644 (file)
@@ -1,6 +1,7 @@
 mod finitenimber;
 
 pub use finitenimber::FiniteNimber;
+pub use finitenimber::IntVector;
 
 #[cfg(test)]
 mod tests {
@@ -11,5 +12,9 @@ mod tests {
         let a = FiniteNimber::new(0b1100u64);
         let b = FiniteNimber::new(0b1010u64);
         assert_eq!(a + b, FiniteNimber::new(0b0110u64));
+
+        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])));
     }
 }