todos dev

Wed, 03 Sep 2025 13:15:32 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 03 Sep 2025 13:15:32 -0500
branch
dev
changeset 166
20fa28637737
parent 165
478c23ce7cef
child 167
effb80efba09

todos

src/bisection_tree/btfn.rs file | annotate | diff | comparison | revisions
src/direct_product.rs file | annotate | diff | comparison | revisions
src/instance.rs file | annotate | diff | comparison | revisions
src/loc.rs file | annotate | diff | comparison | revisions
src/nalgebra_support.rs file | annotate | diff | comparison | revisions
--- a/src/bisection_tree/btfn.rs	Wed Sep 03 12:55:27 2025 -0500
+++ b/src/bisection_tree/btfn.rs	Wed Sep 03 13:15:32 2025 -0500
@@ -58,6 +58,13 @@
     {
         MyCow::Owned(self)
     }
+
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        MyCow::Borrowed(self)
+    }
 }
 
 impl<F: Float, G, BT, const N: usize> Space for BTFN<F, G, BT, N>
--- a/src/direct_product.rs	Wed Sep 03 12:55:27 2025 -0500
+++ b/src/direct_product.rs	Wed Sep 03 13:15:32 2025 -0500
@@ -287,6 +287,14 @@
     {
         MyCow::Owned(self.into_owned())
     }
+
+    #[inline]
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        MyCow::Owned(self.clone_owned())
+    }
 }
 
 /// We only support 'closed' `Euclidean` `Pair`s, as more general ones cause
--- a/src/instance.rs	Wed Sep 03 12:55:27 2025 -0500
+++ b/src/instance.rs	Wed Sep 03 13:15:32 2025 -0500
@@ -38,6 +38,11 @@
     fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
     where
         Self: 'b;
+
+    /// Returns an owned instance or a reference to one.
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b;
 }
 
 impl<'a, X: Ownable> Ownable for &'a X {
@@ -45,12 +50,12 @@
 
     #[inline]
     fn into_owned(self) -> Self::OwnedVariant {
-        self.clone_owned()
+        X::clone_owned(self)
     }
 
     #[inline]
     fn clone_owned(&self) -> Self::OwnedVariant {
-        (*self).into_owned()
+        X::clone_owned(self)
     }
 
     #[inline]
@@ -58,7 +63,14 @@
     where
         Self: 'b,
     {
-        todo!();
+        X::ref_cow_owned(self)
+    }
+
+    fn ref_cow_owned<'b>(&self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        X::ref_cow_owned(self)
     }
 }
 
@@ -67,12 +79,12 @@
 
     #[inline]
     fn into_owned(self) -> Self::OwnedVariant {
-        self.clone_owned()
+        X::clone_owned(self)
     }
 
     #[inline]
     fn clone_owned(&self) -> Self::OwnedVariant {
-        (&**self).into_owned()
+        X::clone_owned(self)
     }
 
     #[inline]
@@ -80,35 +92,124 @@
     where
         Self: 'b,
     {
-        todo!();
+        X::ref_cow_owned(self)
+    }
+
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        X::ref_cow_owned(self)
     }
 }
 
