simply post order iterators.
This commit is contained in:
@@ -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<Box<Term>>)
|
||||
{
|
||||
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<RegType>, head: &'a Term, tail: &'a Term)
|
||||
{
|
||||
self.state_stack.push(IteratorState::FinalCons(lvl, cell, head, tail));
|
||||
}
|
||||
|
||||
fn new(term: &'a Term) -> Result<Self, ArithmeticError> {
|
||||
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);
|
||||
},
|
||||
|
||||
@@ -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<Box<Term>>)
|
||||
{
|
||||
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<RegType>, 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<Self::Item> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user