From 0dd79ca9951f30d68db7714036ae4ee32b99ed1e Mon Sep 17 00:00:00 2001 From: ppraneth Date: Fri, 9 Oct 2026 09:31:09 +0530 Subject: [PATCH 1/2] move Signature and its helper traits to signature.rs and fix a broken doc link --- src/lib.rs | 8 +++-- src/mapping.rs | 87 ++-------------------------------------------- src/signature.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 88 deletions(-) create mode 100644 src/signature.rs diff --git a/src/lib.rs b/src/lib.rs index ca036ab..c13d168 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ pub mod display; pub mod domain; pub mod functions; pub mod mapping; +pub mod signature; pub mod tensor; pub use display::{BoxedTensor, format_boxed}; @@ -15,10 +16,11 @@ pub use functions::{ zeros, }; pub use mapping::{ - BoxedMapping, BoxedPowersetMapping, COO, Concat, Diagonal, EmptyMapping, HRectSignature, - IndexMapping, InvProjection, Permutation, Powerset, PowersetMapping, Projection, Signature, - Squeeze, Stride, Unsqueeze, Where, + BoxedMapping, BoxedPowersetMapping, COO, Concat, Diagonal, EmptyMapping, IndexMapping, + InvProjection, Permutation, Powerset, PowersetMapping, Projection, Squeeze, Stride, Unsqueeze, + Where, }; +pub use signature::{HRectSignature, Signature}; pub use tensor::{ Inf, InfReduction, JaggedTensor, LeftComposition, Physical, Product, ProductReduction, Reduction, ReductionTensor, RightComposition, Sum, SumReduction, Sup, SupReduction, Tensor, diff --git a/src/mapping.rs b/src/mapping.rs index 8619aaf..faaefb4 100644 --- a/src/mapping.rs +++ b/src/mapping.rs @@ -3,6 +3,7 @@ use std::rc::Rc; use crate::display::BoxedTensor; use crate::domain::{Domain, HRect, JaggedDomain}; +use crate::signature::{HRectMapping, HRectPowersetMapping, HRectSignature, Signature}; /// A set of indices, as returned by a `PowersetMapping`. pub type Powerset = HashSet>; @@ -39,90 +40,6 @@ pub trait BoxedPowersetMapping: PowersetMapping { fn hrect(&self) -> &HRect; } -/// The domain and codomain a mapping runs between. -pub struct Signature { - domain: D, - codomain: C, -} - -impl Signature { - pub fn new(domain: D, codomain: C) -> Self { - Self { domain, codomain } - } - - pub fn domain(&self) -> &D { - &self.domain - } - - pub fn codomain(&self) -> &C { - &self.codomain - } -} - -/// The signature of a mapping between two hyperrectangles. -pub type HRectSignature = Signature; - -/// What a mapping from a hyperrectangle has to provide. Implementing this rather -/// than `IndexMapping` gets the domain accessors and printability below for -/// free, whatever the codomain; a mapping over a domain that is not a hrect -/// implements `IndexMapping` directly instead, and is not printable. -pub trait HRectMapping { - type Codomain: Domain; - - fn map(&self, index: &[usize]) -> Vec; - - fn signature(&self) -> &Signature; -} - -impl IndexMapping for M { - fn map(&self, index: &[usize]) -> Vec { - HRectMapping::map(self, index) - } - - fn domain(&self) -> &dyn Domain { - self.signature().domain() - } - - fn codomain(&self) -> &dyn Domain { - self.signature().codomain() - } -} - -impl BoxedMapping for M { - fn hrect(&self) -> &HRect { - self.signature().domain() - } -} - -/// The powerset counterpart of `HRectMapping`. -pub trait HRectPowersetMapping { - type Codomain: Domain; - - fn map(&self, index: &[usize]) -> Powerset; - - fn signature(&self) -> &Signature; -} - -impl PowersetMapping for M { - fn map(&self, index: &[usize]) -> Powerset { - HRectPowersetMapping::map(self, index) - } - - fn domain(&self) -> &dyn Domain { - self.signature().domain() - } - - fn codomain(&self) -> &dyn Domain { - self.signature().codomain() - } -} - -impl BoxedPowersetMapping for M { - fn hrect(&self) -> &HRect { - self.signature().domain() - } -} - pub struct Stride { signature: HRectSignature, strides: Vec, @@ -557,7 +474,7 @@ impl HRectMapping for Projection { } /// The inverse of the projection dropping axis `dim`. -/// e.g. with dim=1, f(m, p) = {[m n p]^T | n in [N]} +/// e.g. with dim=1, f(m, p) = {[m n p]^T | n in \[N\]} pub struct InvProjection { signature: HRectSignature, dim: usize, diff --git a/src/signature.rs b/src/signature.rs new file mode 100644 index 0000000..f6ef957 --- /dev/null +++ b/src/signature.rs @@ -0,0 +1,89 @@ +//! The domain and codomain a mapping runs between, and the traits through which +//! a mapping from a hrect gets its accessors and printability from them. + +use crate::domain::{Domain, HRect}; +use crate::mapping::{BoxedMapping, BoxedPowersetMapping, IndexMapping, Powerset, PowersetMapping}; + +/// The domain and codomain a mapping runs between. +pub struct Signature { + domain: D, + codomain: C, +} + +impl Signature { + pub fn new(domain: D, codomain: C) -> Self { + Self { domain, codomain } + } + + pub fn domain(&self) -> &D { + &self.domain + } + + pub fn codomain(&self) -> &C { + &self.codomain + } +} + +/// The signature of a mapping between two hyperrectangles. +pub type HRectSignature = Signature; + +/// What a mapping from a hyperrectangle has to provide. Implementing this rather +/// than `IndexMapping` gets the domain accessors and printability below for +/// free, whatever the codomain; a mapping over a domain that is not a hrect +/// implements `IndexMapping` directly instead, and is not printable. +pub trait HRectMapping { + type Codomain: Domain; + + fn map(&self, index: &[usize]) -> Vec; + + fn signature(&self) -> &Signature; +} + +impl IndexMapping for M { + fn map(&self, index: &[usize]) -> Vec { + HRectMapping::map(self, index) + } + + fn domain(&self) -> &dyn Domain { + self.signature().domain() + } + + fn codomain(&self) -> &dyn Domain { + self.signature().codomain() + } +} + +impl BoxedMapping for M { + fn hrect(&self) -> &HRect { + self.signature().domain() + } +} + +/// The powerset counterpart of `HRectMapping`. +pub trait HRectPowersetMapping { + type Codomain: Domain; + + fn map(&self, index: &[usize]) -> Powerset; + + fn signature(&self) -> &Signature; +} + +impl PowersetMapping for M { + fn map(&self, index: &[usize]) -> Powerset { + HRectPowersetMapping::map(self, index) + } + + fn domain(&self) -> &dyn Domain { + self.signature().domain() + } + + fn codomain(&self) -> &dyn Domain { + self.signature().codomain() + } +} + +impl BoxedPowersetMapping for M { + fn hrect(&self) -> &HRect { + self.signature().domain() + } +} From 05314148f9a32776d3f5bb96215d44acf84a6dcf Mon Sep 17 00:00:00 2001 From: ppraneth Date: Fri, 9 Oct 2026 19:13:21 +0530 Subject: [PATCH 2/2] keep the helper mapping traits in mapping.rs --- src/mapping.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++- src/signature.rs | 65 +----------------------------------------------- 2 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/mapping.rs b/src/mapping.rs index faaefb4..e05a246 100644 --- a/src/mapping.rs +++ b/src/mapping.rs @@ -3,7 +3,7 @@ use std::rc::Rc; use crate::display::BoxedTensor; use crate::domain::{Domain, HRect, JaggedDomain}; -use crate::signature::{HRectMapping, HRectPowersetMapping, HRectSignature, Signature}; +use crate::signature::{HRectSignature, Signature}; /// A set of indices, as returned by a `PowersetMapping`. pub type Powerset = HashSet>; @@ -40,6 +40,67 @@ pub trait BoxedPowersetMapping: PowersetMapping { fn hrect(&self) -> &HRect; } +/// What a mapping from a hyperrectangle has to provide. Implementing this rather +/// than `IndexMapping` gets the domain accessors and printability below for +/// free, whatever the codomain; a mapping over a domain that is not a hrect +/// implements `IndexMapping` directly instead, and is not printable. +pub trait HRectMapping { + type Codomain: Domain; + + fn map(&self, index: &[usize]) -> Vec; + + fn signature(&self) -> &Signature; +} + +impl IndexMapping for M { + fn map(&self, index: &[usize]) -> Vec { + HRectMapping::map(self, index) + } + + fn domain(&self) -> &dyn Domain { + self.signature().domain() + } + + fn codomain(&self) -> &dyn Domain { + self.signature().codomain() + } +} + +impl BoxedMapping for M { + fn hrect(&self) -> &HRect { + self.signature().domain() + } +} + +/// The powerset counterpart of `HRectMapping`. +pub trait HRectPowersetMapping { + type Codomain: Domain; + + fn map(&self, index: &[usize]) -> Powerset; + + fn signature(&self) -> &Signature; +} + +impl PowersetMapping for M { + fn map(&self, index: &[usize]) -> Powerset { + HRectPowersetMapping::map(self, index) + } + + fn domain(&self) -> &dyn Domain { + self.signature().domain() + } + + fn codomain(&self) -> &dyn Domain { + self.signature().codomain() + } +} + +impl BoxedPowersetMapping for M { + fn hrect(&self) -> &HRect { + self.signature().domain() + } +} + pub struct Stride { signature: HRectSignature, strides: Vec, diff --git a/src/signature.rs b/src/signature.rs index f6ef957..ee653cd 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -1,8 +1,6 @@ -//! The domain and codomain a mapping runs between, and the traits through which -//! a mapping from a hrect gets its accessors and printability from them. +//! The domain and codomain a mapping runs between. use crate::domain::{Domain, HRect}; -use crate::mapping::{BoxedMapping, BoxedPowersetMapping, IndexMapping, Powerset, PowersetMapping}; /// The domain and codomain a mapping runs between. pub struct Signature { @@ -26,64 +24,3 @@ impl Signature { /// The signature of a mapping between two hyperrectangles. pub type HRectSignature = Signature; - -/// What a mapping from a hyperrectangle has to provide. Implementing this rather -/// than `IndexMapping` gets the domain accessors and printability below for -/// free, whatever the codomain; a mapping over a domain that is not a hrect -/// implements `IndexMapping` directly instead, and is not printable. -pub trait HRectMapping { - type Codomain: Domain; - - fn map(&self, index: &[usize]) -> Vec; - - fn signature(&self) -> &Signature; -} - -impl IndexMapping for M { - fn map(&self, index: &[usize]) -> Vec { - HRectMapping::map(self, index) - } - - fn domain(&self) -> &dyn Domain { - self.signature().domain() - } - - fn codomain(&self) -> &dyn Domain { - self.signature().codomain() - } -} - -impl BoxedMapping for M { - fn hrect(&self) -> &HRect { - self.signature().domain() - } -} - -/// The powerset counterpart of `HRectMapping`. -pub trait HRectPowersetMapping { - type Codomain: Domain; - - fn map(&self, index: &[usize]) -> Powerset; - - fn signature(&self) -> &Signature; -} - -impl PowersetMapping for M { - fn map(&self, index: &[usize]) -> Powerset { - HRectPowersetMapping::map(self, index) - } - - fn domain(&self) -> &dyn Domain { - self.signature().domain() - } - - fn codomain(&self) -> &dyn Domain { - self.signature().codomain() - } -} - -impl BoxedPowersetMapping for M { - fn hrect(&self) -> &HRect { - self.signature().domain() - } -}