use std::collections::HashMap;
use std::hash::Hash;
use std::marker::PhantomData;
use derive_new::new;
pub trait EvaluateFunction< Input, Output >{
fn evaluate_function( &self, input: Input ) -> Output;
}
pub trait EvaluateFunctionRef< Input, Output >{
fn evaluate_function_ref( &self, input: &Input ) -> Output;
}
#[derive(Clone,Copy,Debug,new)]
pub struct IdentityFunction;
impl < Input >
EvaluateFunction
< Input, Input > for
IdentityFunction
{
fn evaluate_function( &self, input: Input ) -> Input { input }
}
#[derive(Clone,Copy,Debug,new)]
pub struct LogicalNot< T >{ predicate: T }
impl < T, Input >
EvaluateFunction
< Input, bool > for
LogicalNot
< T > where
T: EvaluateFunction< Input, bool >
{
fn evaluate_function( &self, input: Input ) -> bool { ! self.predicate.evaluate_function(input) }
}
impl < T, Input >
EvaluateFunctionRef
< Input, bool > for
LogicalNot
< T > where
T: EvaluateFunctionRef< Input, bool >
{
fn evaluate_function_ref( &self, input: &Input ) -> bool { ! self.predicate.evaluate_function_ref(input) }
}
#[derive(Clone,Copy,Debug,new)]
pub struct ReverseVector;
impl < T >
EvaluateFunction
< Vec<T>, Vec<T> > for
ReverseVector
{
fn evaluate_function( &self, input: Vec<T> ) -> Vec<T> { let mut a = input; a.reverse(); a }
}
#[derive(Clone,Copy,Debug,new)]
pub struct ReferencedEvaluator< 'a, T >{
pub evaluator: &'a T,
}
impl< 'a, T, Input, Output >
EvaluateFunction
< Input, Output > for
ReferencedEvaluator
< 'a, T >
where
T: EvaluateFunction< Input, Output >
{
fn evaluate_function( &self, input: Input ) -> Output { self.evaluator.evaluate_function(input) }
}
#[derive(Debug)]
pub struct EvaluateFunctionFnMutWrapper< Function > { unwrapped_function: Function }
impl < Function > EvaluateFunctionFnMutWrapper< Function > {
pub fn new( unwrapped_function: Function ) -> EvaluateFunctionFnMutWrapper< Function > { EvaluateFunctionFnMutWrapper {unwrapped_function} }
}
impl < Function, Input, Output >
EvaluateFunction
< Input, Output > for
EvaluateFunctionFnMutWrapper
< Function >
where
Function: Fn( Input ) -> Output
{
fn evaluate_function( & self, input: Input ) -> Output {
(self.unwrapped_function)( input )
}
}
impl < Function >
Clone for
EvaluateFunctionFnMutWrapper
< Function >
where
Function: Clone
{
fn clone( &self ) -> EvaluateFunctionFnMutWrapper<Function>
{ EvaluateFunctionFnMutWrapper::new( self.unwrapped_function.clone() ) }
}
#[derive(Clone,Copy,Debug)]
pub struct EvaluateFunctionDisambiguator< Function: EvaluateFunction< Input, Output >, Input, Output >
{
unwrapped_function: Function,
phantom_input: PhantomData< Input >,
phantom_output: PhantomData< Output >,
}
impl < Input, Output, Function: EvaluateFunction< Input, Output > >
EvaluateFunctionDisambiguator
< Function, Input, Output, >
{
pub fn new( unwrapped_function: Function ) -> Self { EvaluateFunctionDisambiguator{ unwrapped_function, phantom_input: PhantomData, phantom_output: PhantomData } }
}
impl < Input, Output, Function: EvaluateFunction< Input, Output > >
EvaluateFunction
< Input, Output > for
EvaluateFunctionDisambiguator
< Function, Input, Output, >
{
fn evaluate_function( &self, input: Input ) -> Output { self.unwrapped_function.evaluate_function(input) }
}
#[derive(Debug, Clone, Copy)]
pub struct Map< MappingRule >{
pub mapping_rule: MappingRule
}
impl < Input, Output, MappingRule >
EvaluateFunction
< Option< Input >, Option< Output > > for
Map
< MappingRule > where
MappingRule: EvaluateFunction< Input, Output >
{
fn evaluate_function( &self, input: Option< Input > ) -> Option< Output > {
input.map(|x| self.mapping_rule.evaluate_function(x))
}
}
#[derive(Debug, Clone, Copy)]
pub struct AndThen< MappingRule >{
pub mapping_rule: MappingRule
}
impl < Input, Output, MappingRule >
EvaluateFunction
< Option< Input >, Option< Output > > for
AndThen
< MappingRule > where
MappingRule: EvaluateFunction< Input, Option< Output > >
{
fn evaluate_function( &self, input: Option< Input > ) -> Option< Output > {
input.and_then(|x| self.mapping_rule.evaluate_function(x))
}
}
impl < Output: Clone >
EvaluateFunction
< usize, Output > for
Vec< Output >
{
fn evaluate_function( & self, input: usize ) -> Output {
self[ input ].clone()
}
}
impl < 'a, Output: Clone >
EvaluateFunction
< usize, Output > for
&'a Vec< Output >
{
fn evaluate_function( & self, input: usize ) -> Output {
self[ input ].clone()
}
}
impl < 'a, Output >
EvaluateFunction
< usize, &'a Output > for
&'a Vec< Output >
{
fn evaluate_function( & self, input: usize ) -> &'a Output {
& self[ input ]
}
}
impl < Output: Clone >
EvaluateFunction
< usize, Option< Output > > for
Vec< Output >
{
fn evaluate_function( & self, input: usize ) -> Option< Output > {
match input < self.len() {
true => { Some( self[ input ].clone() ) },
false => { None }
}
}
}
impl < 'a, Output: Clone >
EvaluateFunction
< usize, Option< Output > > for
&'a Vec< Output >
{
fn evaluate_function( & self, input: usize ) -> Option< Output > {
match input < self.len() {
true => { Some( self[ input ].clone() ) },
false => { None }
}
}
}
impl < 'a, Output >
EvaluateFunction
< usize, Option< &'a Output > > for
&'a Vec< Output >
{
fn evaluate_function( & self, input: usize ) -> Option< &'a Output > {
match input < self.len() {
true => { Some( & self[ input ] ) },
false => { None }
}
}
}
impl < 'b, Output: Clone >
EvaluateFunction
< &'b usize, Output > for
Vec< Output >
{
fn evaluate_function( & self, input: &'b usize ) -> Output {
self[ *input ].clone()
}
}
impl < 'a, 'b, Output: Clone >
EvaluateFunction
< &'b usize, Output > for
&'a Vec< Output >
{
fn evaluate_function( & self, input: &'b usize ) -> Output {
self[ *input ].clone()
}
}
impl < 'a, 'b, Output >
EvaluateFunction
< &'b usize, &'a Output > for
&'a Vec< Output >
{
fn evaluate_function( & self, input: &'b usize ) -> &'a Output {
& self[ *input ]
}
}
impl < 'b, Output: Clone >
EvaluateFunction
< &'b usize, Option< Output > > for
Vec< Output >
{
fn evaluate_function( & self, input: &'b usize ) -> Option< Output > {
match *input < self.len() {
true => { Some( self[ *input ].clone() ) },
false => { None }
}
}
}
impl < 'a, 'b, Output: Clone >
EvaluateFunction
< &'b usize, Option< Output > > for
&'a Vec< Output >
{
fn evaluate_function( & self, input: &'b usize ) -> Option< Output > {
match *input < self.len() {
true => { Some( self[ *input ].clone() ) },
false => { None }
}
}
}
impl < 'a, 'b, Output >
EvaluateFunction
< &'b usize, Option< &'a Output > > for
&'a Vec< Output >
{
fn evaluate_function( & self, input: &'b usize ) -> Option< &'a Output > {
match *input < self.len() {
true => { Some( & self[ *input ] ) },
false => { None }
}
}
}
impl < Input, Output >
EvaluateFunction
< Input, Output > for
HashMap
< Input, Output >
where
Input: std::cmp::Eq + Hash,
Output: Clone,
{
fn evaluate_function( & self, input: Input ) -> Output {
return self.get( &input ).unwrap().clone()
}
}
impl < Input, Output >
EvaluateFunction
< &Input, Option< Output > > for
HashMap
< Input, Output >
where
Input: std::cmp::Eq + Hash,
Output: Clone,
{
fn evaluate_function( & self, input: &Input ) -> Option< Output > {
return self.get( input ).cloned()
}
}
impl < Input, Output >
EvaluateFunction
< Input, Option< Output > > for
HashMap
< Input, Output >
where
Input: std::cmp::Eq + Hash,
Output: Clone,
{
fn evaluate_function( & self, input: Input ) -> Option< Output > {
return self.get( &input ).cloned()
}
}
impl < 'a, Input, Output >
EvaluateFunction
< &Input, Option< &'a Output > > for
&'a HashMap
< Input, Output >
where
Input: std::cmp::Eq + Hash,
{
fn evaluate_function( & self, input: &Input ) -> Option< &'a Output > {
return self.get( input )
}
}
impl < 'a, Input, Output >
EvaluateFunction
< &Input, &'a Output > for
&'a HashMap
< Input, Output >
where
Input: std::cmp::Eq + Hash,
{
fn evaluate_function( & self, input: &Input ) -> &'a Output {
return self.get( input ).unwrap()
}
}
impl < 'a, Input, Output >
EvaluateFunction
< Input, &'a Output > for
&'a HashMap
< Input, Output >
where
Input: std::cmp::Eq + Hash,
{
fn evaluate_function( & self, input: Input ) -> &'a Output {
return self.get( &input ).unwrap()
}
}
impl < 'a, Input, Output >
EvaluateFunction
< Input, Option< &'a Output > > for
&'a HashMap
< Input, Output >
where
Input: std::cmp::Eq + Hash,
{
fn evaluate_function( & self, input: Input ) -> Option< &'a Output > {
return self.get( &input )
}
}
impl < 'a, Input, Output >
EvaluateFunction
< Input, Option< Output > > for
&'a HashMap
< Input, Output >
where
Input: std::cmp::Eq + Hash,
Output: Clone,
{
fn evaluate_function( & self, input: Input ) -> Option< Output > {
return self.get( &input ).cloned()
}
}
impl < 'a, Input, Output >
EvaluateFunction
< Input, Output > for
&'a HashMap
< Input, Output >
where
Input: std::cmp::Eq + Hash,
Output: Clone,
{
fn evaluate_function( & self, input: Input ) -> Output {
return self.get( &input ).cloned().unwrap()
}
}
#[cfg(test)]
mod doc_test_drafts {
#[test]
fn test_0() {
use crate::utilities::functions::evaluate::{EvaluateFunction, EvaluateFunctionDisambiguator};
use std::collections::HashMap;
let mut hash = HashMap::new();
hash.insert( 1usize, -1);
let disambiguated: EvaluateFunctionDisambiguator::< HashMap< usize, i32 >, usize, i32 >
= EvaluateFunctionDisambiguator::new( hash );
assert_eq!( disambiguated.evaluate_function( 1usize ), -1 );
}
#[test]
fn test_1() {
use crate::utilities::functions::evaluate::EvaluateFunction;
use std::collections::HashMap;
let mut hash = HashMap::new();
hash.insert( 1usize, -1);
assert_eq!( hash.evaluate_function( &1 ), Some( -1 ) );
}
#[test]
fn test_2() {
use crate::utilities::functions::evaluate::{EvaluateFunction, EvaluateFunctionDisambiguator};
use std::collections::HashMap;
let mut hash = HashMap::new();
hash.insert( 1usize, -1);
let disambiguated: EvaluateFunctionDisambiguator::< HashMap< usize, i32 >, usize, Option< i32 > >
= EvaluateFunctionDisambiguator::new( hash );
assert_eq!( disambiguated.evaluate_function( 1usize ), Some( -1i32 ) );
}
#[test]
fn test_3() {
use crate::utilities::functions::evaluate::{EvaluateFunction, EvaluateFunctionDisambiguator};
use std::collections::HashMap;
let mut hash = HashMap::new();
hash.insert( 1usize, -1);
let disambiguated: EvaluateFunctionDisambiguator< &HashMap<usize, i32>, &usize, Option<&i32>, >
= EvaluateFunctionDisambiguator::new( &hash );
assert_eq!( disambiguated.evaluate_function( &1 ), Some( & -1 ) );
}
#[test]
fn test_4() {
use crate::utilities::functions::evaluate::{EvaluateFunction, EvaluateFunctionDisambiguator};
use std::collections::HashMap;
let mut hash = HashMap::new();
hash.insert( 1usize, -1);
let disambiguated: EvaluateFunctionDisambiguator< &HashMap<usize, i32>, usize, Option<&i32>, >
= EvaluateFunctionDisambiguator::new( &hash );
assert_eq!( disambiguated.evaluate_function( 1 ), Some( &-1 ) );
}
#[test]
fn test_5() {
use crate::utilities::functions::evaluate::{EvaluateFunction, EvaluateFunctionDisambiguator};
use std::collections::HashMap;
let mut hash = HashMap::new();
hash.insert( 1usize, -1);
let disambiguated: EvaluateFunctionDisambiguator< &HashMap<usize, i32>, usize, Option<i32>, >
= EvaluateFunctionDisambiguator::new( &hash );
assert_eq!( disambiguated.evaluate_function(1), Some( -1 ) );
}
#[test]
fn test_6() {
use crate::utilities::functions::evaluate::{EvaluateFunction, EvaluateFunctionDisambiguator};
use std::collections::HashMap;
let mut hash = HashMap::new();
hash.insert( 1usize, -1);
let disambiguated: EvaluateFunctionDisambiguator< &HashMap<usize, i32>, usize, i32, >
= EvaluateFunctionDisambiguator::new( &hash );
assert_eq!( disambiguated.evaluate_function( 1 ), -1 );
}
#[test]
fn test_7() {
use std::sync::Arc;
use crate::utilities::functions::evaluate::EvaluateFunction;
let a = Arc::new( vec![0,1,2] );
let b: usize = a.evaluate_function( 0 );
}
}