partial_function

[Done] Partial Function library for Rust.
git clone https://git.jojolepro.com/partial_function.git
Log | Files | Refs | README | LICENSE

commit f67c9f48161d67044517d657f74eb8113f0d89cc
parent 89b4bead5e1efea4a2a24e99f5bd1962b5b7e828
Author: Jojolepro <jojolepromain@gmail.com>
Date:   Wed,  8 Aug 2018 18:33:37 -0400

Partial implementation of lower bound pf

Diffstat:
MCargo.toml | 2+-
Msrc/lib.rs | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "partial_function" -version = "0.2.0" +version = "0.3.0" authors = ["jojolepro <jojolepromain@gmail.com>"] description = """ A clean way to define function as a set of subfunctions where each has diff --git a/src/lib.rs b/src/lib.rs @@ -102,3 +102,65 @@ where PartialFunction { funcs: self.funcs } } } + + +struct LowerBoundedFunction<B, O> +where + B: PartialOrd, +{ + /// The stored function f(x) = ??? + pub func: fn(B) -> O, + /// The lower bound of the function. + pub lower: B, +} + +pub struct LowerPartialFunction<B, O> +where + B: PartialOrd, +{ + funcs: Vec<LowerBoundedFunction<B, O>>, +} + +/// A builder to create an immutable PartialFunction. +pub struct LowerPartialFunctionBuilder<B, O> +where + B: PartialOrd, +{ + funcs: Vec<LowerBoundedFunction<B, O>>, +} + +impl<B, O> LowerPartialFunctionBuilder<B, O> +where + B: PartialOrd, +{ + /// Creates a new PartialFunctionBuilder. + pub fn new() -> Self { + LowerPartialFunctionBuilder { funcs: vec![] } + } + + /// Adds a bounded function bounded between [lower,higher[ of function func. + pub fn with(mut self, lower: B, func: fn(B) -> O) -> Self { + debug_assert!(self.can_insert(&lower)); + let f = LowerBoundedFunction { + func, + lower, + }; + self.funcs.push(f); + self + } + + /// Check if you can safely insert into the function list for the specified bounds. + pub fn can_insert(&self, lower: &B) -> bool { + !self.funcs.iter().any(|b| lower == &b.lower) + } + + /// Builds the PartialFunction from the functions added using with. + pub fn build(mut self) -> LowerPartialFunction<B, O> { + self.funcs.sort_by(|a, b| { + a.lower + .partial_cmp(&b.lower) + .unwrap_or(Ordering::Equal) + }); + LowerPartialFunction { funcs: self.funcs } + } +}