Skip to content

Commit 3546ebb

Browse files
authored
Merge pull request #176 from orxfun/leaves_mut
Leaves mut
2 parents 796f6ff + 12a7543 commit 3546ebb

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "orx-tree"
3-
version = "1.10.0"
3+
version = "1.11.0"
44
edition = "2024"
55
authors = ["orxfun <[email protected]>"]
66
description = "A beautiful tree 🌳 with convenient, efficient, parallelizable growth, mutation and traversal features."

src/node_mut.rs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::{
88
subtrees::{MovedSubTree, SubTreeCore},
99
subtrees_within::SubTreeWithin,
1010
traversal::{
11-
OverData, OverMut,
11+
Over, OverData, OverMut,
12+
enumeration::Enumeration,
1213
enumerations::Val,
1314
over::OverPtr,
1415
over_mut::{OverItemInto, OverItemMut},
@@ -2535,6 +2536,88 @@ where
25352536
traverser.into_iter(self)
25362537
}
25372538

2539+
// traversal shorthands
2540+
2541+
/// Returns an iterator of mutable references to data of leaves of the subtree rooted at this node.
2542+
///
2543+
/// The order of the elements is determined by the type of the `traverser` which implements [`Traverser`].
2544+
/// Available implementations are:
2545+
/// * [`Bfs`] for breadth-first ([wikipedia](https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search))
2546+
/// * [`Dfs`] for (pre-order) depth-first ([wikipedia](https://en.wikipedia.org/wiki/Tree_traversal#Depth-first_search))
2547+
/// * [`PostOrder`] for post-order ([wikipedia](https://en.wikipedia.org/wiki/Tree_traversal#Post-order,_LRN))
2548+
///
2549+
/// [`Bfs`]: crate::Bfs
2550+
/// [`Dfs`]: crate::Dfs
2551+
/// [`PostOrder`]: crate::PostOrder
2552+
///
2553+
/// # Examples
2554+
///
2555+
/// ```
2556+
/// use orx_tree::*;
2557+
///
2558+
/// // 1
2559+
/// // ╱ ╲
2560+
/// // ╱ ╲
2561+
/// // 2 3
2562+
/// // ╱ ╲ ╱ ╲
2563+
/// // 4 5 6 7
2564+
/// // | | ╱ ╲
2565+
/// // 8 9 10 11
2566+
///
2567+
/// let mut tree = DynTree::new(1);
2568+
///
2569+
/// let mut root = tree.root_mut();
2570+
/// let [id2, id3] = root.push_children([2, 3]);
2571+
///
2572+
/// let mut n2 = tree.node_mut(&id2);
2573+
/// let [id4, _] = n2.push_children([4, 5]);
2574+
///
2575+
/// tree.node_mut(&id4).push_child(8);
2576+
///
2577+
/// let mut n3 = tree.node_mut(&id3);
2578+
/// let [id6, id7] = n3.push_children([6, 7]);
2579+
///
2580+
/// tree.node_mut(&id6).push_child(9);
2581+
/// tree.node_mut(&id7).push_children([10, 11]);
2582+
///
2583+
/// // access the leaves in different orders that is determined by traversal
2584+
///
2585+
/// let mut root = tree.root_mut();
2586+
/// for (l, leaf) in root.leaves_mut::<Bfs>().enumerate() {
2587+
/// *leaf += 100 * l;
2588+
/// }
2589+
///
2590+
/// let bfs_leaves: Vec<_> = tree.root().leaves::<Bfs>().copied().collect();
2591+
/// assert_eq!(bfs_leaves, [5, 108, 209, 310, 411]);
2592+
///
2593+
/// // get the leaves from any node
2594+
///
2595+
/// let mut n3 = tree.node_mut(&id3);
2596+
/// for (l, leaf) in n3.leaves_mut::<PostOrder>().enumerate() {
2597+
/// *leaf -= 100 * l;
2598+
/// }
2599+
///
2600+
/// let n3 = tree.node(&id3);
2601+
/// let leaves: Vec<_> = n3.leaves::<PostOrder>().copied().collect();
2602+
/// assert_eq!(leaves, [209, 210, 211]);
2603+
/// ```
2604+
pub fn leaves_mut<T>(&'a mut self) -> impl Iterator<Item = &'a mut V::Item>
2605+
where
2606+
T: Traverser<OverData>,
2607+
{
2608+
T::iter_ptr_with_owned_storage(self.node_ptr().clone())
2609+
.filter(|x: &NodePtr<V>| unsafe { &*x.ptr() }.next().is_empty())
2610+
.map(|x: NodePtr<V>| {
2611+
<OverData as Over>::Enumeration::from_element_ptr_mut::<
2612+
'a,
2613+
V,
2614+
M,
2615+
P,
2616+
&'a mut V::Item,
2617+
>(self.col(), x)
2618+
})
2619+
}
2620+
25382621
// recursive
25392622

25402623
/// Recursively sets the data of all nodes belonging to the subtree rooted at this node using the `compute_data`

0 commit comments

Comments
 (0)