From 55479cf6f2136e59af5858e746afb3a402b5ec44 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 31 Jan 2024 18:10:01 +0100 Subject: [PATCH] add nodes_including iterator for shared ownership ketrees --- .../src/keyexpr_tree/arc_tree.rs | 51 +++++++++++++++++++ .../src/keyexpr_tree/traits/mod.rs | 6 +++ 2 files changed, 57 insertions(+) diff --git a/commons/zenoh-keyexpr/src/keyexpr_tree/arc_tree.rs b/commons/zenoh-keyexpr/src/keyexpr_tree/arc_tree.rs index 4571ab050e..8e1f608041 100644 --- a/commons/zenoh-keyexpr/src/keyexpr_tree/arc_tree.rs +++ b/commons/zenoh-keyexpr/src/keyexpr_tree/arc_tree.rs @@ -329,6 +329,57 @@ where IterOrOption::Opt(self.node_mut(token, key).map(Into::into)) } } + + type IncluderItem = Self::Node; + type Includer = IterOrOption< + TokenPacker< + Includer< + 'a, + Children, + Arc, Wildness, Children, Token>, Token>>, + Weight, + >, + &'a Token, + >, + Self::IncluderItem, + >; + fn nodes_including(&'a self, token: &'a Token, key: &'a keyexpr) -> Self::Includer { + let inner = ketree_borrow(&self.inner, token); + if inner.wildness.get() || key.is_wild() { + IterOrOption::Iter(TokenPacker { + iter: Includer::new(&inner.children, key), + token, + }) + } else { + IterOrOption::Opt(self.node(token, key)) + } + } + type IncluderItemMut = Self::TreeIterItemMut; + type IncluderMut = IterOrOption< + TokenPacker< + Includer< + 'a, + Children, + Arc, Wildness, Children, Token>, Token>>, + Weight, + >, + &'a mut Token, + >, + Self::IncluderItemMut, + >; + fn nodes_including_mut(&'a self, token: &'a mut Token, key: &'a keyexpr) -> Self::IncluderMut { + let inner = ketree_borrow(&self.inner, token); + if inner.wildness.get() || key.is_wild() { + unsafe { + IterOrOption::Iter(TokenPacker { + iter: Includer::new(core::mem::transmute(&inner.children), key), + token, + }) + } + } else { + IterOrOption::Opt(self.node_mut(token, key).map(Into::into)) + } + } type PruneNode = KeArcTreeNode, Wildness, Children, Token>; fn prune_where bool>( diff --git a/commons/zenoh-keyexpr/src/keyexpr_tree/traits/mod.rs b/commons/zenoh-keyexpr/src/keyexpr_tree/traits/mod.rs index 1a77d4fea8..c83072aed1 100644 --- a/commons/zenoh-keyexpr/src/keyexpr_tree/traits/mod.rs +++ b/commons/zenoh-keyexpr/src/keyexpr_tree/traits/mod.rs @@ -111,6 +111,12 @@ pub trait ITokenKeyExprTree<'a, Weight, Token> { type InclusionItemMut; type InclusionMut: Iterator; fn included_nodes_mut(&'a self, token: &'a mut Token, key: &'a keyexpr) -> Self::InclusionMut; + type IncluderItem; + type Includer: Iterator; + fn nodes_including(&'a self, token: &'a Token, key: &'a keyexpr) -> Self::Includer; + type IncluderItemMut; + type IncluderMut: Iterator; + fn nodes_including_mut(&'a self, token: &'a mut Token, key: &'a keyexpr) -> Self::IncluderMut; type PruneNode: IKeyExprTreeNodeMut; fn prune_where bool>(&self, token: &mut Token, predicate: F); }