summaryrefslogtreecommitdiffstats
path: root/CLI/rust/src/game_of_life/simulation/plane.rs
diff options
context:
space:
mode:
authora14m <[email protected]>2021-04-07 14:24:44 +0200
committera14m <[email protected]>2021-04-07 14:24:44 +0200
commitaa0c96b05c7012ac84a3787dcb2b661c7bf53029 (patch)
treed15247aa76f423f8d7220204344de9a53f17f0d7 /CLI/rust/src/game_of_life/simulation/plane.rs
parentbd0ef4dcf5b46664c9e0b206f6acd8cf9b3532da (diff)
Refactor module structure to split universe/plane
The objects will grow bigger and might be hard for readability to keep them in 1 file hence the split
Diffstat (limited to 'CLI/rust/src/game_of_life/simulation/plane.rs')
-rw-r--r--CLI/rust/src/game_of_life/simulation/plane.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/CLI/rust/src/game_of_life/simulation/plane.rs b/CLI/rust/src/game_of_life/simulation/plane.rs
new file mode 100644
index 0000000..10acd43
--- /dev/null
+++ b/CLI/rust/src/game_of_life/simulation/plane.rs
@@ -0,0 +1,72 @@
+use std::ops::{Index, IndexMut};
+use super::circular_index;
+
+/// Game of Life universe building blocks (a plane is a circular Vec)
+///
+/// This is used internally in the implementation of the Universe to allow
+/// more readable and safer traversing of the universe blocks
+#[derive(Debug, Clone, PartialEq)]
+pub(crate) struct Plane(pub Vec<bool>);
+
+impl Index<isize> for Plane {
+ type Output = bool;
+ fn index(&self, idx: isize) -> &bool {
+ let len = self.0.len() as isize;
+ &self.0[circular_index(idx, len)]
+ }
+}
+
+impl IndexMut<isize> for Plane {
+ fn index_mut(&mut self, idx: isize) -> &mut Self::Output {
+ let len = self.0.len() as isize;
+ &mut self.0[circular_index(idx, len)]
+ }
+}
+
+#[cfg(test)]
+mod plane {
+ use super::*;
+
+ mod index {
+ use super::*;
+
+ #[test]
+ fn get_element() {
+ let p = Plane(vec![false, false, true, false, false]);
+ assert_eq!(p[0], false);
+ assert_eq!(p[1], false);
+ assert_eq!(p[3], false);
+ assert_eq!(p[4], false);
+
+ assert_eq!(p[-3], true);
+ assert_eq!(p[2], true);
+ assert_eq!(p[7], true);
+ }
+ }
+
+ #[cfg(test)]
+ mod index_mut {
+ use super::*;
+
+ #[test]
+ fn set_negative_index_element() {
+ let mut p = Plane(vec![false, false, false, false, false]);
+ p[-3] = true;
+ assert_eq!(p, Plane(vec![false, false, true, false, false]));
+ }
+
+ #[test]
+ fn set_element() {
+ let mut p = Plane(vec![false, false, false, false, false]);
+ p[2] = true;
+ assert_eq!(p, Plane(vec![false, false, true, false, false]));
+ }
+
+ #[test]
+ fn set_out_of_bound_element() {
+ let mut p = Plane(vec![false, false, false, false, false]);
+ p[7] = true;
+ assert_eq!(p, Plane(vec![false, false, true, false, false]));
+ }
+ }
+}