use a queue

This commit is contained in:
Phoebe Szmucer 2024-07-22 10:19:36 +01:00
parent a20c6341ec
commit 4543063f5a
3 changed files with 31 additions and 30 deletions

View file

@ -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<DatabaseKeyIndex> = std::iter::once(db_key).collect();
let mut stack = vec![db_key];
let mut queue: VecDeque<DatabaseKeyIndex> = 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);
}
}
}

View file

@ -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));
})

View file

@ -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::<Log>(&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::<Log>(&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::<Log>(&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#"