Skip to content

Provide consuming movement methods so that NodeMut can act as a cursor. #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 76 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,39 +247,36 @@ impl<'a, T: 'a> NodeRef<'a, T> {
&self.node.value
}

fn axis<F>(&self, f: F) -> Option<Self>
where
F: FnOnce(&Node<T>) -> Option<NodeId>,
{
f(self.node).map(|id| unsafe { self.tree.get_unchecked(id) })
}

/// Returns the parent of this node.
pub fn parent(&self) -> Option<Self> {
self.node
.parent
.map(|id| unsafe { self.tree.get_unchecked(id) })
self.axis(|node| node.parent)
}

/// Returns the previous sibling of this node.
pub fn prev_sibling(&self) -> Option<Self> {
self.node
.prev_sibling
.map(|id| unsafe { self.tree.get_unchecked(id) })
self.axis(|node| node.prev_sibling)
}

/// Returns the next sibling of this node.
pub fn next_sibling(&self) -> Option<Self> {
self.node
.next_sibling
.map(|id| unsafe { self.tree.get_unchecked(id) })
self.axis(|node| node.next_sibling)
}

/// Returns the first child of this node.
pub fn first_child(&self) -> Option<Self> {
self.node
.children
.map(|(id, _)| unsafe { self.tree.get_unchecked(id) })
self.axis(|node| node.children.map(|(id, _)| id))
}

/// Returns the last child of this node.
pub fn last_child(&self) -> Option<Self> {
self.node
.children
.map(|(_, id)| unsafe { self.tree.get_unchecked(id) })
self.axis(|node| node.children.map(|(_, id)| id))
}

/// Returns true if this node has siblings.
Expand Down Expand Up @@ -313,34 +310,88 @@ impl<'a, T: 'a> NodeMut<'a, T> {
&mut self.node().value
}

fn axis<F>(&mut self, f: F) -> Option<NodeMut<T>>
where
F: FnOnce(&mut Node<T>) -> Option<NodeId>,
{
let id = f(self.node());
id.map(move |id| unsafe { self.tree.get_unchecked_mut(id) })
}

fn into_axis<F>(mut self, f: F) -> Result<Self, Self>
where
F: FnOnce(&mut Node<T>) -> Option<NodeId>,
{
let id = f(self.node());
match id {
Some(id) => Ok(unsafe { self.tree.get_unchecked_mut(id) }),
None => Err(self),
}
}

/// Returns the parent of this node.
pub fn parent(&mut self) -> Option<NodeMut<T>> {
let id = self.node().parent;
id.map(move |id| unsafe { self.tree.get_unchecked_mut(id) })
self.axis(|node| node.parent)
}

/// Returns the parent of this node.
///
/// Returns `Ok(parent)` if possible and `Err(self)` otherwise
/// so the caller can recover the current position.
pub fn into_parent(self) -> Result<Self, Self> {
self.into_axis(|node| node.parent)
}

/// Returns the previous sibling of this node.
pub fn prev_sibling(&mut self) -> Option<NodeMut<T>> {
let id = self.node().prev_sibling;
id.map(move |id| unsafe { self.tree.get_unchecked_mut(id) })
self.axis(|node| node.prev_sibling)
}

/// Returns the previous sibling of this node.
///
/// Returns `Ok(prev_sibling)` if possible and `Err(self)` otherwise
/// so the caller can recover the current position.
pub fn into_prev_sibling(self) -> Result<Self, Self> {
self.into_axis(|node| node.prev_sibling)
}

/// Returns the next sibling of this node.
pub fn next_sibling(&mut self) -> Option<NodeMut<T>> {
let id = self.node().next_sibling;
id.map(move |id| unsafe { self.tree.get_unchecked_mut(id) })
self.axis(|node| node.next_sibling)
}

/// Returns the next sibling of this node.
///
/// Returns `Ok(next_sibling)` if possible and `Err(self)` otherwise
/// so the caller can recover the current position.
pub fn into_next_sibling(self) -> Result<Self, Self> {
self.into_axis(|node| node.next_sibling)
}

/// Returns the first child of this node.
pub fn first_child(&mut self) -> Option<NodeMut<T>> {
let ids = self.node().children;
ids.map(move |(id, _)| unsafe { self.tree.get_unchecked_mut(id) })
self.axis(|node| node.children.map(|(id, _)| id))
}

/// Returns the first child of this node.
///
/// Returns `Ok(first_child)` if possible and `Err(self)` otherwise
/// so the caller can recover the current position.
pub fn into_first_child(self) -> Result<Self, Self> {
self.into_axis(|node| node.children.map(|(id, _)| id))
}

/// Returns the last child of this node.
pub fn last_child(&mut self) -> Option<NodeMut<T>> {
let ids = self.node().children;
ids.map(move |(_, id)| unsafe { self.tree.get_unchecked_mut(id) })
self.axis(|node| node.children.map(|(_, id)| id))
}

/// Returns the last child of this node.
///
/// Returns `Ok(last_child)` if possible and `Err(self)` otherwise
/// so the caller can recover the current position.
pub fn into_last_child(self) -> Result<Self, Self> {
self.into_axis(|node| node.children.map(|(_, id)| id))
}

/// Returns true if this node has siblings.
Expand Down
24 changes: 24 additions & 0 deletions tests/node_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,30 @@ fn insert_after() {
assert_eq!(None, d.next_sibling());
}

#[test]
fn insert_at_index() {
let mut tree = tree!('a' => { 'b', 'c', 'e' });

{
let mut root = tree.root_mut();
let mut child = root.first_child().unwrap();

for _ in 0..2 {
child = child.into_next_sibling().unwrap();
}

child.insert_before('d');
}

let descendants = tree
.root()
.descendants()
.map(|n| n.value())
.collect::<Vec<_>>();

assert_eq!(&[&'a', &'b', &'c', &'d', &'e',], &descendants[..]);
}

#[test]
fn detach() {
let mut tree = tree!('a' => { 'b', 'd' });
Expand Down
Loading