Skip to content
Draft
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
132 changes: 132 additions & 0 deletions src/rbtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down