Add missing path options

Thu, 07 Nov 2024 13:35:28 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 07 Nov 2024 13:35:28 -0500
changeset 30
4fc8c93ed7e8
parent 29
76f9ec073a83
child 31
49227d097d14

Add missing path options

src/cube.rs file | annotate | diff | comparison | revisions
--- a/src/cube.rs	Thu Nov 07 12:49:42 2024 -0500
+++ b/src/cube.rs	Thu Nov 07 13:35:28 2024 -0500
@@ -26,34 +26,28 @@
 
 /// An iterator over paths on a cube, from a source face to a destination face.
 #[derive(Clone, Debug)]
-pub enum PathIter {
-    Direct(Face),
-    Indirect{ destination : Face, intermediate : AdjacentFaces, current : usize},
-    Exhausted,
+pub struct PathIter {
+    destination : Face,
+    intermediate : AdjacentFaces,
+    current : usize
 }
 
 impl std::iter::Iterator for PathIter {
     type Item = Path;
     
     fn next(&mut self) -> Option<Self::Item> {
-        use PathIter::*;
-        match self {
-            &mut Exhausted => None,
-            &mut Direct(destination) => {
-                *self = Exhausted;
-                Some(Path::Direct { destination })
-            },
-            &mut Indirect{destination, intermediate : ref i, ref mut current} => {
-                if *current < i.len() {
-                    let intermediate = i[*current];
-                    *current += 1;
-                    Some(Path::Indirect{ destination, intermediate })
-                } else {
-                    *self = Exhausted;
-                    None
-                }
+        let PathIter { destination, intermediate : ref i, ref mut current } = *self;
+        while *current < i.len() {
+            let intermediate = i[*current];
+            *current += 1;
+            if intermediate == destination {
+                return Some(Path::Direct { destination })
+            } else if intermediate != destination.opposing_face() {
+                return Some(Path::Indirect{ destination, intermediate })
             }
+            // Paths should never go through a face opposing the destination.
         }
+        None
     }
 }
 
@@ -172,15 +166,10 @@
 
     /// Returns an iterator over all the paths from `self` to `other`.
     fn paths(&self, other : Face) -> PathIter {
-        //dbg!(self, other);
-        if self.opposing_face() == other {
-            PathIter::Indirect {
-                intermediate : self.adjacent_faces(),
-                destination : other,
-                current : 0
-            }
-        } else {
-            PathIter::Direct(other)
+        PathIter {
+            intermediate : self.adjacent_faces(),
+            destination : other,
+            current : 0
         }
     }
 

mercurial