eliminate duplicates in term_variables/2, term_attributed_variables/2 (#481)

This commit is contained in:
Mark Thom
2020-05-10 13:43:39 -06:00
parent ea7b1a9592
commit c5057127ee
2 changed files with 14 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
use crate::prolog::heap_iter::*;
use crate::prolog::machine::*;
use crate::indexmap::IndexSet;
use std::cmp::Ordering;
use std::vec::IntoIter;
@@ -141,12 +143,19 @@ impl MachineState {
pub(super)
fn attr_vars_of_term(&self, addr: Addr) -> Vec<Addr> {
let mut seen_set = IndexSet::new();
let mut seen_vars = vec![];
let mut iter = self.acyclic_pre_order_iter(addr);
while let Some(addr) = iter.next() {
if let HeapCellValue::Addr(Addr::AttrVar(h)) = self.heap.index_addr(&addr).as_ref() {
if seen_set.contains(h) {
continue;
}
seen_vars.push(addr);
seen_set.insert(*h);
let mut l = h + 1;
let mut list_elements = vec![];

View File

@@ -18,6 +18,8 @@ use crate::prolog::ordered_float::OrderedFloat;
use crate::prolog::read::readline;
use crate::prolog::rug::Integer;
use crate::indexmap::IndexSet;
use crate::ref_thread_local::RefThreadLocal;
use std::cmp;
@@ -4838,11 +4840,13 @@ impl MachineState {
}
&SystemClauseType::TermVariables => {
let a1 = self[temp_v!(1)];
let mut seen_set = IndexSet::new();
let mut seen_vars = vec![];
for addr in self.acyclic_pre_order_iter(a1) {
if addr.is_ref() {
if addr.is_ref() && !seen_set.contains(&addr) {
seen_vars.push(addr);
seen_set.insert(addr);
}
}