From e0f2ca80b59787b0b06292dc1e25796d4b54ad9b Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 12 Nov 2017 01:36:19 -0700 Subject: [PATCH] simply post order iterators. --- src/prolog/arithmetic.rs | 15 ++------------- src/prolog/iterators.rs | 19 ++++--------------- 2 files changed, 6 insertions(+), 28 deletions(-) diff --git a/src/prolog/arithmetic.rs b/src/prolog/arithmetic.rs index 8a93ed3f..52a2a89a 100644 --- a/src/prolog/arithmetic.rs +++ b/src/prolog/arithmetic.rs @@ -3,7 +3,6 @@ use prolog::fixtures::*; use prolog::num::{BigInt, Zero}; use prolog::ordered_float::{OrderedFloat}; -use std::cell::Cell; use std::cmp::{min, max}; use std::vec::Vec; @@ -12,20 +11,10 @@ pub struct ArithExprIterator<'a> { } impl<'a> ArithExprIterator<'a> { - fn push_clause(&mut self, child_num: usize, ct: ClauseType<'a>, child_terms: &'a Vec>) - { - self.state_stack.push(IteratorState::Clause(child_num, ct, child_terms)); - } - fn push_subterm(&mut self, lvl: Level, term: &'a Term) { self.state_stack.push(IteratorState::to_state(lvl, term)); } - fn push_final_cons(&mut self, lvl: Level, cell: &'a Cell, head: &'a Term, tail: &'a Term) - { - self.state_stack.push(IteratorState::FinalCons(lvl, cell, head, tail)); - } - fn new(term: &'a Term) -> Result { let state = match term { &Term::AnonVar => @@ -62,12 +51,12 @@ impl<'a> Iterator for ArithExprIterator<'a> { if child_num == child_terms.len() { return Some(TermRef::Clause(ct, child_terms)); } else { - self.push_clause(child_num + 1, ct, child_terms); + self.state_stack.push(IteratorState::Clause(child_num + 1, ct, child_terms)); self.push_subterm(ct.level_of_subterms(), child_terms[child_num].as_ref()); } }, IteratorState::InitialCons(lvl, cell, head, tail) => { - self.push_final_cons(lvl, cell, head, tail); + self.state_stack.push(IteratorState::FinalCons(lvl, cell, head, tail)); self.push_subterm(Level::Deep, tail); self.push_subterm(Level::Deep, head); }, diff --git a/src/prolog/iterators.rs b/src/prolog/iterators.rs index e61a6d5f..a2a96976 100644 --- a/src/prolog/iterators.rs +++ b/src/prolog/iterators.rs @@ -1,6 +1,5 @@ use prolog::ast::*; -use std::cell::Cell; use std::collections::VecDeque; use std::iter::*; use std::vec::Vec; @@ -10,20 +9,10 @@ pub struct QueryIterator<'a> { } impl<'a> QueryIterator<'a> { - fn push_clause(&mut self, child_num: usize, ct: ClauseType<'a>, child_terms: &'a Vec>) - { - self.state_stack.push(IteratorState::Clause(child_num, ct, child_terms)); - } - fn push_subterm(&mut self, lvl: Level, term: &'a Term) { self.state_stack.push(IteratorState::to_state(lvl, term)); } - fn push_final_cons(&mut self, lvl: Level, cell: &'a Cell, head: &'a Term, tail: &'a Term) - { - self.state_stack.push(IteratorState::FinalCons(lvl, cell, head, tail)); - } - fn from_term(term: &'a Term) -> Self { let state = match term { &Term::AnonVar => @@ -76,7 +65,7 @@ impl QueryTerm { impl<'a> Iterator for QueryIterator<'a> { type Item = TermRef<'a>; - + fn next(&mut self) -> Option { while let Some(iter_state) = self.state_stack.pop() { match iter_state { @@ -93,12 +82,12 @@ impl<'a> Iterator for QueryIterator<'a> { return None }; } else { - self.push_clause(child_num + 1, ct, child_terms); + self.state_stack.push(IteratorState::Clause(child_num + 1, ct, child_terms)); self.push_subterm(ct.level_of_subterms(), child_terms[child_num].as_ref()); } }, IteratorState::InitialCons(lvl, cell, head, tail) => { - self.push_final_cons(lvl, cell, head, tail); + self.state_stack.push(IteratorState::FinalCons(lvl, cell, head, tail)); self.push_subterm(Level::Deep, tail); self.push_subterm(Level::Deep, head); }, @@ -111,7 +100,7 @@ impl<'a> Iterator for QueryIterator<'a> { }; } - None + None } } -- 2.54.0