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
/// A sample of the given dataset
#[derive(Debug)]
pub struct Sample {
    pub inputs: Vec<f64>,
    pub outputs: Option<Vec<f64>>,
}

impl Sample {
    pub fn new(inputs: Vec<f64>, outputs: Vec<f64>) -> Sample {
        Sample {
            inputs: inputs,
            outputs: Some(outputs),
        }
    }

    pub fn predict(inputs: Vec<f64>) -> Sample {
        Sample {
            inputs: inputs,
            outputs: None,
        }
    }

    pub fn get_inputs_count(&self) -> usize {
        self.inputs.len()
    }

    pub fn get_outputs_count(&self) -> usize {
        match &self.outputs {
            &Some(ref outputs) => outputs.len(),
            &None => 0,
        }
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn inputs_count() {
        let sample = Sample::new(vec![1f64, 0f64], vec![0f64]);
        assert_eq!(sample.get_inputs_count(), 2);
    }

    #[test]
    fn outputs_count() {
        let sample = Sample::new(vec![1f64, 0f64], vec![0f64]);
        assert_eq!(sample.get_outputs_count(), 1);
    }

    #[test]
    fn new_predict_inputs_count() {
        let sample = Sample::predict(vec![1f64, 0f64]);
        assert_eq!(sample.get_inputs_count(), 2);
    }

    #[test]
    fn new_predict_output_count() {
        let sample = Sample::predict(vec![1f64, 0f64]);
        assert_eq!(sample.get_outputs_count(), 0);
    }
}