-impl<'a, X: Ownable> Ownable for MyCow<'a, X> {
-    type OwnedVariant = X::OwnedVariant;
+impl<'a, A, B> Ownable for EitherDecomp<A, B>
+where
+    A: Ownable,
+    B: Ownable<OwnedVariant = A::OwnedVariant>,
+{
+    type OwnedVariant = A::OwnedVariant;
 
     #[inline]
     fn into_owned(self) -> Self::OwnedVariant {
         match self {
-            EitherDecomp::Owned(x) => x.into_owned(),
-            EitherDecomp::Borrowed(x) => x.into_owned(),
+            EitherDecomp::Owned(a) => A::into_owned(a),
+            EitherDecomp::Borrowed(b) => B::into_owned(b),
         }
     }
 
     #[inline]
     fn clone_owned(&self) -> Self::OwnedVariant {
         match self {
-            EitherDecomp::Owned(x) => x.into_owned(),
-            EitherDecomp::Borrowed(x) => x.into_owned(),
+            EitherDecomp::Owned(a) => A::clone_owned(a),
+            EitherDecomp::Borrowed(b) => B::clone_owned(b),
+        }
+    }
+
+    #[inline]
+    fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        A: 'b,
+        B: 'b,
+    {
+        match self {
+            EitherDecomp::Owned(a) => A::cow_owned(a),
+            EitherDecomp::Borrowed(b) => B::cow_owned(b),
+        }
+    }
+
+    #[inline]
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        match self {
+            EitherDecomp::Owned(a) => A::ref_cow_owned(a),
+            EitherDecomp::Borrowed(b) => B::ref_cow_owned(b),
         }
     }
+}
+
+#[macro_export]
+macro_rules! self_ownable {
+    ($type:ty where $($qual:tt)*) => {
+        impl<$($qual)*> $crate::instance::Ownable for $type {
+            type OwnedVariant = Self;
+
+            #[inline]
+            fn into_owned(self) -> Self::OwnedVariant {
+                self
+            }
+
+            fn clone_owned(&self) -> Self::OwnedVariant {
+                self.clone()
+            }
+
+            fn cow_owned<'b>(self) -> $crate::instance::MyCow<'b, Self::OwnedVariant>
+            where
+                Self: 'b,
+            {
+                $crate::instance::MyCow::Owned(self)
+            }
+
+            fn ref_cow_owned<'b>(&'b self) -> $crate::instance::MyCow<'b, Self::OwnedVariant>
+            where
+                Self: 'b,
+            {
+                $crate::instance::MyCow::Borrowed(self)
+            }
+        }
+    };
+}
+
+self_ownable!(Vec<T> where T : Clone);
+
+impl<'a, T: Clone> Ownable for &'a [T] {
+    type OwnedVariant = Vec<T>;
+
+    #[inline]
+    fn into_owned(self) -> Self::OwnedVariant {
+        Vec::from(self)
+    }
+
+    #[inline]
+    fn clone_owned(&self) -> Self::OwnedVariant {
+        Vec::from(*self)
+    }
 
     #[inline]
     fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
     where
         Self: 'b,
     {
-        todo!();
+        MyCow::Owned(Vec::from(self))
+    }
+
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        MyCow::Owned(Vec::from(*self))
     }
 }
 
@@ -161,7 +262,12 @@
 
             #[inline]
             fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b {
-                todo!();
+                MyCow::Owned(self)
+            }
+
+            #[inline]
+            fn ref_cow_owned<'b>(&self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b {
+                MyCow::Owned(*self)
             }
         }
     };
--- a/src/loc.rs	Wed Sep 03 12:55:27 2025 -0500
+++ b/src/loc.rs	Wed Sep 03 13:15:32 2025 -0500
@@ -4,11 +4,12 @@
 */
 
 use crate::euclidean::*;
-use crate::instance::{BasicDecomposition, Instance, MyCow, Ownable};
+use crate::instance::{BasicDecomposition, Instance};
 use crate::linops::{Linear, Mapping, VectorSpace, AXPY};
 use crate::mapping::Space;
 use crate::maputil::{map1, map1_mut, map2, map2_mut, FixedLength, FixedLengthMut};
 use crate::norms::*;
+use crate::self_ownable;
 use crate::types::{Float, Num, SignedNum};
 use serde::ser::{Serialize, SerializeSeq, Serializer};
 use std::fmt::{Display, Formatter};
@@ -27,28 +28,7 @@
     pub [F; N],
 );
 
-/// Trait for ownable-by-consumption objects
-impl<const N: usize, F: Copy> Ownable for Loc<N, F> {
-    type OwnedVariant = Self;
-
-    #[inline]
-    fn into_owned(self) -> Self::OwnedVariant {
-        self
-    }
-
-    /// Returns an owned instance of a reference.
-    fn clone_owned(&self) -> Self::OwnedVariant {
-        self.clone()
-    }
-
-    /// Returns an owned instance of a reference.
-    fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
-    where
-        Self: 'b,
-    {
-        MyCow::Owned(self)
-    }
-}
+self_ownable!(Loc<N, F> where const N: usize, F: Copy);
 
 impl<F: Display, const N: usize> Display for Loc<N, F> {
     // Required method
--- a/src/nalgebra_support.rs	Wed Sep 03 12:55:27 2025 -0500
+++ b/src/nalgebra_support.rs	Wed Sep 03 13:15:32 2025 -0500
@@ -48,8 +48,14 @@
     where
         Self: 'b,
     {
-        todo!()
-        //MyCow::owned(self.into_owned())
+        MyCow::Owned(self.into_owned())
+    }
+
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        MyCow::Owned(self.clone_owned())
     }
 }
 

mercurial