# HG changeset patch # User Tuomo Valkonen # Date 1756923332 18000 # Node ID 20fa286377373d7362493e383629b64862deb803 # Parent 478c23ce7cefa969480b991926870d3c1071c999 todos diff -r 478c23ce7cef -r 20fa28637737 src/bisection_tree/btfn.rs --- 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 Space for BTFN diff -r 478c23ce7cef -r 20fa28637737 src/direct_product.rs --- 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 diff -r 478c23ce7cef -r 20fa28637737 src/instance.rs --- 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 +where + A: Ownable, + B: Ownable, +{ + 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 where T : Clone); + +impl<'a, T: Clone> Ownable for &'a [T] { + type OwnedVariant = Vec; + + #[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) } } }; diff -r 478c23ce7cef -r 20fa28637737 src/loc.rs --- 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 Ownable for Loc { - 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 where const N: usize, F: Copy); impl Display for Loc { // Required method diff -r 478c23ce7cef -r 20fa28637737 src/nalgebra_support.rs --- 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()) } }