@@ -309,6 +309,33 @@ impl<T> DataTree<T> {
309309 }
310310 }
311311
312+ /// Iterate over direct children, yielding `(optional_key, child)` pairs in index order.
313+ ///
314+ /// # Example
315+ /// ```rust
316+ /// use qiskit_providers::DataTree;
317+ /// let mut tree = DataTree::new();
318+ /// tree.push_leaf(10); // unnamed
319+ /// tree.insert_leaf("b", 20); // named
320+ /// tree.push_leaf(30); // unnamed
321+ /// let children: Vec<_> = tree.iter_children().collect();
322+ /// assert_eq!(children[0], (None, &DataTree::Leaf(10)));
323+ /// assert_eq!(children[1], (Some("b"), &DataTree::Leaf(20)));
324+ /// assert_eq!(children[2], (None, &DataTree::Leaf(30)));
325+ /// ```
326+ pub fn iter_children ( & self ) -> impl Iterator < Item = ( Option < & str > , & DataTree < T > ) > + ' _ {
327+ let branch = match self {
328+ Self :: Branch ( branch) => branch,
329+ Self :: Leaf ( _) => panic ! ( "called iter_children() on a leaf node" ) ,
330+ } ;
331+ let rev: HashMap < usize , & str > = branch. keys . iter ( ) . map ( |( k, & v) | ( v, k. as_str ( ) ) ) . collect ( ) ;
332+ branch
333+ . data
334+ . iter ( )
335+ . enumerate ( )
336+ . map ( move |( i, child) | ( rev. get ( & i) . copied ( ) , child) )
337+ }
338+
312339 /// Insert a new leaf node with an associated string key
313340 ///
314341 /// If a key is provided that is already in the tree the new value will be associated with
0 commit comments