Skip to content

Commit

Permalink
get node id from index with arena
Browse files Browse the repository at this point in the history
  • Loading branch information
coderedart committed Feb 17, 2024
1 parent a0e699f commit 90f582a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ impl<T> Arena<T> {
}
}

/// Retrieves the `NodeId` corresponding to the `Node` at `index` in the `Arena`, if it exists.
///
/// Note: We use 1 based indexing, so the first element is at `1` and not `0`.
///
/// # Examples
///
/// ```
/// # use indextree::Arena;
/// # use std::num::NonZeroUsize;
/// let mut arena = Arena::new();
/// let foo = arena.new_node("foo");
/// let node = arena.get(foo).unwrap();
/// let index: NonZeroUsize = foo.into();
///
/// let new_foo = arena.get_node_id_at(index).unwrap();
/// assert_eq!(foo, new_foo);
///
/// foo.remove(&mut arena);
/// let new_foo = arena.get_node_id_at(index);
/// assert!(new_foo.is_none(), "must be none if the node at the index doesn't exist");
/// ```
pub fn get_node_id_at(&self, index: NonZeroUsize) -> Option<NodeId> {
let index0 = index.get() - 1; // we use 1 based indexing.
self.nodes
.get(index0)
.filter(|n| !n.is_removed())
.map(|node| NodeId::from_non_zero_usize(index, node.stamp))
}

/// Creates a new node from its associated data.
///
/// # Panics
Expand Down

0 comments on commit 90f582a

Please sign in to comment.