scarlet_queen_core/
individual.rs

1//! Mod for `Individual`.
2
3use std::{cell::RefCell, fmt::Debug};
4
5use serde::{ser::SerializeStruct, Serialize};
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8/// Individual for `Group`.
9/// A target value with an id.
10/// * `T` - A type of value.
11///
12/// # Example
13/// ```
14/// use scarlet_queen_core::Individual;
15///
16/// let sample: Individual<u8> = Individual::new(5);
17///
18/// assert_eq!(sample.get_id(), 0usize);
19/// assert_eq!(sample.get_value(), &5u8);
20///
21/// sample.set_id(1);
22///
23/// assert_eq!(sample.get_id(), 1usize);
24/// ```
25pub struct Individual<T> {
26    /// An id of the individual.
27    id: RefCell<usize>,
28    /// A value of the individual.
29    value: T,
30}
31
32impl<T> Individual<T> {
33    /// Make an individual from a value.
34    /// * `value` - A target value.
35    pub fn new(value: T) -> Individual<T> {
36        Self::new_with_id(0, value)
37    }
38
39    /// Make an individual from a value and an id.
40    /// * `id` - An id.
41    /// * `value` - A target value.
42    pub fn new_with_id(id: usize, value: T) -> Individual<T> {
43        Individual {
44            id: RefCell::new(id),
45            value,
46        }
47    }
48
49    /// Get this id.
50    pub fn get_id(&self) -> usize {
51        *self.id.borrow()
52    }
53
54    /// Set this id.
55    ///
56    /// This method does not require mutable borrow.
57    /// * `id` - An id to be set.
58    pub fn set_id(&self, id: usize) {
59        *self.id.borrow_mut() = id;
60    }
61
62    /// Get this value.
63    pub fn get_value(&self) -> &T {
64        &self.value
65    }
66}
67
68impl<T> Serialize for Individual<T>
69where
70    T: Debug,
71{
72    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
73    where
74        S: serde::Serializer,
75    {
76        let mut s: <S as serde::Serializer>::SerializeStruct =
77            serializer.serialize_struct("Individual", 2)?;
78        s.serialize_field("id", &self.id)?;
79        s.serialize_field("value", &format!("{:?}", &self.value))?;
80        s.end()
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use std::cell::RefCell;
87
88    use super::Individual;
89
90    #[test]
91    fn test_individual_new() {
92        let testcases: Vec<(u8, Individual<u8>)> = vec![
93            (
94                5u8,
95                Individual::<u8> {
96                    id: RefCell::new(0),
97                    value: 5,
98                },
99            ),
100            (
101                0u8,
102                Individual::<u8> {
103                    id: RefCell::new(0),
104                    value: 0,
105                },
106            ),
107            (
108                13u8,
109                Individual::<u8> {
110                    id: RefCell::new(0),
111                    value: 13,
112                },
113            ),
114        ];
115        for (arg, result) in testcases.into_iter() {
116            assert_eq!(Individual::<u8>::new(arg), result)
117        }
118    }
119
120    #[test]
121    fn test_individual_newwithid() {
122        let testcases: Vec<((usize, u8), Individual<u8>)> = vec![
123            (
124                (6usize, 5u8),
125                Individual::<u8> {
126                    id: RefCell::new(6),
127                    value: 5,
128                },
129            ),
130            (
131                (10usize, 0u8),
132                Individual::<u8> {
133                    id: RefCell::new(10),
134                    value: 0,
135                },
136            ),
137            (
138                (0usize, 13u8),
139                Individual::<u8> {
140                    id: RefCell::new(0),
141                    value: 13,
142                },
143            ),
144        ];
145        for ((arg_1, arg_2), result) in testcases.into_iter() {
146            assert_eq!(Individual::<u8>::new_with_id(arg_1, arg_2), result)
147        }
148    }
149
150    #[test]
151    fn test_individual_getid() {
152        let testcases: Vec<(Individual<u8>, usize)> = vec![
153            (Individual::new_with_id(6usize, 5u8), 6usize),
154            (Individual::new_with_id(10usize, 0u8), 10usize),
155            (Individual::new_with_id(0usize, 13u8), 0usize),
156        ];
157        for (arg, result) in testcases.into_iter() {
158            assert_eq!(Individual::<u8>::get_id(&arg), result)
159        }
160    }
161
162    #[test]
163    fn test_individual_setid() {
164        let testcases: Vec<(Individual<u8>, usize)> = vec![
165            (Individual::new_with_id(6usize, 5u8), 10usize),
166            (Individual::new_with_id(10usize, 0u8), 0usize),
167            (Individual::new_with_id(0usize, 13u8), 6usize),
168        ];
169        for (arg_1, arg_2) in testcases.into_iter() {
170            arg_1.set_id(arg_2);
171            assert_eq!(Individual::<u8>::get_id(&arg_1), arg_2)
172        }
173    }
174
175    #[test]
176    fn test_individual_getvalue() {
177        let testcases: Vec<(Individual<u8>, u8)> = vec![
178            (Individual::new_with_id(6usize, 5u8), 5u8),
179            (Individual::new_with_id(10usize, 0u8), 0u8),
180            (Individual::new_with_id(0usize, 13u8), 13u8),
181        ];
182        for (arg_1, arg_2) in testcases.into_iter() {
183            assert_eq!(Individual::<u8>::get_value(&arg_1), &arg_2)
184        }
185    }
186}