From afc5736418ba3a411dd5f0b0e9e552b1ed73e33e Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Fri, 4 May 2018 20:15:39 -0600 Subject: [PATCH] set up heap_var Cow in heap_print.rs --- src/prolog/heap_iter.rs | 33 ++++++++++++++++++++++++++++++++- src/prolog/heap_print.rs | 24 ++++++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/prolog/heap_iter.rs b/src/prolog/heap_iter.rs index 367daace..33b552aa 100644 --- a/src/prolog/heap_iter.rs +++ b/src/prolog/heap_iter.rs @@ -120,7 +120,7 @@ impl MachineState { pub fn pre_order_iter<'a>(&'a self, a: Addr) -> HCPreOrderIterator<'a> { HCPreOrderIterator::new(self, a) } - + pub fn post_order_iter<'a>(&'a self, a: Addr) -> HCPostOrderIterator<'a> { HCPostOrderIterator::new(HCPreOrderIterator::new(self, a)) } @@ -179,6 +179,37 @@ where HCIter: Iterator + MutStackHCIterator } } +pub struct HCDerefAcyclicIterator { + iter: HCIter, + seen: HashSet +} + +impl HCDerefAcyclicIterator +{ + pub fn new(iter: HCIter) -> Self { + HCDerefAcyclicIterator { iter, seen: HashSet::new() } + } +} + +impl Iterator for HCDerefAcyclicIterator + where HCIter: Iterator + MutStackHCIterator +{ + type Item = HeapCellValue; + + fn next(&mut self) -> Option { + loop { + match self.iter.next() { + Some(HeapCellValue::Addr(addr)) => + if !self.seen.contains(&addr) { + self.seen.insert(addr.clone()); + return Some(HeapCellValue::Addr(addr)); + }, + item => return item + } + } + } +} + pub struct HCZippedAcyclicIterator { i1: HCIter, i2: HCIter, diff --git a/src/prolog/heap_print.rs b/src/prolog/heap_print.rs index f901a5e4..cb8d7bd6 100644 --- a/src/prolog/heap_print.rs +++ b/src/prolog/heap_print.rs @@ -1,6 +1,7 @@ use prolog::ast::*; use prolog::heap_iter::*; +use std::borrow::Cow; use std::cell::Cell; use std::rc::Rc; @@ -143,18 +144,33 @@ pub struct HCPrinter<'a, Formatter, Outputter> { formatter: Formatter, outputter: Outputter, iter: HCPreOrderIterator<'a>, - state_stack: Vec + state_stack: Vec, + heap_locs: Cow<'a, HeapVarDict> } impl<'a, Formatter: HCValueFormatter, Outputter: HCValueOutputter> HCPrinter<'a, Formatter, Outputter> { - pub fn new(iter: HCPreOrderIterator<'a>, formatter: Formatter, outputter: Outputter) - -> Self + pub fn new(iter: HCPreOrderIterator<'a>, fmt: Formatter, output: Outputter) -> Self { - HCPrinter { formatter, outputter, iter, state_stack: vec![] } + HCPrinter { formatter: fmt, + outputter: output, + iter, + state_stack: vec![], + heap_locs: Cow::default() } } + pub fn from_heap_locs(iter: HCPreOrderIterator<'a>, fmt: Formatter, + output: Outputter, heap_locs: &'a HeapVarDict) + -> Self + { + HCPrinter { formatter: fmt, + outputter: output, + iter, + state_stack: vec![], + heap_locs: Cow::Borrowed(heap_locs) } + } + fn handle_heap_term(&mut self, heap_val: HeapCellValue) { match heap_val { HeapCellValue::NamedStr(arity, name, fixity) => { -- 2.54.0