radiate_rust/engines/
score.rs

1use std::hash::Hash;
2
3
4// #[derive(Hash)]
5pub struct Score {
6    pub values: Vec<f32>,
7}
8
9impl Score {
10    pub fn from_f32(value: f32) -> Self {
11        if value.is_nan() {
12            panic!("Score value cannot be NaN")
13        }
14
15        Score { values: vec![value] }
16    }
17
18    pub fn from_int(value: i32) -> Self {
19        Score {
20            values: vec![value as f32],
21        }
22    }
23
24    pub fn from_usize(value: usize) -> Self {
25        Score {
26            values: vec![value as f32],
27        }
28    }
29
30    pub fn from_string(value: &str) -> Self {
31        Score {
32            values: vec![value.parse::<f32>().unwrap()],
33        }
34    }
35
36    pub fn as_float(&self) -> f32 {
37        if self.values.len() > 1 {
38            panic!("Score has multiple values, cannot be converted to float")
39        }
40
41        self.values[0]
42    }
43
44    pub fn as_int(&self) -> i32 {
45        if self.values.len() > 1 {
46            panic!("Score has multiple values, cannot be converted to int")
47        }
48
49        self.values[0] as i32
50    }
51
52    pub fn as_string(&self) -> String {
53        if self.values.len() > 1 {
54            panic!("Score has multiple values, cannot be converted to string")
55        }
56
57        self.values[0].to_string()
58    }
59
60    pub fn as_usize(&self) -> usize {
61        if self.values.len() > 1 {
62            panic!("Score has multiple values, cannot be converted to usize")
63        }
64
65        self.values[0] as usize
66    }
67}
68
69impl Clone for Score {
70    fn clone(&self) -> Self {
71        Score { values: self.values.clone() }
72    }
73}
74
75impl PartialEq for Score {
76    fn eq(&self, other: &Self) -> bool {
77        self.values == other.values
78    }
79}
80
81impl PartialOrd for Score {
82    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
83        self.values.partial_cmp(&other.values)
84    }
85}
86
87impl std::fmt::Debug for Score {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        write!(f, "{:?}", self.values)
90    }
91}
92
93impl Eq for Score {}
94
95impl Hash for Score {
96    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
97        let mut hash = 0;
98        for value in &self.values {
99            hash ^= value.to_bits();
100        }
101        hash.hash(state);
102    }
103}