From cebb415b08642d74a0f15db1aee80db39a8a8d67 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Thu, 28 Oct 2021 14:35:45 +0200 Subject: [PATCH] Replace two uses of build_array_helper with a new recurse_elem variant For the item index generation and the member population for the C++ structs, a level-order variant of `recurse_elem` is sufficient - as also indicate by the unused item index parameters. --- sixtyfps_compiler/generator/cpp.rs | 2 +- sixtyfps_compiler/object_tree.rs | 18 ++++++++++++++++++ .../passes/generate_item_indices.rs | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/sixtyfps_compiler/generator/cpp.rs b/sixtyfps_compiler/generator/cpp.rs index 597fee486..3e8bf8eb1 100644 --- a/sixtyfps_compiler/generator/cpp.rs +++ b/sixtyfps_compiler/generator/cpp.rs @@ -1170,7 +1170,7 @@ fn generate_component( let mut repeater_count = 0; - super::build_array_helper(component, |item_rc, _, _| { + crate::object_tree::recurse_elem_level_order(&component.root_element, &mut |item_rc| { let item = item_rc.borrow(); if item.base_type == Type::Void { assert!(component.is_global()); diff --git a/sixtyfps_compiler/object_tree.rs b/sixtyfps_compiler/object_tree.rs index 0e6d7152d..d4ef7bc81 100644 --- a/sixtyfps_compiler/object_tree.rs +++ b/sixtyfps_compiler/object_tree.rs @@ -1419,6 +1419,24 @@ pub fn recurse_elem( } } +/// Call the visitor for each children of the element recursively, starting with the element itself. +/// The traversal happens in level-order, meaning each level of the element tree is visisted before +/// going to the next depth. +/// +/// The state returned by the visitor is passed to the children +pub fn recurse_elem_level_order(elem: &ElementRc, vis: &mut impl FnMut(&ElementRc)) { + vis(elem); + visit_children(elem, vis); + fn visit_children(elem: &ElementRc, vis: &mut impl FnMut(&ElementRc)) { + for child in &elem.borrow().children { + vis(child); + } + for child in &elem.borrow().children { + visit_children(child, vis); + } + } +} + /// Same as [`recurse_elem`] but include the elements form sub_components pub fn recurse_elem_including_sub_components( component: &Component, diff --git a/sixtyfps_compiler/passes/generate_item_indices.rs b/sixtyfps_compiler/passes/generate_item_indices.rs index b7977c916..87d2c4146 100644 --- a/sixtyfps_compiler/passes/generate_item_indices.rs +++ b/sixtyfps_compiler/passes/generate_item_indices.rs @@ -10,7 +10,7 @@ LICENSE END */ //! Assign the Element::item_index on each elements pub fn generate_item_indices(component: &std::rc::Rc) { let mut current_item_index: usize = 0; - crate::generator::build_array_helper(component, move |item_rc, _, _| { + crate::object_tree::recurse_elem_level_order(&component.root_element, &mut |item_rc| { let item = item_rc.borrow(); if item.base_type == crate::langtype::Type::Void { } else {