duskphantom_utils/
traverse.rs

1// Copyright 2024 Duskphantom Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16
17use std::{
18    collections::{HashSet, VecDeque},
19    hash::Hash,
20};
21
22pub trait Node: Eq + Hash + Clone {
23    fn get_succ(&mut self) -> Vec<Self>;
24}
25
26/// Postorder iterator.
27pub struct POIterator<T>
28where
29    T: Node,
30{
31    container: VecDeque<T>,
32}
33
34impl<T> Iterator for POIterator<T>
35where
36    T: Node,
37{
38    type Item = T;
39    fn next(&mut self) -> Option<Self::Item> {
40        self.container.pop_front()
41    }
42}
43
44impl<T> From<T> for POIterator<T>
45where
46    T: Node,
47{
48    fn from(bb: T) -> Self {
49        // Run postorder traversal
50        let mut container = Vec::new();
51        let mut visited = HashSet::new();
52        run_postorder(bb, &mut visited, &mut container);
53
54        // Wrap in iterator
55        Self {
56            container: container.into(),
57        }
58    }
59}
60
61/// Reverse postorder iterator.
62pub struct RPOIterator<T>
63where
64    T: Node,
65{
66    container: Vec<T>,
67}
68
69impl<T> Iterator for RPOIterator<T>
70where
71    T: Node,
72{
73    type Item = T;
74    fn next(&mut self) -> Option<Self::Item> {
75        self.container.pop()
76    }
77}
78
79impl<T> From<T> for RPOIterator<T>
80where
81    T: Node,
82{
83    fn from(bb: T) -> Self {
84        // Run postorder traversal
85        let mut container = Vec::new();
86        let mut visited = HashSet::new();
87        run_postorder(bb, &mut visited, &mut container);
88
89        // Wrap in iterator
90        Self { container }
91    }
92}
93
94/// Run a complete post order traversal.
95fn run_postorder<T>(mut bb: T, visited: &mut HashSet<T>, container: &mut Vec<T>)
96where
97    T: Node,
98{
99    if visited.contains(&bb) {
100        return;
101    }
102    visited.insert(bb.clone());
103    for succ in bb.get_succ() {
104        run_postorder(succ.clone(), visited, container);
105    }
106    container.push(bb);
107}