-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
199 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use super::{Node, NodeKind, Tree}; | ||
use dioxus::core::ElementId; | ||
|
||
enum Operation { | ||
Key(ElementId), | ||
Pop, | ||
} | ||
|
||
pub enum Item<'a> { | ||
Node { | ||
key: ElementId, | ||
node: &'a Node, | ||
level: usize, | ||
}, | ||
Pop { | ||
level: usize, | ||
}, | ||
} | ||
|
||
pub struct Iter<'a> { | ||
tree: &'a Tree, | ||
stack: Vec<Operation>, | ||
count: usize, | ||
} | ||
|
||
impl<'a> Iter<'a> { | ||
pub(crate) fn new(tree: &'a Tree, root: ElementId) -> Self { | ||
Iter { | ||
tree, | ||
stack: vec![Operation::Key(root)], | ||
count: 0, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Iterator for Iter<'a> { | ||
type Item = Item<'a>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.stack.pop().map(|item| match item { | ||
Operation::Key(key) => { | ||
let node = &self.tree.elements[&key]; | ||
|
||
self.stack.push(Operation::Pop); | ||
if let NodeKind::Element { ref children, .. } = node.kind { | ||
for child in children.iter().copied().map(Operation::Key) { | ||
self.stack.push(child); | ||
} | ||
} | ||
|
||
let count = self.count; | ||
self.count += 1; | ||
|
||
Item::Node { | ||
key, | ||
node, | ||
level: count, | ||
} | ||
} | ||
Operation::Pop => { | ||
self.count -= 1; | ||
Item::Pop { level: self.count } | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use super::{Node, NodeKind, Tree}; | ||
use dioxus::core::ElementId; | ||
use std::marker::PhantomData; | ||
|
||
enum Operation { | ||
Key(ElementId), | ||
Pop, | ||
} | ||
|
||
pub enum ItemMut<'a> { | ||
Node { node: &'a mut Node, level: usize }, | ||
Pop { level: usize }, | ||
} | ||
|
||
pub struct IterMut<'a> { | ||
tree: *mut Tree, | ||
stack: Vec<Operation>, | ||
count: usize, | ||
_marker: PhantomData<&'a mut Tree>, | ||
} | ||
|
||
impl<'a> IterMut<'a> { | ||
pub(crate) fn new(tree: &'a mut Tree, root: ElementId) -> Self { | ||
IterMut { | ||
tree, | ||
stack: vec![Operation::Key(root)], | ||
count: 0, | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Iterator for IterMut<'a> { | ||
type Item = ItemMut<'a>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.stack.pop().map(|item| match item { | ||
Operation::Key(key) => { | ||
let tree = unsafe { &mut *self.tree }; | ||
let node = tree.elements.get_mut(&key).unwrap(); | ||
|
||
if let NodeKind::Element { ref children, .. } = node.kind { | ||
for child in children.iter().copied().map(Operation::Key) { | ||
self.stack.push(child); | ||
} | ||
} | ||
self.stack.push(Operation::Pop); | ||
|
||
let count = self.count; | ||
self.count += 1; | ||
|
||
ItemMut::Node { node, level: count } | ||
} | ||
Operation::Pop => { | ||
self.count -= 1; | ||
ItemMut::Pop { level: self.count } | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters