ndhistogram provides a generic Histogram
type, with, among others, fill
and value
methods:
pub trait Histogram<A: Axis, V> {
// ...
fn value(&self, coordinate: &A::Coordinate) -> Option<&V> { ... }
fn fill(&mut self, coordinate: &A::Coordinate)
where
V: Fill,
{ ... }
// ...
}
These histograms can have arbitrary dimensions, combining axes with a variety of possible features. For example
-
A 1D histogram with an axis with uniformly-spaced bins and overflow and underflow bins:
ndgistogram!(Uniform::new(nbins, low, high); usize)
-
A 2D histogram with no over/underflow bins on the second axis
ndhistogram!( Uniform ::new(nbins1, low1, high1), UniformNoFlow::new(nbins2, low2, high2); usize)
I would like to create an adaptor which allows the client to use the aforementioned value
and fill
methods of Histogram
in terms of some TypeICareAbout
, rather than the specific choice of axes. When creating/instantiating/defining a specific adaptor, the client would do two things:
-
Specify the space covered by the histogram. In other words, a choice of axes to be given to
ndhistogram!
. -
Provide a mapping from some
TypeICareAbout
to the coordinates that need to be passed to thefill
andvalue
methods, for that choice of axes. Very roughly,fn to_coordinate(t: &TypeICareAbout) -> <???>::Coordinate
The mapping should be used in the adaptor's fill
and value
methods, roughly like this
fn fill (&mut self, t: &TypeICareAbout) { self.histogram.fill (&to_coordinate(t)); }
fn value(& self, t: &TypeICareAbout) -> usize { *self.histogram.value(&to_coordinate(t)).unwrap_or(&0) }
Enabling the client to interact with the underlying histogram in terms of TypeICareAbout
, without caring about the specific axes being used, beyond the initial choice of axes and mapping function.
How can this be expressed in Rust?