From: Mark Date: Mon, 23 Oct 2023 18:03:25 +0000 (-0600) Subject: add STOP_AT_CYCLES const parameter for CycleDetectingIter X-Git-Tag: remove~13 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=97b899f4a35b4cd58041513759fa0b481b926e87;p=scryer-prolog.git add STOP_AT_CYCLES const parameter for CycleDetectingIter --- diff --git a/src/heap_iter.rs b/src/heap_iter.rs index 2e1bf6eb..75c8829b 100644 --- a/src/heap_iter.rs +++ b/src/heap_iter.rs @@ -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) } diff --git a/src/machine/cycle_detection.rs b/src/machine/cycle_detection.rs index 8de3e8e6..384017ae 100644 --- a/src/machine/cycle_detection.rs +++ b/src/machine/cycle_detection.rs @@ -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();