diff --git a/src/rbtree.rs b/src/rbtree.rs index 8d4b76e..4f3dfcf 100644 --- a/src/rbtree.rs +++ b/src/rbtree.rs @@ -1130,6 +1130,42 @@ where } } + /// Moves the cursor to the parent of current element in the `RBTree`. + /// + /// If the cursor is null or pointing to the root object then this will return None. + #[inline] + pub fn move_parent(&mut self) { + if let Some(current) = self.current { + self.current = unsafe { self.tree.adapter.link_ops().parent(current) } + } else { + self.current = None; + } + } + + /// Moves the cursor to the left of current element in the `RBTree`. + /// + /// If the cursor is null object then this will return None. + #[inline] + pub fn move_left(&mut self) { + if let Some(current) = self.current { + self.current = unsafe { self.tree.adapter.link_ops().left(current) } + } else { + self.current = None; + } + } + + /// Moves the cursor to the right of current element in the `RBTree`. + /// + /// If the cursor is null object then this will return None. + #[inline] + pub fn move_right(&mut self) { + if let Some(current) = self.current { + self.current = unsafe { self.tree.adapter.link_ops().right(current) } + } else { + self.current = None; + } + } + /// Returns a cursor pointing to the next element of the `RBTree`. /// /// If the cursor is pointer to the null object then this will return the @@ -1153,6 +1189,36 @@ where prev.move_prev(); prev } + + /// Returns a cursor pointing to the parent of current element in the `RBTree` + /// + /// If the cursor is null or pointing to the root object then this will return None. + #[inline] + pub fn peek_parent(&self) -> Cursor<'_, A> { + let mut parent = self.clone(); + parent.move_parent(); + parent + } + + /// Returns a cursor pointing to the left of current element in the `RBTree` + /// + /// If the cursor is null object then this will return None. + #[inline] + pub fn peek_left(&self) -> Cursor<'_, A> { + let mut left = self.clone(); + left.move_left(); + left + } + + /// Returns a cursor pointing to the right of current element in the `RBTree` + /// + /// If the cursor is null object then this will return None. + #[inline] + pub fn peek_right(&self) -> Cursor<'_, A> { + let mut right = self.clone(); + right.move_right(); + right + } } /// A cursor which provides mutable access to a `RBTree`. @@ -1229,6 +1295,42 @@ where } } + /// Moves the cursor to the parent of current element in the `RBTree`. + /// + /// If the cursor is null or pointing to the root object then this will return None. + #[inline] + pub fn move_parent(&mut self) { + if let Some(current) = self.current { + self.current = unsafe { self.tree.adapter.link_ops().parent(current) } + } else { + self.current = None; + } + } + + /// Moves the cursor to the left of current element in the `RBTree`. + /// + /// If the cursor is null object then this will return None. + #[inline] + pub fn move_left(&mut self) { + if let Some(current) = self.current { + self.current = unsafe { self.tree.adapter.link_ops().left(current) } + } else { + self.current = None; + } + } + + /// Moves the cursor to the right of current element in the `RBTree`. + /// + /// If the cursor is null object then this will return None. + #[inline] + pub fn move_right(&mut self) { + if let Some(current) = self.current { + self.current = unsafe { self.tree.adapter.link_ops().right(current) } + } else { + self.current = None; + } + } + /// Returns a cursor pointing to the next element of the `RBTree`. /// /// If the cursor is pointer to the null object then this will return the @@ -1253,6 +1355,36 @@ where prev } + /// Returns a cursor pointing to the parent of current element in the `RBTree` + /// + /// If the cursor is null or pointing to the root object then this will return None. + #[inline] + pub fn peek_parent(&self) -> Cursor<'_, A> { + let mut parent = self.as_cursor(); + parent.move_parent(); + parent + } + + /// Returns a cursor pointing to the left of current element in the `RBTree` + /// + /// If the cursor is null object then this will return None. + #[inline] + pub fn peek_left(&self) -> Cursor<'_, A> { + let mut left = self.as_cursor(); + left.move_left(); + left + } + + /// Returns a cursor pointing to the right of current element in the `RBTree` + /// + /// If the cursor is null object then this will return None. + #[inline] + pub fn peek_right(&self) -> Cursor<'_, A> { + let mut right = self.as_cursor(); + right.move_right(); + right + } + /// Removes the current element from the `RBTree`. /// /// A pointer to the element that was removed is returned, and the cursor is