From 4543063f5a160f0aecac8a7232483fa7404d8da7 Mon Sep 17 00:00:00 2001 From: Phoebe Szmucer Date: Mon, 22 Jul 2024 10:19:36 +0100 Subject: [PATCH] use a queue --- src/function/accumulated.rs | 7 ++++--- tests/accumulate-dag.rs | 12 +++++------ tests/accumulate.rs | 42 ++++++++++++++++++------------------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/function/accumulated.rs b/src/function/accumulated.rs index 51b16fa..5cd3ef9 100644 --- a/src/function/accumulated.rs +++ b/src/function/accumulated.rs @@ -1,4 +1,5 @@ use crate::{accumulator, hash::FxHashSet, storage::DatabaseGen, DatabaseKeyIndex, Id}; +use std::collections::VecDeque; use super::{Configuration, IngredientImpl}; @@ -23,9 +24,9 @@ where let db_key = self.database_key_index(key); let mut visited: FxHashSet = std::iter::once(db_key).collect(); - let mut stack = vec![db_key]; + let mut queue: VecDeque = std::iter::once(db_key).collect(); - while let Some(k) = stack.pop() { + while let Some(k) = queue.pop_front() { accumulator.produced_by(runtime, k, &mut output); let origin = db.lookup_ingredient(k.ingredient_index).origin(k.key_index); @@ -34,7 +35,7 @@ where for input in inputs { if let Ok(input) = input.try_into() { if visited.insert(input) { - stack.push(input); + queue.push_back(input); } } } diff --git a/tests/accumulate-dag.rs b/tests/accumulate-dag.rs index d7decab..d0c0cfe 100644 --- a/tests/accumulate-dag.rs +++ b/tests/accumulate-dag.rs @@ -45,6 +45,12 @@ fn accumulate_a_called_twice() { // Check that we don't see logs from `a` appearing twice in the input. expect![[r#" [ + Log( + "log_a(0 of 2)", + ), + Log( + "log_a(1 of 2)", + ), Log( "log_b(0 of 3)", ), @@ -54,12 +60,6 @@ fn accumulate_a_called_twice() { Log( "log_b(2 of 3)", ), - Log( - "log_a(0 of 2)", - ), - Log( - "log_a(1 of 2)", - ), ]"#]] .assert_eq(&format!("{:#?}", logs)); }) diff --git a/tests/accumulate.rs b/tests/accumulate.rs index 0387bac..3b6ce19 100644 --- a/tests/accumulate.rs +++ b/tests/accumulate.rs @@ -94,6 +94,12 @@ fn accumulate_once() { // (execution order). expect![[r#" [ + Log( + "log_a(0 of 2)", + ), + Log( + "log_a(1 of 2)", + ), Log( "log_b(0 of 3)", ), @@ -103,12 +109,6 @@ fn accumulate_once() { Log( "log_b(2 of 3)", ), - Log( - "log_a(0 of 2)", - ), - Log( - "log_a(1 of 2)", - ), ]"#]] .assert_eq(&format!("{:#?}", logs)); } @@ -122,6 +122,12 @@ fn change_a_from_2_to_0() { let logs = push_logs::accumulated::(&db, input); expect![[r#" [ + Log( + "log_a(0 of 2)", + ), + Log( + "log_a(1 of 2)", + ), Log( "log_b(0 of 3)", ), @@ -131,12 +137,6 @@ fn change_a_from_2_to_0() { Log( "log_b(2 of 3)", ), - Log( - "log_a(0 of 2)", - ), - Log( - "log_a(1 of 2)", - ), ]"#]] .assert_eq(&format!("{:#?}", logs)); db.assert_logs(expect![[r#" @@ -177,6 +177,12 @@ fn change_a_from_2_to_1() { let logs = push_logs::accumulated::(&db, input); expect![[r#" [ + Log( + "log_a(0 of 2)", + ), + Log( + "log_a(1 of 2)", + ), Log( "log_b(0 of 3)", ), @@ -186,12 +192,6 @@ fn change_a_from_2_to_1() { Log( "log_b(2 of 3)", ), - Log( - "log_a(0 of 2)", - ), - Log( - "log_a(1 of 2)", - ), ]"#]] .assert_eq(&format!("{:#?}", logs)); db.assert_logs(expect![[r#" @@ -206,6 +206,9 @@ fn change_a_from_2_to_1() { let logs = push_logs::accumulated::(&db, input); expect![[r#" [ + Log( + "log_a(0 of 1)", + ), Log( "log_b(0 of 3)", ), @@ -215,9 +218,6 @@ fn change_a_from_2_to_1() { Log( "log_b(2 of 3)", ), - Log( - "log_a(0 of 1)", - ), ]"#]] .assert_eq(&format!("{:#?}", logs)); db.assert_logs(expect![[r#"