]> Repositorios git - scryer-prolog.git/commitdiff
add STOP_AT_CYCLES const parameter for CycleDetectingIter
authorMark <[email protected]>
Mon, 23 Oct 2023 18:03:25 +0000 (12:03 -0600)
committerMark <[email protected]>
Mon, 23 Oct 2023 18:05:13 +0000 (12:05 -0600)
src/heap_iter.rs
src/machine/cycle_detection.rs

index 2e1bf6ebfd329f30e75ab921ff321bfc80bc1a0a..75c8829ba0a2d88bda9852cce2a33d378f701dec 100644 (file)
@@ -2,7 +2,7 @@
 pub(crate) use crate::machine::gc::StacklessPreOrderHeapIter;
 
 use crate::atom_table::*;
-use crate::machine::cycle_detection::CycleDetectingIter;
+use crate::machine::cycle_detection::*;
 use crate::machine::heap::*;
 use crate::machine::stack::*;
 use crate::types::*;
@@ -509,7 +509,9 @@ impl<'a, ElideLists: ListElisionPolicy> Iterator for StackfulPreOrderHeapIter<'a
 pub(crate) fn cycle_detecting_stackless_preorder_iter<'a>(
     heap: &'a mut [HeapCellValue],
     start: usize,
-) -> CycleDetectingIter<'a> {
+) -> CycleDetectingIter<'a, true> {
+    // const generics argument of true so that cycle discovery stops
+    // the iterator.
     CycleDetectingIter::new(heap, start)
 }
 
index 8de3e8e626664ed7014720c7d58a289fd4a603ce..384017ae5e87219d835b7bd5fbf06c630d49536e 100644 (file)
@@ -21,7 +21,7 @@ use crate::types::*;
  */
 
 #[derive(Debug)]
-pub(crate) struct CycleDetectingIter<'a> {
+pub(crate) struct CycleDetectingIter<'a, const STOP_AT_CYCLES: bool> {
     pub(crate) heap: &'a mut [HeapCellValue],
     start: usize,
     current: usize,
@@ -30,7 +30,7 @@ pub(crate) struct CycleDetectingIter<'a> {
     mark_phase: bool,
 }
 
-impl<'a> CycleDetectingIter<'a> {
+impl<'a, const STOP_AT_CYCLES: bool> CycleDetectingIter<'a, STOP_AT_CYCLES> {
     pub(crate) fn new(heap: &'a mut [HeapCellValue], start: usize) -> Self {
         heap[start].set_forwarding_bit(true);
         let next = heap[start].get_value();
@@ -52,7 +52,7 @@ impl<'a> CycleDetectingIter<'a> {
 
     #[inline]
     fn cycle_detection_active(&self) -> bool {
-        self.mark_phase && !self.cycle_found
+        STOP_AT_CYCLES && self.mark_phase && !self.cycle_found
     }
 
     fn backward_and_return(&mut self) -> HeapCellValue {
@@ -401,7 +401,7 @@ impl<'a> CycleDetectingIter<'a> {
     }
 }
 
-impl<'a> Iterator for CycleDetectingIter<'a> {
+impl<'a, const STOP_AT_CYCLES: bool> Iterator for CycleDetectingIter<'a, STOP_AT_CYCLES> {
     type Item = HeapCellValue;
 
     #[inline]
@@ -411,7 +411,7 @@ impl<'a> Iterator for CycleDetectingIter<'a> {
 }
 
 
-impl<'a> Drop for CycleDetectingIter<'a> {
+impl<'a, const STOP_AT_CYCLES: bool> Drop for CycleDetectingIter<'a, STOP_AT_CYCLES> {
     fn drop(&mut self) {
         self.invert_marker();