1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2015 Threat X, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0

#[derive(Debug, Clone)]
pub enum MfType {
    Triangle(Triangle),
    Trapezoid(Trapezoid),
    Up(Up),
    Down(Down)
}

impl MfType {
    pub fn compute(&self, x: f32) -> f32 {
        match *self {
            MfType::Triangle(ref value) => value.compute(x),
            MfType::Trapezoid(ref value) => value.compute(x),
            MfType::Up(ref value) => value.compute(x),
            MfType::Down(ref value) => value.compute(x)
        }
    }
    
    pub fn name(&self) -> &str {
        match *self {
            MfType::Triangle(ref value) => value.name(),
            MfType::Trapezoid(ref value) => value.name(),
            MfType::Up(ref value) => value.name(),
            MfType::Down(ref value) => value.name()
        }
    }

    
}

#[derive(Debug, Clone)]
pub struct Triangle {
    name: String,
    a: f32,
    b: f32,
    c: f32
}

impl Triangle {
    pub fn new(name: &str, init: Vec<f32>) -> MfType {
        if init.len() != 3 {
            panic!("init var for Triangle needs 3 values");
        }
        let triangle = Triangle {
            name: name.to_owned(),
            a: init[0],
            b: init[1],
            c: init[2]
        };
        MfType::Triangle(triangle)
    }
    
    fn compute(&self, x: f32) -> f32 {
        let g1 = (x - self.a) / (self.b - self.a);
        let g2 = (self.c - x) / (self.c - self.a);
        0f32.max(g1.min(g2))
    }
    
    fn name(&self) -> &str {
        &self.name
    }
}

#[derive(Debug, Clone)]
pub struct Trapezoid {
    name: String,
    a: f32,
    b: f32,
    c: f32,
    d: f32
}

impl Trapezoid {
    pub fn new(name: &str, init: Vec<f32>) -> MfType {
        if init.len() != 4 {
            panic!("init var for Trapezoid needs 3 values");
        }
        let trapezoid = Trapezoid {
            name: name.to_owned(),
            a: init[0],
            b: init[1],
            c: init[2],
            d: init[3]
        };
        MfType::Trapezoid(trapezoid)
    }
    
    fn compute(&self, x: f32) -> f32 {
        let g1 = (x - self.a) / (self.b - self.a);
        let g2 = (self.d - x) / (self.d - self.c);
        g1.min(g2).min(1f32).max(0f32)
    }

    fn name(&self) -> &str {
        &self.name
    }
}

#[derive(Debug, Clone)]
pub struct Up {
    name: String,
    a: f32,
    b: f32
}

impl Up {
    pub fn new(name: &str, init: Vec<f32>) -> MfType {
        if init.len() != 2 {
            panic!("init var for Up needs 2 values");
        }
        let up = Up {
            name: name.to_owned(),
            a: init[0],
            b: init[1]
        };
        MfType::Up(up)
    }

    fn compute(&self, x: f32) -> f32 {
        if x < self.a {
            return 0f32
        }
        if x > self.b {
            return 1f32
        }
       (x - self.a) / (self.b - self.a)
        
    }

    fn name(&self) -> &str {
        &self.name
    }
    
}

#[derive(Debug, Clone)]
pub struct Down {
    name: String,
    a: f32,
    b: f32
}

impl Down {
    pub fn new(name: &str, init: Vec<f32>) -> MfType {
        if init.len() != 2 {
            panic!("init var for Up needs 2 values");
        }
        let down = Down {
            name: name.to_owned(),
            a: init[0],
            b: init[1]
        };
        MfType::Down(down)
    }

    fn compute(&self, x: f32) -> f32 {
        let up = Up::new(&self.name, vec![self.a, self.b]);
        1f32 - up.compute(x)
        
    }
    
    fn name(&self) -> &str {
        &self.name
    }
}