add STOP_AT_CYCLES const parameter for CycleDetectingIter

This commit is contained in:
Mark
2023-10-23 12:03:25 -06:00
parent 6a913bc4cc
commit 97b899f4a3
2 changed files with 9 additions and 7 deletions

View File

@@ -2,7 +2,7 @@
pub(crate) use crate::machine::gc::StacklessPreOrderHeapIter; pub(crate) use crate::machine::gc::StacklessPreOrderHeapIter;
use crate::atom_table::*; use crate::atom_table::*;
use crate::machine::cycle_detection::CycleDetectingIter; use crate::machine::cycle_detection::*;
use crate::machine::heap::*; use crate::machine::heap::*;
use crate::machine::stack::*; use crate::machine::stack::*;
use crate::types::*; use crate::types::*;
@@ -509,7 +509,9 @@ impl<'a, ElideLists: ListElisionPolicy> Iterator for StackfulPreOrderHeapIter<'a
pub(crate) fn cycle_detecting_stackless_preorder_iter<'a>( pub(crate) fn cycle_detecting_stackless_preorder_iter<'a>(
heap: &'a mut [HeapCellValue], heap: &'a mut [HeapCellValue],
start: usize, start: usize,
) -> CycleDetectingIter<'a> { ) -> CycleDetectingIter<'a, true> {
// const generics argument of true so that cycle discovery stops
// the iterator.
CycleDetectingIter::new(heap, start) CycleDetectingIter::new(heap, start)
} }

View File

@@ -21,7 +21,7 @@ use crate::types::*;
*/ */
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct CycleDetectingIter<'a> { pub(crate) struct CycleDetectingIter<'a, const STOP_AT_CYCLES: bool> {
pub(crate) heap: &'a mut [HeapCellValue], pub(crate) heap: &'a mut [HeapCellValue],
start: usize, start: usize,
current: usize, current: usize,
@@ -30,7 +30,7 @@ pub(crate) struct CycleDetectingIter<'a> {
mark_phase: bool, 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 { pub(crate) fn new(heap: &'a mut [HeapCellValue], start: usize) -> Self {
heap[start].set_forwarding_bit(true); heap[start].set_forwarding_bit(true);
let next = heap[start].get_value(); let next = heap[start].get_value();
@@ -52,7 +52,7 @@ impl<'a> CycleDetectingIter<'a> {
#[inline] #[inline]
fn cycle_detection_active(&self) -> bool { 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 { 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; type Item = HeapCellValue;
#[inline] #[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) { fn drop(&mut self) {
self.invert_marker(); self.invert_marker();