diff -r 1f19c6bbf07b -r 3868555d135c src/sets.rs --- a/src/sets.rs Sun Apr 27 20:29:43 2025 -0500 +++ b/src/sets.rs Fri May 15 14:46:30 2026 -0500 @@ -2,51 +2,57 @@ This module provides various sets and traits for them. */ -use std::ops::{RangeFull,RangeFrom,Range,RangeInclusive,RangeTo,RangeToInclusive}; -use crate::types::*; +use crate::euclidean::Euclidean; +use crate::instance::{BasicDecomposition, Instance, Space}; use crate::loc::Loc; -use crate::euclidean::Euclidean; -use crate::instance::{Space, Instance}; +use crate::types::*; use serde::Serialize; +use std::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}; pub mod cube; pub use cube::Cube; /// Trait for arbitrary sets. The parameter `U` is the element type. -pub trait Set where U : Space { +pub trait Set +where + U: Space, +{ /// Check for element containment - fn contains>(&self, item : I) -> bool; + fn contains>(&self, item: I) -> bool; } /// Additional ordering (besides [`PartialOrd`]) of a subfamily of sets: /// greatest lower bound and least upper bound. -pub trait SetOrd : Sized { - /// Returns the smallest set of same class contain both parameters. - fn common(&self, other : &Self) -> Self; +pub trait SetOrd: Sized { + /// Returns the smallest set of same class contain both parameters. + fn common(&self, other: &Self) -> Self; - /// Returns the greatest set of same class contaied by n both parameter sets. - fn intersect(&self, other : &Self) -> Option; + /// Returns the greatest set of same class contaied by n both parameter sets. + fn intersect(&self, other: &Self) -> Option; } -impl Set> -for Cube -where U : Num + PartialOrd + Sized { - fn contains>>(&self, item : I) -> bool { - self.0.iter().zip(item.ref_instance().iter()).all(|(s, x)| s.contains(x)) +impl Set> for Cube +where + U: Num + PartialOrd + Sized, +{ + fn contains>>(&self, item: I) -> bool { + item.eval_ref(|r| self.0.iter().zip(r.iter()).all(|(s, x)| s.contains(x))) } } -impl Set for RangeFull { - fn contains>(&self, _item : I) -> bool { true } +impl Set for RangeFull { + fn contains>(&self, _item: I) -> bool { + true + } } macro_rules! impl_ranges { ($($range:ident),*) => { $( impl Set for $range where - Idx : PartialOrd, - U : PartialOrd + Space, - Idx : PartialOrd + U : Space, + U::Principal : PartialOrd, + Idx : PartialOrd + PartialOrd, { #[inline] fn contains>(&self, item : I) -> bool { @@ -56,45 +62,56 @@ )* } } -impl_ranges!(RangeFrom,Range,RangeInclusive,RangeTo,RangeToInclusive); +impl_ranges!(RangeFrom, Range, RangeInclusive, RangeTo, RangeToInclusive); /// Halfspaces described by an orthogonal vector and an offset. /// /// The halfspace is $H = \\{ t v + a \mid a^⊤ v = 0 \\}$, where $v$ is the orthogonal /// vector and $t$ the offset. -#[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] -pub struct Halfspace where A : Euclidean, F : Float { - pub orthogonal : A, - pub offset : F, +#[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)] +pub struct Halfspace +where + A: Euclidean, + F: Float, +{ + pub orthogonal: A, + pub offset: F, } -impl Halfspace where A : Euclidean, F : Float { +impl Halfspace +where + A: Euclidean, + F: Float, +{ #[inline] - pub fn new(orthogonal : A, offset : F) -> Self { - Halfspace{ orthogonal : orthogonal, offset : offset } + pub fn new(orthogonal: A, offset: F) -> Self { + Halfspace { orthogonal: orthogonal, offset: offset } } } /// Trait for generating a halfspace spanned by another set `Self` of elements of type `U`. -pub trait SpannedHalfspace where F : Float { +pub trait SpannedHalfspace +where + F: Float, +{ /// Type of the orthogonal vector describing the halfspace. - type A : Euclidean; + type A: Euclidean; /// Returns the halfspace spanned by this set. fn spanned_halfspace(&self) -> Halfspace; } // TODO: Gram-Schmidt for higher N. -impl SpannedHalfspace for [Loc; 2] { - type A = Loc; +impl SpannedHalfspace for [Loc<1, F>; 2] { + type A = Loc<1, F>; fn spanned_halfspace(&self) -> Halfspace { let (x0, x1) = (self[0], self[1]); - Halfspace::new(x1-x0, x0[0]) + Halfspace::new(x1 - x0, x0[0]) } } // TODO: Gram-Schmidt for higher N. -impl SpannedHalfspace for [Loc; 2] { - type A = Loc; +impl SpannedHalfspace for [Loc<2, F>; 2] { + type A = Loc<2, F>; fn spanned_halfspace(&self) -> Halfspace { let (x0, x1) = (&self[0], &self[1]); let d = x1 - x0; @@ -104,29 +121,30 @@ } } -impl Set for Halfspace +impl Set for Halfspace where - A : Euclidean, - F : Float, + A: Euclidean, + F: Float, { #[inline] - fn contains>(&self, item : I) -> bool { + fn contains>(&self, item: I) -> bool { self.orthogonal.dot(item) >= self.offset } } /// Polygons defined by `N` `Halfspace`s. -#[derive(Clone,Copy,Debug,Eq,PartialEq)] -pub struct NPolygon(pub [Halfspace; N]) -where A : Euclidean, F : Float; - -impl Set for NPolygon +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct NPolygon(pub [Halfspace; N]) where - A : Euclidean, - F : Float, + A: Euclidean, + F: Float; + +impl Set for NPolygon +where + A: Euclidean, + F: Float, { - fn contains>(&self, item : I) -> bool { - let r = item.ref_instance(); - self.0.iter().all(|halfspace| halfspace.contains(r)) + fn contains>(&self, item: I) -> bool { + item.eval_ref(|r| self.0.iter().all(|halfspace| halfspace.contains(r))) } }