linnet/half_edge/nodestore/
forest.rs

1use super::{NodeStorage, NodeStorageOps, NodeStorageVec};
2use crate::{
3    half_edge::{
4        involution::Hedge,
5        subgraph::{BaseSubgraph, SuBitGraph},
6        swap::Swap,
7        NodeIndex, NodeVec,
8    },
9    tree::{
10        parent_pointer::ParentPointerStore, Forest, ForestNodeStore, ForestNodeStorePreorder,
11        RootData, RootId,
12    },
13};
14
15// pub struct ForestNodeStore<Tree, N> {
16//     forest: Forest<N, Tree>,
17// }
18
19#[derive(Debug, Clone)]
20/// An iterator over the half-edges incident to a graph node when the node
21/// storage is implemented using a [`Forest`].
22///
23/// In this context, a graph node corresponds to a tree in the `Forest`, and the
24/// half-edges incident to that graph node are represented as nodes within that tree.
25/// This iterator typically traverses the nodes of a specific tree in pre-order,
26/// yielding [`Hedge`] identifiers.
27///
28/// # Type Parameters
29///
30/// - `'a`: The lifetime of the borrow from the underlying forest node store.
31/// - `P`: The type of the [`ForestNodeStore`] used by the `Forest`, which must
32///   also implement [`ForestNodeStorePreorder`] to allow pre-order traversal
33///   of tree nodes (representing half-edges).
34pub struct ForestNeighborIter<'a, P: ForestNodeStorePreorder + 'a> {
35    // root: Option<TreeNodeId>, // Potentially for future use or alternative iteration strategies
36    /// The underlying pre-order iterator from the forest's node store.
37    iter: P::Iterator<'a>,
38}
39
40impl<'a, P: ForestNodeStorePreorder + 'a> Iterator for ForestNeighborIter<'a, P> {
41    type Item = Hedge;
42    fn next(&mut self) -> Option<Self::Item> {
43        // if let Some(root) = self.root.take() {
44        //     Some(root.into())
45        // } else {
46        self.iter.next().map(|a| a.into())
47        // }
48    }
49}
50
51impl<'a, P: ForestNodeStorePreorder + 'a> ExactSizeIterator for ForestNeighborIter<'a, P> {
52    fn len(&self) -> usize {
53        self.iter.clone().count()
54    }
55}
56
57impl<V, P: ForestNodeStore + ForestNodeStorePreorder + Clone> NodeStorage for Forest<V, P> {
58    type Storage<N> = Forest<N, P>;
59    type NodeData = V;
60    type Neighbors = SuBitGraph;
61    type NeighborsIter<'a>
62        = ForestNeighborIter<'a, P>
63    where
64        Self: 'a;
65}
66
67impl From<RootId> for NodeIndex {
68    fn from(value: RootId) -> Self {
69        NodeIndex(value.0)
70    }
71}
72
73impl<V, P: ForestNodeStore> Swap<Hedge> for Forest<V, P> {
74    fn len(&self) -> Hedge {
75        Hedge(self.nodes.n_nodes())
76    }
77
78    fn is_zero_length(&self) -> bool {
79        self.nodes.n_nodes() == 0
80    }
81
82    fn swap(&mut self, a: Hedge, b: Hedge) {
83        if a != b {
84            let a = a.into();
85            let b = b.into();
86            self.nodes.swap(a, b);
87            let rid = self.nodes.root_node(a);
88            let r = self.root(rid);
89            self.roots[r.0].root_id = rid;
90            let rid = self.nodes.root_node(b);
91            let r = self.root(rid);
92            self.roots[r.0].root_id = rid;
93        }
94
95        #[cfg(test)]
96        self.validate_structure().unwrap()
97    }
98}
99impl<V, P: ForestNodeStore> Swap<NodeIndex> for Forest<V, P> {
100    fn len(&self) -> NodeIndex {
101        NodeIndex(self.roots.len())
102    }
103
104    fn is_zero_length(&self) -> bool {
105        self.roots.is_empty()
106    }
107    fn swap(&mut self, _i: NodeIndex, _j: NodeIndex) {
108        todo!()
109    }
110}
111
112impl<V, P: ForestNodeStore + ForestNodeStorePreorder + Clone> NodeStorageOps for Forest<V, P> {
113    type Base = SuBitGraph;
114    type OpStorage<N> = Forest<N, P>;
115
116    fn check_nodes(&self) -> Result<(), crate::half_edge::HedgeGraphError> {
117        todo!()
118    }
119    fn extract_nodes(&mut self, _nodes: impl IntoIterator<Item = NodeIndex>) -> (SuBitGraph, Self) {
120        todo!()
121    }
122
123    fn iter(&self) -> impl Iterator<Item = (NodeIndex, &Self::NodeData)> {
124        self.iter_roots()
125            .enumerate()
126            .map(|(rid, (d, _))| (NodeIndex(rid), d))
127    }
128
129    fn build<
130        I: IntoIterator<Item = crate::half_edge::builder::HedgeNodeBuilder<Self::NodeData>>,
131    >(
132        nodes: I,
133        n_hedges: usize,
134    ) -> Self {
135        Forest::from_bitvec_partition(nodes.into_iter().map(|n| {
136            (
137                n.data,
138                SuBitGraph::from_hedge_iter(n.hedges.into_iter(), n_hedges),
139            )
140        }))
141        .unwrap()
142    }
143
144    fn build_with_mapping<
145        I: IntoIterator<Item = crate::half_edge::builder::HedgeNodeBuilder<ND>>,
146        ND,
147    >(
148        nodes: I,
149        n_hedges: usize,
150        mut map_data: impl FnMut(ND) -> Self::NodeData,
151    ) -> Self {
152        Forest::from_bitvec_partition(nodes.into_iter().map(|n| {
153            (
154                map_data(n.data),
155                SuBitGraph::from_hedge_iter(n.hedges.into_iter(), n_hedges),
156            )
157        }))
158        .unwrap()
159    }
160
161    fn drain(self) -> impl Iterator<Item = (NodeIndex, Self::NodeData)> {
162        self.roots
163            .into_iter()
164            .enumerate()
165            .map(|(id, a)| (NodeIndex(id), a.data))
166    }
167
168    fn forget_identification_history(&mut self) -> NodeVec<Self::NodeData> {
169        let mut active_nodes_upper_bound = NodeIndex(0);
170        let mut historical_nodes_lower_bound = self.len();
171
172        // first we swap to the front all nodes that have correct pointers.
173        while active_nodes_upper_bound < historical_nodes_lower_bound {
174            if self
175                .nodes
176                .root_node(self.roots[active_nodes_upper_bound.0].root_id)
177                == self.roots[active_nodes_upper_bound.0].root_id
178            {
179                //left is in the right place
180
181                active_nodes_upper_bound.0 += 1;
182            } else {
183                //left needs to be swapped
184                historical_nodes_lower_bound.0 -= 1;
185                if self
186                    .nodes
187                    .root_node(self.roots[historical_nodes_lower_bound.0].root_id)
188                    == self.roots[historical_nodes_lower_bound.0].root_id
189                {
190                    //only with an extracted that is in the wrong spot
191                    self.swap_roots(
192                        active_nodes_upper_bound.into(),
193                        historical_nodes_lower_bound.into(),
194                    );
195                    active_nodes_upper_bound.0 += 1;
196                }
197            }
198        }
199        self.roots
200            .split_off(active_nodes_upper_bound.0)
201            .into_iter()
202            .map(|r| r.data)
203            .collect()
204    }
205
206    fn extend_mut(&mut self, other: Self) {
207        let nodeshift: NodeIndex = other.len();
208        let shift_roots_by = RootId(self.roots.len());
209        self.roots.extend(other.roots.into_iter().map(|mut a| {
210            a.root_id.0 += nodeshift.0;
211            a
212        }));
213
214        self.nodes.extend(other.nodes, shift_roots_by);
215    }
216
217    fn extend(mut self, other: Self) -> Self {
218        self.extend_mut(other);
219        self
220    }
221
222    fn random(sources: &[Self::Neighbors], sinks: &[Self::Neighbors]) -> Self
223    where
224        Self::NodeData: Default,
225    {
226        let a = NodeStorageVec::<()>::random(sources, sinks)
227            .to_forest::<Self::NodeData, P::NodeData>(|_| Default::default());
228
229        Forest {
230            roots: a.roots,
231            nodes: P::from_pp(a.nodes),
232        }
233    }
234
235    fn delete<S: crate::half_edge::subgraph::SubSetLike<Base = Self::Base>>(
236        &mut self,
237        subgraph: &S,
238    ) {
239        let mut left = Hedge(0);
240        let mut extracted = self.len();
241        // println!("{}", self.nodes.debug_draw(|_| None));
242        // Do the same swapping as for the edge store, so that they line up
243        while left < extracted {
244            // println!("{left},{extracted}");
245            if !subgraph.includes(&left) {
246                //left is in the right place
247                left.0 += 1;
248            } else {
249                //left needs to be swapped
250                extracted.0 -= 1;
251                if !subgraph.includes(&extracted) {
252                    //only with an extracted that is in the wrong spot
253                    // println!("{left}<->{extracted}");
254                    self.swap(left, extracted);
255                    left.0 += 1;
256                }
257            }
258        }
259        // println!("{}", self.nodes.debug_draw(|_| None));
260
261        // Now need to partition the nodes into 3 ranges,
262        // - 0..left_nodes do not contain any half edges in the subgraph,
263        // - left_nodes..overlapping_nodes are all nodes who have incident half-edges inside the subgraph, but not all of them. These ones need to be split
264        // - overlapping_nodes..len are the nodes containing only half-edges in the subgraph
265        let mut left_nodes = NodeIndex(0);
266        let mut extracted_nodes = self.len();
267
268        // first we swap to the front all nodes that contain no edges from subgraph. Since we have swapped all edges in the subgraph to be after left, in the above, now we check if the neighbor iter contains no hedge>=left
269        while left_nodes < extracted_nodes {
270            if self.get_neighbor_iterator(left_nodes).all(|h| h < left) {
271                //left is in the right place
272
273                left_nodes.0 += 1;
274            } else {
275                //left needs to be swapped
276                extracted_nodes.0 -= 1;
277                if self
278                    .get_neighbor_iterator(extracted_nodes)
279                    .all(|h| h < left)
280                {
281                    //only with an extracted that is in the wrong spot
282                    self.swap_roots(left_nodes.into(), extracted_nodes.into());
283                    left_nodes.0 += 1;
284                }
285            }
286        }
287
288        let mut overlapping_nodes = left_nodes;
289        let mut non_overlapping_extracted = self.len();
290
291        // println!(
292        //     "RootsplitnonOverlapping:{}",
293        //     self.nodes.debug_draw(|_| None)
294        // );
295
296        // now we place all overlapping nodes after all nodes totally outside. We can check whether a node is overlapping if it now contains a hedge<left.
297        while overlapping_nodes < non_overlapping_extracted {
298            // println!("looking at possibly overlapping:{overlapping_nodes}");
299            if self
300                .get_neighbor_iterator(overlapping_nodes)
301                .any(|h| h < left)
302            {
303                //overlapping is in the right place, as it intersects (is after left_nodes) but isn't fully included
304                overlapping_nodes.0 += 1;
305                // println!("found overlap");
306            } else {
307                //overlapping needs to be swapped
308                non_overlapping_extracted.0 -= 1;
309                if self
310                    .get_neighbor_iterator(non_overlapping_extracted)
311                    .any(|h| h < left)
312                {
313                    //only with an extracted that is in the wrong spot
314                    self.swap_roots(overlapping_nodes.into(), non_overlapping_extracted.into());
315                    // println!("swap overlapping");
316                    overlapping_nodes.0 += 1;
317                }
318            }
319        }
320
321        // println!("Rootsplit:{}", self.nodes.debug_draw(|_| None));
322
323        // Now all hedges in the subgraph are at the end of the storage (swapped)
324        // and all nodes in the subgraph are also at the end of the nodestore
325        //
326        // We can safely split off the roots after the ones with overlap.
327        let _ = self.roots.split_off(overlapping_nodes.0);
328
329        // We now need to adjust the pointer structure that associates a hedge with its node.
330        // Splitting it off aswell.
331        let _ = self.nodes.split_off(left.into());
332
333        // we need to adjust the root_node_pointer that the root stores, and also shift the root_id that the root node stores.
334        //
335
336        for n in self.nodes.iter_node_id() {
337            let rid = self.nodes.root_node(n);
338            let r = self.nodes.root(rid);
339
340            self.roots[r.0].root_id = rid;
341        }
342
343        #[cfg(test)]
344        self.nodes.validate().unwrap();
345    }
346
347    fn extract<S: crate::half_edge::subgraph::SubSetLike<Base = Self::Base>, V2>(
348        &mut self,
349        subgraph: &S,
350        mut split_node: impl FnMut(&Self::NodeData) -> V2,
351        mut owned_node: impl FnMut(Self::NodeData) -> V2,
352    ) -> Self::OpStorage<V2> {
353        let mut left = Hedge(0);
354        let mut extracted = self.len();
355        println!("{}", self.nodes.debug_draw(|_| None));
356        // Do the same swapping as for the edge store, so that they line up
357        while left < extracted {
358            println!("{left},{extracted}");
359            if !subgraph.includes(&left) {
360                //left is in the right place
361                left.0 += 1;
362            } else {
363                //left needs to be swapped
364                extracted.0 -= 1;
365                if !subgraph.includes(&extracted) {
366                    //only with an extracted that is in the wrong spot
367                    println!("{left}<->{extracted}");
368                    self.swap(left, extracted);
369                    left.0 += 1;
370                }
371            }
372        }
373        println!("{}", self.nodes.debug_draw(|_| None));
374
375        // Now need to partition the nodes into 3 ranges,
376        // - 0..left_nodes do not contain any half edges in the subgraph,
377        // - left_nodes..overlapping_nodes are all nodes who have incident half-edges inside the subgraph, but not all of them. These ones need to be split
378        // - overlapping_nodes..len are the nodes containing only half-edges in the subgraph
379        let mut left_nodes = NodeIndex(0);
380        let mut extracted_nodes = self.len();
381
382        // first we swap to the front all nodes that contain no edges from subgraph. Since we have swapped all edges in the subgraph to be after left, in the above, now we check if the neighbor iter contains no hedge>=left
383        while left_nodes < extracted_nodes {
384            if self.get_neighbor_iterator(left_nodes).all(|h| h < left) {
385                //left is in the right place
386
387                left_nodes.0 += 1;
388            } else {
389                //left needs to be swapped
390                extracted_nodes.0 -= 1;
391                if self
392                    .get_neighbor_iterator(extracted_nodes)
393                    .all(|h| h < left)
394                {
395                    //only with an extracted that is in the wrong spot
396                    self.swap_roots(left_nodes.into(), extracted_nodes.into());
397                    left_nodes.0 += 1;
398                }
399            }
400        }
401
402        let mut overlapping_nodes = left_nodes;
403        let mut non_overlapping_extracted = self.len();
404
405        // println!(
406        //     "RootsplitnonOverlapping:{}",
407        //     self.nodes.debug_draw(|_| None)
408        // );
409
410        // now we place all overlapping nodes after all nodes totally outside. We can check whether a node is overlapping if it now contains a hedge<left.
411        while overlapping_nodes < non_overlapping_extracted {
412            // println!("looking at possibly overlapping:{overlapping_nodes}");
413            if self
414                .get_neighbor_iterator(overlapping_nodes)
415                .any(|h| h < left)
416            {
417                //overlapping is in the right place, as it intersects (is after left_nodes) but isn't fully included
418                overlapping_nodes.0 += 1;
419                // println!("found overlap");
420            } else {
421                //overlapping needs to be swapped
422                non_overlapping_extracted.0 -= 1;
423                if self
424                    .get_neighbor_iterator(non_overlapping_extracted)
425                    .any(|h| h < left)
426                {
427                    //only with an extracted that is in the wrong spot
428                    self.swap_roots(overlapping_nodes.into(), non_overlapping_extracted.into());
429                    // println!("swap overlapping");
430                    overlapping_nodes.0 += 1;
431                }
432            }
433        }
434
435        // println!("Rootsplit:{}", self.nodes.debug_draw(|_| None));
436
437        // Now all hedges in the subgraph are at the end of the storage (swapped)
438        // and all nodes in the subgraph are also at the end of the nodestore
439        //
440        // We can safely split off the roots after the ones with overlap.
441        let extracted_roots: Vec<_> = self
442            .roots
443            .split_off(overlapping_nodes.0)
444            .into_iter()
445            .map(|a| RootData {
446                data: owned_node(a.data),
447                root_id: a.root_id,
448            })
449            .collect();
450
451        let mut overlapping_roots = vec![];
452
453        // For those with overlap, duplicate them with the closure.
454        for i in (left_nodes.0)..(overlapping_nodes.0) {
455            let data = split_node(&self.roots[i].data);
456            // println!("overlapping node found");
457
458            overlapping_roots.push(RootData {
459                data,
460                root_id: self.roots[i].root_id,
461            });
462        }
463
464        // We start with the overlapping and then the fully included, so that we do not need to swap
465        overlapping_roots.extend(extracted_roots);
466
467        // We now need to adjust the pointer structure that associates a hedge with its node.
468        // Splitting it off aswell.
469        let mut extracted_nodes = self.nodes.split_off(left.into());
470
471        // we need to adjust the root_node_pointer that the root stores, and also shift the root_id that the root node stores.
472        //
473
474        #[cfg(test)]
475        extracted_nodes.validate().unwrap();
476        // println!("{}", extracted_nodes.debug_draw(|_| None));
477        for n in extracted_nodes.iter_node_id() {
478            let rid = extracted_nodes.root_node(n);
479            let r = extracted_nodes.root(rid);
480
481            overlapping_roots[r.0 - left_nodes.0].root_id = rid;
482        }
483
484        for n in self.nodes.iter_node_id() {
485            let rid = self.nodes.root_node(n);
486            let r = self.nodes.root(rid);
487
488            self.roots[r.0].root_id = rid;
489        }
490
491        for (i, r) in overlapping_roots.iter().enumerate() {
492            extracted_nodes.set_root(r.root_id, RootId(i))
493        }
494        // println!("{}", extracted_nodes.debug_draw(|_| None));
495        // println!("{}", self.nodes.debug_draw(|_| None));
496        #[cfg(test)]
497        self.nodes.validate().unwrap();
498
499        Forest {
500            nodes: extracted_nodes,
501            roots: overlapping_roots,
502        }
503    }
504
505    fn to_forest<U, H>(
506        &self,
507        map_data: impl Fn(&Self::NodeData) -> U,
508    ) -> Forest<U, ParentPointerStore<H>> {
509        Forest {
510            nodes: self.nodes.forgetful_map().to_store().into(),
511            roots: self
512                .roots
513                .iter()
514                .map(|a| crate::tree::RootData {
515                    data: map_data(&a.data),
516                    root_id: a.root_id,
517                })
518                .collect(),
519        }
520    }
521
522    fn get_neighbor_iterator(&self, node_id: NodeIndex) -> Self::NeighborsIter<'_> {
523        let root_id = self.roots[node_id.0].root_id;
524        ForestNeighborIter {
525            // root: Some(root_id),
526            iter: self.nodes.iter_preorder(root_id),
527        }
528    }
529
530    fn map_data_ref_graph_result<'a, E, V2, H, Er>(
531        &'a self,
532        graph: &'a crate::half_edge::HedgeGraph<E, Self::NodeData, H, Self>,
533        mut node_map: impl FnMut(
534            &'a crate::half_edge::HedgeGraph<E, Self::NodeData, H, Self>,
535            Self::NeighborsIter<'a>,
536            &'a Self::NodeData,
537        ) -> Result<V2, Er>,
538    ) -> Result<Self::OpStorage<V2>, Er> {
539        Ok(Forest {
540            nodes: self.nodes.clone(),
541            roots: self
542                .roots
543                .iter()
544                .enumerate()
545                .map(|(i, r)| {
546                    match node_map(graph, self.get_neighbor_iterator(NodeIndex(i)), &r.data) {
547                        Err(err) => Err(err),
548                        Ok(data) => Ok(RootData {
549                            data,
550                            root_id: r.root_id,
551                        }),
552                    }
553                })
554                .collect::<Result<Vec<_>, Er>>()?,
555        })
556    }
557
558    fn check_and_set_nodes(&mut self) -> Result<(), crate::half_edge::HedgeGraphError> {
559        Ok(())
560    }
561
562    fn iter_nodes(
563        &self,
564    ) -> impl Iterator<Item = (NodeIndex, Self::NeighborsIter<'_>, &Self::NodeData)> {
565        self.roots.iter().enumerate().map(|(i, d)| {
566            (
567                NodeIndex(i),
568                self.get_neighbor_iterator(NodeIndex(i)),
569                &d.data,
570            )
571        })
572    }
573
574    fn node_id_ref(&self, hedge: Hedge) -> NodeIndex {
575        self.root(hedge.into()).into()
576    }
577
578    fn get_node_data(&self, node_id: NodeIndex) -> &Self::NodeData {
579        &self.roots[node_id.0].data
580    }
581
582    fn identify_nodes(
583        &mut self,
584        nodes: &[NodeIndex],
585        node_data_merge: Self::NodeData,
586    ) -> NodeIndex {
587        let first = nodes[0];
588        let x = self.roots[first.0].root_id;
589
590        for i in nodes.iter().skip(1) {
591            let y = self.roots[i.0].root_id;
592            self.nodes.make_root_of(x, y);
593        }
594
595        self.roots[first.0].data = node_data_merge;
596
597        first
598    }
599
600    fn iter_nodes_mut(
601        &mut self,
602    ) -> impl Iterator<Item = (NodeIndex, Self::NeighborsIter<'_>, &mut Self::NodeData)> {
603        self.roots.iter_mut().enumerate().map(|(i, d)| {
604            let root_id = d.root_id;
605            (
606                NodeIndex(i),
607                ForestNeighborIter {
608                    // root: Some(root_id),
609                    iter: self.nodes.iter_preorder(root_id),
610                },
611                &mut d.data,
612            )
613        })
614    }
615
616    fn add_dangling_edge(
617        mut self,
618        source: NodeIndex,
619    ) -> Result<Self, crate::half_edge::HedgeGraphError> {
620        let parent = self.roots[source.0].root_id;
621        self.nodes.add_dataless_child(parent);
622        Ok(self)
623    }
624
625    fn get_node_data_mut(&mut self, node_id: NodeIndex) -> &mut Self::NodeData {
626        &mut self.roots[node_id.0].data
627    }
628
629    fn map_data_graph<'a, V2>(
630        self,
631        involution: &'a crate::half_edge::involution::Involution<
632            crate::half_edge::involution::EdgeIndex,
633        >,
634        mut f: impl FnMut(
635            &'a crate::half_edge::involution::Involution<crate::half_edge::involution::EdgeIndex>,
636            NodeIndex,
637            Self::NodeData,
638        ) -> V2,
639    ) -> Self::OpStorage<V2> {
640        Forest {
641            roots: self
642                .roots
643                .into_iter()
644                .enumerate()
645                .map(|(i, r)| RootData {
646                    data: f(involution, NodeIndex(i), r.data),
647                    root_id: r.root_id,
648                })
649                .collect(),
650            nodes: self.nodes,
651        }
652    }
653
654    fn map_data_graph_result<'a, V2, Err>(
655        self,
656        involution: &'a crate::half_edge::involution::Involution<
657            crate::half_edge::involution::EdgeIndex,
658        >,
659        mut f: impl FnMut(
660            &'a crate::half_edge::involution::Involution<crate::half_edge::involution::EdgeIndex>,
661            NodeIndex,
662            Self::NodeData,
663        ) -> Result<V2, Err>,
664    ) -> Result<Self::OpStorage<V2>, Err> {
665        Ok(Forest {
666            roots: self
667                .roots
668                .into_iter()
669                .enumerate()
670                .map(|(i, r)| match f(involution, NodeIndex(i), r.data) {
671                    Ok(data) => Ok(RootData {
672                        data,
673                        root_id: r.root_id,
674                    }),
675                    Err(e) => Err(e),
676                })
677                .collect::<Result<_, Err>>()?,
678            nodes: self.nodes,
679        })
680    }
681
682    fn map_data_ref_graph<'a, E, V2, H>(
683        &'a self,
684        graph: &'a crate::half_edge::HedgeGraph<E, Self::NodeData, H, Self>,
685        mut node_map: impl FnMut(
686            &'a crate::half_edge::HedgeGraph<E, Self::NodeData, H, Self>,
687            Self::NeighborsIter<'a>,
688            &'a Self::NodeData,
689        ) -> V2,
690    ) -> Self::OpStorage<V2> {
691        Forest {
692            nodes: self.nodes.clone(),
693            roots: self
694                .roots
695                .iter()
696                .enumerate()
697                .map(|(i, r)| RootData {
698                    data: node_map(graph, self.get_neighbor_iterator(NodeIndex(i)), &r.data),
699                    root_id: r.root_id,
700                })
701                .collect(),
702        }
703    }
704
705    fn new_nodevec<'a, V2>(
706        &'a self,
707        mut node_map: impl FnMut(NodeIndex, Self::NeighborsIter<'a>, &'a Self::NodeData) -> V2,
708    ) -> NodeVec<V2> {
709        self.roots
710            .iter()
711            .enumerate()
712            .map(|(i, r)| {
713                node_map(
714                    NodeIndex(i),
715                    self.get_neighbor_iterator(NodeIndex(i)),
716                    &r.data,
717                )
718            })
719            .collect()
720    }
721
722    fn map_data_ref_mut_graph<'a, V2>(
723        &'a mut self,
724        mut node_map: impl FnMut(Self::NeighborsIter<'a>, &'a mut Self::NodeData) -> V2,
725    ) -> Self::OpStorage<V2> {
726        Forest {
727            nodes: self.nodes.clone(),
728            roots: self
729                .roots
730                .iter_mut()
731                // .enumerate()
732                .map(|r| {
733                    let root_id = r.root_id;
734
735                    RootData {
736                        data: node_map(
737                            ForestNeighborIter {
738                                // root: Some(root_id),
739                                iter: self.nodes.iter_preorder(root_id),
740                            },
741                            &mut r.data,
742                        ),
743                        root_id: r.root_id,
744                    }
745                })
746                .collect(),
747        }
748    }
749}
750
751// impl<V,P:ForestNodeStoreDown> Forest<V,P>{
752//     fn hedge_node(&self,root:RootId)->HedgeNode{
753
754//     }
755// }
756
757// impl<V, P: ForestNodeStoreDown> NodeStorageOps for Forest<V, P> {
758//     type Base = BitVec;
759//     type OpStorage<N> = Self::Storage<N>;
760
761//     fn iter(&self) -> impl Iterator<Item = (crate::half_edge::NodeIndex, &Self::NodeData)> {
762//         self.iter_roots()
763//             .enumerate()
764//             .map(|(id, (n, hedge))| (NodeIndex::from(id), n))
765//     }
766
767//     fn iter_nodes(
768//         &self,
769//     ) -> impl Iterator<
770//         Item = (
771//             &crate::half_edge::subgraph::HedgeNode,
772//             NodeIndex,
773//             &Self::NodeData,
774//         ),
775//     > {
776//     }
777// }